PDA

View Full Version : data switch




farmerdoug
May 1, 2013, 08:33 AM
load_data is called twice. I know that the first time I call it the proper data is loaded in to data_close_array and average. The data gets replaced the second time I call load_data is called although its passed a pointer to a different array.



if ( (data_open_array = (float **)calloc(number_of_stocks, sizeof(float*))) == NULL)
printf("no mem data_open_arrays\n");
else
for (i = 0; i < number_of_stocks; i++)
if ( (data_open_array[i] = (float *)calloc(total_back_test_period, sizeof(float))) == NULL)
printf("no mem data_open array\n");
if ( (average = (float **)calloc(number_of_stocks, sizeof(float*))) == NULL)
printf("no mem data_open_arrays\n");
else
for (i = 0; i < number_of_stocks; i++)
if ( (average[i] = (float *)calloc(total_back_test_period, sizeof(float))) == NULL)
printf("no mem data_open array\n");

if ((data_close_array = (float **)calloc(number_of_stocks, sizeof(float*))) == NULL)
printf("no mem data_close_arrays\n");
else
for (i = 0; i < number_of_stocks; i++)
if ((data_close_array[i] = (float *)calloc(total_back_test_period, sizeof(float))) == NULL)
printf("no mem data_close array\n");;
index_open_array = (float **)calloc(number_of_indices, sizeof(float*));
for (i = 0; i < number_of_indices; i++)
index_open_array[i] = (float *)calloc(total_back_test_period, sizeof(float));
index_close_array = (float **)calloc(number_of_indices, sizeof(float*));
for (i = 0; i < number_of_indices; i++)
index_close_array[i] = (float *)calloc(total_back_test_period, sizeof(float));

stock_list = load_data(dir, list, data_open_array,data_close_array, date_list, average);
index_list = load_data(dir , list,index_open_array,index_close_array, date_list, average);
//NO LONGER HAVE DATA IN ARRAYS THAT WERE PRINTED OUT WHILE RUNNING stock_list = load_data
for ( i = 0; i < total_back_test_period; i++)
printf("%s %f %f\n", date_list[i], data_close_array[0][i], average[0][i]);


char** load_data(char *dir_path, char* list, float **open_array,float **close_array, char **date_list, float **average)
{
FILE *FILELIST, *SEC;
char *name, *full_path, *line_of_data, *close_s, **symbol_list, *cmd;
int day = 0, i, j, symbol_number = 0;
float close = 0, open = 0;

close_s = (char *) calloc (30, sizeof(char));
name = (char *) calloc (100, sizeof(char));
full_path = (char *) calloc (150, sizeof(char));
line_of_data = (char *) calloc (200, sizeof(char));
cmd = (char *) calloc (200, sizeof(char));

symbol_list = (char**) calloc(number_of_indices, sizeof(char*));
for (i = 0; i <number_of_indices; i++)
symbol_list[i] = (char *)calloc(10, sizeof(char));
if( (FILELIST = fopen(list,"r")) == NULL)
{
printf( "couldn't open file list\n");
}


while (fscanf(FILELIST, "%s\n", name) != EOF && symbol_number < number_of_indices)
{
{
strcpy(full_path, dir_path); strcat(full_path, name);
if ( (SEC = fopen(full_path, "r")) == NULL)
printf( "couldn't open index file %s\n", full_path);
else
if (PRINT)
printf( "opened index file %s\n", full_path);
day = 0;
while ( (fgets(line_of_data, 200, SEC)) && day < total_back_test_period)
{
close_s = strtok (line_of_data,","); //symbol

if ( day == 0)
{
strcpy(symbol_list[symbol_number],close_s);
// printf("%s\n",symbol_list[symbol_number]);
}
close_s = strtok (NULL, ",");
close_s = strtok (NULL, ",");// date
if (strcmp(close_s,date_list[total_back_test_period - 1 - day]) == 0)
{

if (strcmp(close_s, "<TICKER>") != 0)
close_s = strtok (NULL, ",");
close_s = strtok (NULL, ",");
open = (float)atof(close_s);
for (i = 0; i < 3; i++)
close_s = strtok (NULL, ",");
close = (float)atof(close_s);

close_array[symbol_number][total_back_test_period - 1 - day] = close;
open_array[symbol_number][total_back_test_period - 1 - day] = open;
day++;
}


}
fclose(SEC);

}
symbol_number++;


}

fclose(FILELIST);

// now do averages
for (j = 0; j < symbol_number; j ++)
{ day = total_back_test_period;
while (day > -1 )
{
for (i = 0; i < average_period; i++)
average[j][day] += close_array[j][day - i]/average_period;
day--;
}
}


return (symbol_list);

}



gnasher729
May 1, 2013, 10:24 AM
Your calculation of averages looks very, very dodgy to me. When day = total_back_test_period it overwrites memory that wasn't allocated. If that's fixed, it still overwrites the averages from the first call in the second call. And I really can't say why averages has to be a two-dimensional array.

farmerdoug
May 1, 2013, 01:53 PM
However, when I do comment out the average calculation the problem goes away. This doesn't make sense because the memory seems to be properly allocated

gnasher729
May 1, 2013, 02:38 PM
for (j = 0; j < symbol_number; j ++)
{
day = total_back_test_period;
while (day > -1 )
{
for (i = 0; i < average_period; i++)
average[j][day] += close_array[j][day - i]/average_period;
day--;
}
}

Initially day = total_back_test_period. So the first assignment
average[j][day] += xxx
assigns to average[j][total_back_test_period]. But the array in question has a size of total_back_test_period elements, so indices must be 0 to total_back_test_period - 1. You write past the bounds of the array that you are allocating.

farmerdoug
May 1, 2013, 02:43 PM
I already fixed that as per your earlier post average[j][total_back_test_period]

Part of the problem was I was using the same average array each time, I called load data. I used have used two different arrays. Furthermore, I was only allocating enough memory for the smaller average array.
However, I'm still missing something

farmerdoug
May 1, 2013, 02:54 PM
All fixed.
Thanks.