Mac destroying data

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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?
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,575
4,005
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.
 

MeFromHere

macrumors 6502
Oct 11, 2012
468
16
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.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
#defines have global scope, which makes them suitable for things like global settings, they can also be used in array initializers.
 

kaydell.leavitt

macrumors regular
Apr 19, 2010
100
0
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
 

robvas

macrumors 68030
Mar 29, 2009
2,812
374
USA
What does this code actually do? I'm betting you could do it in bash or Perl and be done with it already...