1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

destroying data

Discussion in 'Mac Programming' started by farmerdoug, Jun 23, 2013.

  1. macrumors 6502a

    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.

    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);
    			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)
    			close_array[symbol_number][days_of_data - day - 1] = (float)atof(close_s);
    			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]);
    			data_days[symbol_number] = day;
    			if (data_days[symbol_number] < *min)
    					*min = data_days[symbol_number] ;
    	return (symbol_list);
  2. ghellquist, Jun 24, 2013
    Last edited: Jun 24, 2013

    macrumors regular

    First guess:

    Suggestion: #define constant values

    Second suggestion: what happens if any value happens to be longer than 13 chars?
  3. macrumors 6502a

    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.
  4. macrumors 6502a

    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?
  5. macrumors 603


    Use const for constant values. Stop using #define - it exists for the preprocessing and is a hack anywhere else.
  6. macrumors 601

    Mr. Retrofire

    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.
  7. macrumors 6502

    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.
  8. macrumors 68040

    #defines have global scope, which makes them suitable for things like global settings, they can also be used in array initializers.
  9. macrumors 601

    Mr. Retrofire

  10. macrumors 68040

    Function pointers? Care to explain.

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

    #define SIZE 128
    int a[SIZE] = {0};
  11. macrumors regular

    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.


    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:

    #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
  12. macrumors 68000

    What does this code actually do? I'm betting you could do it in bash or Perl and be done with it already...

Share This Page