segmentation/abort trap.

Discussion in 'Mac Programming' started by farmerdoug, Sep 28, 2011.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    Full code had no compiling error but gave a segmentation error which I fixed by adding the loop for temp. That led to an abort trap a few lines later. I moved the offending code but the abort trap stayed at the same line were price[l] allocation is. Ideas?
    Code:
    float  **price;
    
    char **temp, ***vol, *date;
    
    temp = (char **) calloc(600,sizeof(char*));
            for (i = 0; i < 2000; i++)
                temp[i] = (char *) calloc(40,sizeof(char));
    
                
             vol = (char ***) calloc(2,sizeof(char**));
                vol[0] = (char **) calloc(2000,sizeof(char*));
                vol[1] = (char **) calloc(2000,sizeof(char*));
                    for(i = 0; i < 2000; i++)
                        {
                        vol[0][i] = (char *) calloc(10,sizeof(char));
                        vol[1][i] = (char *) calloc(10,sizeof(char));
                        }
    
            date = (char *) calloc(12, sizeof(char));       
            price = (float **) calloc(2000,sizeof(float*));
            for(l = 0; l < 2000; l++)
                    price[l] = (float*)calloc(2000,sizeof(float));
       
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Code:
    temp = (char **) calloc([COLOR="Blue"]600[/COLOR],sizeof(char*));  // A
    for (i = 0; i < [COLOR="Red"]2000[/COLOR]; i++)  // B
       temp[i] = (char *) calloc(40,sizeof(char));
    
    A allocates space for 600 char pointers.
    B populates the space with 2000 char pointers.

    Basic programming principle: Avoid magic numbers.

    I didn't bother reading the rest of the code.
     
  3. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #3
    Farmerdoug, you've been posting code snippets like this for quite a while now. Had you invested some time in learning a higher-level language, it would have paid itself back tenfold by now.
     

Share This Page