Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 23, 2013, 07:11 PM   #1
farmerdoug
macrumors 6502a
 
Join Date: Sep 2008
destroying data

Where it says "HERE" are two print statements. From the first one I learn that I continue to load the data in the close_array with out apparent problems. From the second one I see that first the data in close_array[0] are changed to zero then to nonsense; I eventually get an EXC_BAD_ACCESS error. All suggestions appreciated.

Code:
dir = (char *)calloc(200, sizeof(char));
list = (char *)calloc(200, sizeof(char));
data_days = (int *) calloc (number_of_indices, sizeof(int));
/* date_list allocated in another routine
dates = (char**) calloc(days_of_data, sizeof(char*));
	for (i = 0; i < days_of_data; i++)
				dates[i] = (char *)calloc(12, sizeof(char));
*/
vol_close_array = (float **)calloc(number_of_vols, sizeof(float*));
	for (i = 0; i < number_of_vols; i++)
		vol_close_array[i] = (float *)calloc(days_of_data, sizeof(float));

vol_list = load_vols(dir, list, vol_close_array, date_list, data_days, &min);
char**  load_vols(char *dir_path, char* list, float **close_array, char **date_list, int *data_days, int *min)
{ 
	FILE *FILELIST,  *newfile;;
	char  *close_s, **symbol_list;
	int day = 0, i, symbol_number = 0;
	char *oldname,*newname,   *firstline, *date, *junk;
	
	oldname = (char *)calloc(100, sizeof(char));
	newname = (char *)calloc(100, sizeof(char));
	firstline = (char *)calloc(75, sizeof(char));
	date = (char *)calloc(14, sizeof(char));
	junk = (char *)calloc(14, sizeof(char));
	close_s = (char *) calloc (10, sizeof(char));
	symbol_list = (char**) calloc(number_of_vols, sizeof(char*));
	for (i = 0; i <number_of_vols; i++)
		symbol_list[i] = (char *)calloc(10, sizeof(char));
	*min = 10000;
	if( (FILELIST = fopen(list,"r")) == NULL)
		{
		printf( "couldn't open  file list\n");
		}
	
	while((fscanf(FILELIST,"%s\n", oldname) != EOF))
		{
		strcpy(newname, vol_file_directory);
		strcat(newname, oldname);
		if( (newfile = fopen(newname,"r")) == NULL)
		  	printf("couldn't open file %s\n", newname);
		do
			{
			fgets(firstline, 300, newfile);
			date = strtok (firstline,",");
		        }
		while ( strcmp(date, "04/25/2013"));
		junk = strtok (NULL, ","); //time
		junk = strtok (NULL, ",");//open		 
		junk = strtok (NULL, ","); //high		  
		junk = strtok (NULL, ","); //low	  
		close_s = strtok (NULL, ","); //close
		day = 0;
		close_array[symbol_number][days_of_data - day - 1] = (float)atof(close_s);
		printf("%d  %d %s  %f\n",symbol_number, day, date, close_array[symbol_number][days_of_data - day - 1]);
		day ++;
		while (  fgets(firstline, 75, newfile)  && day < days_of_data)
			{			
			date = strtok (firstline,",");			
			junk = strtok (NULL, ","); //time
		  	junk = strtok (NULL, ",");//open		 
			junk = strtok (NULL, ","); //high		  
		  	junk = strtok (NULL, ","); //low	  
		  	close_s = strtok (NULL, ","); //close
			if (day == 1)
				strcpy(symbol_list[symbol_number],oldname);
			close_array[symbol_number][days_of_data - day - 1] = (float)atof(close_s);

//HERE

			printf("%d  %s %d %s %f\n",symbol_number,symbol_list[symbol_number], day, date, close_array[symbol_number][days_of_data - day - 1]);
			printf("%d  %s %d  %f\n",symbol_number, symbol_list[0], day,  close_array[0][day]);


			day++;
			}
			data_days[symbol_number] = day;
			if (data_days[symbol_number] < *min)
					*min = data_days[symbol_number] ;
			symbol_number++;		
			fclose(newfile);
			}
		fclose(FILELIST);
	

	return (symbol_list);
}
farmerdoug is offline   0 Reply With Quote
Old Jun 24, 2013, 08:04 AM   #2
ghellquist
macrumors regular
 
Join Date: Aug 2011
Location: Stockholm Sweden
First guess:

Quote:
Originally Posted by farmerdoug View Post

firstline = (char *)calloc(75, sizeof(char));
fgets(firstline, 300, newfile);
Suggestion: #define constant values

Second suggestion: what happens if any value happens to be longer than 13 chars?

Last edited by ghellquist; Jun 24, 2013 at 08:34 AM.
ghellquist is offline   0 Reply With Quote
Old Jun 24, 2013, 11:16 AM   #3
farmerdoug
Thread Starter
macrumors 6502a
 
Join Date: Sep 2008
Originally, I had allocated 300 chars. I then changed everything to 75 but obviously I missed one. I fixed that but its not the problem.
farmerdoug is offline   0 Reply With Quote
Old Jun 24, 2013, 11:32 AM   #4
farmerdoug
Thread Starter
macrumors 6502a
 
