Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 1, 2013, 08:33 AM   #1
farmerdoug
macrumors 6502a
 
Join Date: Sep 2008
data switch

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.

Code:
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);
	
}
farmerdoug is offline   0 Reply With Quote
Old May 1, 2013, 10:24 AM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
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.
gnasher729 is offline   0 Reply With Quote
Old May 1, 2013, 01:53 PM   #3
farmerdoug
Thread Starter
macrumors 6502a
 
Join Date: Sep 2008
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
farmerdoug is offline   0 Reply With Quote
Old May 1, 2013, 02:38 PM   #4
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by farmerdoug View Post
Code:
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
Code:
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.
gnasher729 is offline   0 Reply With Quote
Old May 1, 2013, 02:43 PM   #5
farmerdoug
Thread Starter
macrumors 6502a
 
Join Date: Sep 2008
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 is offline   0 Reply With Quote
Old May 1, 2013, 02:54 PM   #6
farmerdoug
Thread Starter
macrumors 6502a
 
Join Date: Sep 2008
All fixed.
Thanks.
farmerdoug 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
iPhone: Cellular Data Switch slight Bug Aixwei iOS 7 2 Sep 12, 2013 06:43 AM
Carrier: Any tricks to still switch onto AT&T unlimited data? benguild iPhone 20 Aug 7, 2013 08:32 AM
Can I switch data plans with my grandma? tymaster50 iPhone 6 Oct 23, 2012 02:56 PM
Anyone on Verizon with unlimited data actually switch to new shared plans? EvilEvil iPhone 0 Sep 14, 2012 02:04 PM
Is there an easy way to switch Data Usage ON/OFF? flameproof iPhone and iPod touch Apps 0 Sep 4, 2012 05:02 AM

Forum Jump

All times are GMT -5. The time now is 03:04 PM.

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

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