PDA

View Full Version : destroying data




farmerdoug
Jun 23, 2013, 07:11 PM
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);
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);

}



ghellquist
Jun 24, 2013, 08:04 AM
First guess:



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?

farmerdoug
Jun 24, 2013, 11:16 AM
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
Jun 24, 2013, 11:32 AM
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
Jun 24, 2013, 03:58 PM
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.

Mr. Retrofire
Jun 25, 2013, 07:25 AM
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.

MeFromHere
Jun 25, 2013, 12:00 PM
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
Jun 25, 2013, 01:36 PM
#defines have global scope, which makes them suitable for things like global settings, they can also be used in array initializers.

Mr. Retrofire
Jun 26, 2013, 10:00 AM
#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/201101/how-to-initialize-an-array-in-c

subsonix
Jun 26, 2013, 11:56 AM
AFAIK, i used const function pointers as array initializers. This is a rumor.

Other obvious and less obvious ways:
http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c

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};

kaydell.leavitt
Jun 26, 2013, 08:08 PM
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:


#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
Jun 29, 2013, 11:33 AM
What does this code actually do? I'm betting you could do it in bash or Perl and be done with it already...