Join Date: Sep 2008
The code in the first post is obviously part of a larger program. I took an earlier version; copy and modified it. I just repeated the process but the modifications were all copied an pasted from the first modified program. I don't get the same errors. Does this make sense to anyone?
farmerdoug is offline   0 Reply With Quote
Old Jun 24, 2013, 03:58 PM   #5
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by ghellquist View Post
First guess:



Suggestion: #define constant values

Second suggestion: what happens if any value happens to be longer than 13 chars?
Use const for constant values. Stop using #define - it exists for the preprocessing and is a hack anywhere else.
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   0 Reply With Quote
Old Jun 25, 2013, 07:25 AM   #6
Mr. Retrofire
macrumors 601
 
Mr. Retrofire's Avatar
 
Join Date: Mar 2010
Location: www.emiliana.cl
Quote:
Originally Posted by ArtOfWarfare View Post
Use const for constant values. Stop using #define - it exists for the preprocessing and is a hack anywhere else.
No. You use the const keyword for variables or parameters, but not for predefined constants. In some cases you can replace #define with enum lists.
__________________

“Only the dead have seen the end of the war.”
-- Plato --
Mr. Retrofire is offline   0 Reply With Quote
Old Jun 25, 2013, 12:00 PM   #7
MeFromHere
macrumors regular
 
Join Date: Oct 2012
Quote:
Originally Posted by Mr. Retrofire View Post
No. You use the const keyword for variables or parameters, but not for predefined constants. In some cases you can replace #define with enum lists.
If you want a predefined numeric constant without type checking, #define is ok. But often its better to use
static const int foo = 1234;
instead. Of course you can and should use an application-specific type instead of "int", as appropriate.
MeFromHere is offline   0 Reply With Quote
Old Jun 25, 2013, 01:36 PM   #8
subsonix
macrumors 68040
 
Join Date: Feb 2008
#defines have global scope, which makes them suitable for things like global settings, they can also be used in array initializers.
subsonix is offline   0 Reply With Quote
Old Jun 26, 2013, 10:00 AM   #9
Mr. Retrofire
macrumors 601
 
Mr. Retrofire's Avatar
 
Join Date: Mar 2010
Location: www.emiliana.cl
Quote:
Originally Posted by subsonix View Post
#defines have global scope, which makes them suitable for things like global settings, they can also be used in array initializers.
AFAIK, i used const function pointers as array initializers. This is a rumor.

Other obvious and less obvious ways:
http://stackoverflow.com/questions/2...-an-array-in-c
__________________

“Only the dead have seen the end of the war.”
-- Plato --
Mr. Retrofire is offline   0 Reply With Quote
Old Jun 26, 2013, 11:56 AM   #10
subsonix
macrumors 68040
 
Join Date: Feb 2008
Quote:
Originally Posted by Mr. Retrofire View Post
AFAIK, i used const function pointers as array initializers. This is a rumor.

Other obvious and less obvious ways:
http://stackoverflow.com/questions/2...-an-array-in-c
Function pointers? Care to explain.

What I meant is, you can declare and initialize an array like this:

Code:
#define SIZE 128

int a[SIZE] = {0};
subsonix is offline   0 Reply With Quote
Old Jun 26, 2013, 08:08 PM   #11
kaydell.leavitt
macrumors regular
 
Join Date: Apr 2010
Some Ideas

My first thought is that maybe you are using a bad index to index an array. I suggest that you write accessor functions to get and set elements of your arrays.

My second thought was to actually run your code, but I'm not sure that it is complete enough to compile.

http://sscce.org

With C, other common problems are variables that aren't getting initialized, functions that don't return a value, or using a pointer where an actual array is needed. Arrays and pointers can be used interchangeably in C, but the memory needs to be allocated before writing to what a pointer points too.

These are just general ideas, but I really think that the problem is a bad index. Here is what I mean by defining constants and writing an accessor function:

Code:
#include <stdio.h>

const int DIR_SIZE = 200;
const int LIST_SIZE = 200;

void setElement(float** close_array, int index1, int index2, float f) {
    if (index1 < 0 || index1 >= LIST_SIZE || index2 < 0 || index2 >= LIST_SIZE) {
        // Bad Index.  Raise an exception or something here.
    } else {
        close_array[index1][index2] = f;
    }
}
If you need more help you can send me an email and I would be glad to help.

-- Kaydell
kaydell@yahoo.com
http://learnmacprogramming.com
kaydell.leavitt is offline   0 Reply With Quote
Old Jun 29, 2013, 11:33 AM   #12
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: USA
What does this code actually do? I'm betting you could do it in bash or Perl and be done with it already...
robvas is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Garage Band Destroying My Podcasts Rusticbohemian Mac Basics and Help 6 Jul 8, 2014 06:17 PM
Big government is destroying the US and the world nec207 Politics, Religion, Social Issues 138 Aug 8, 2013 10:13 AM
iPhone: Something is destroying my battery! Help! Shredder96 Jailbreaks and iOS Hacks 25 May 30, 2013 01:28 PM
Obama is destroying America! (Or unemployment down to 7.7%) mcrain Politics, Religion, Social Issues 135 Mar 18, 2013 07:29 PM
Creating Mirrored RAID without destroying the data KG2002 Mac Pro 0 Dec 26, 2012 12:50 PM

Forum Jump

All times are GMT -5. The time now is 06:16 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC