memory issue

Discussion in 'Mac Programming' started by farmerdoug, Apr 30, 2013.

  1. macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    I've used this code many times over but I must have done something stupid. I get an EXC_BAD_ACCESS code 2 error in the load data subroutine. Its marked where. It happens on the first attempt to write to the array.

    Thanks.

    Code:
    #include "string.h"
    #include "stdlib.h"
    #include "RunSauce.h"
    
    #define number_of_indices 66
    #define number_of_stocks  10 
    #define total_back_test_period 500
    #define back_test_period 500
    #define pretest 400 
    #define OFF  0  
    
    int main()
    {
    	int  index1, index2,  i,  day, *signal, stock = 0, top, sort, buy_count, sell_count, move, num_of_trades = 0, k, off = OFF;
    	float long_profit, short_profit, gross_profit, gross_loss, long_total_eff, short_total_eff;
    	float  ** index_open_array, ** index_close_array,** data_open_array, ** data_close_array, *sauce, profit = 0, *account, earlier_profit, initial_investment = 100000;
    	char *dir, *list,  **index_list,**stock_list, *output, **date_list, *tmp;
    	struct system
    	{
    		char stock[5];
    		char index1[10];
    		char index2[10];
    		int stock_location;
    		int signal;
    		int index_location[2];
    		int k;
    		float max_profit;
    		int buy;
    		int sell;
    		int num_of_trades;
    		float long_profit;
    		float short_profit;
    		float earlier_profit;
    		float gross_profit;
    		float gross_loss;
    		float long_total_eff;
    		float short_total_eff;
    	};
    	
    	struct system  **maxes;
    	
    	maxes = (struct system **) calloc(number_of_stocks, sizeof(struct system * ));
    		for(i = 0; i < number_of_stocks; i++)
    			maxes[i] = (struct system*) calloc(maxes_saved, sizeof(system));
    
    
    	
    	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 ((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));
    
    	account = (float*) calloc (total_back_test_period, sizeof(float));
    	sauce = (float*) calloc (total_back_test_period, sizeof(float));
    	signal = (int*) calloc (total_back_test_period, sizeof(int));
    	
    	dir = (char *)calloc(200, sizeof(char));
    	list = (char *)calloc(200, sizeof(char));
    	output = (char *)calloc(200, sizeof(char));
    	tmp = (char *)calloc(7, sizeof(char));
    
    for (stock = 0; stock< number_of_stocks; stock++)
      for (top = 0; top < maxes_saved; top ++)
    		{	
    		maxes[stock][top].max_profit = 0;
    		maxes[stock][top].gross_loss = 1;
    		maxes[stock][top].gross_profit = 1;
    		}
    
    	printf("loading stock data\n");
    	strcpy( dir, stock_file_directory);
    	strcpy( list, stock_file_list);
    	date_list = load_date (dir, list);
    	stock_list = load_data(dir, list, data_open_array,data_close_array, date_list);
    
    
    char**  load_data(char *dir_path, char* list, float **open_array,float **close_array, char **date_list)
    {
    	FILE *FILELIST, *SEC;
    	char *name, *full_path, *line_of_data, *close_s, **symbol_list, *cmd;
    	int file_counter = 0, day = 0, i, 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  && file_counter < 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[file_counter][total_back_test_period - 1 - day] = close;
    
    //FAILS HERE
    						open_array[file_counter][total_back_test_period - 1 - day] = open;
    						day++;
    						}
    					
    					
    				}
    			fclose(SEC);
    			
    			}
    		symbol_number++;
    		file_counter++;
    	
    		}
    	
    	fclose(FILELIST);
    	return (symbol_list);
    	
    }
    
    }
    
     
  2. gnasher729, Apr 30, 2013
    Last edited: Apr 30, 2013

    macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    sizeof (system).

    "system" and "struct system" is not the same thing. If you have a variable "int system;" defined somewhere then sizeof (system) is the size of that integer. I'd always use something like

    typedef struct xxx { ... } xxx;

    so you can use the name without the "struct".

    And why does it compile? Because there is a Posix function named "system". sizeof (system) is the same as sizeof (&system); it is the size of a function pointer; four or eight bytes depending on whether you run 32 bit or 64 bit code.
     
  3. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #3
    Thanks I didn't realize that calling it system was a problem. Calling it systm didn't w.ork either so I stuck with typedef.
     

Share This Page