memory problem in C

Discussion in 'Mac Programming' started by farmerdoug, Oct 6, 2009.

  1. macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    When I run my code I can't open and/or read all the files I need to. Then I get a segmentation error reading an array I have previously read.

    If I delete a calloc call
    for (n=0; n < (waves + 4); n ++)
    matrixa[n] = (float * )calloc((waves+ 4), sizeof(float));
    waves = 23,

    not only can I open and read all the files, the segmentation error goes away.

    Suggestions?
    thanks
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
  3. macrumors 603

    Joined:
    Aug 9, 2009
    #3
    Please post all your code.

    It is not uncommon for errors involving memory allocation to actually be caused by an error in a different location than where the memory problem appears. For example, if an array isn't really the size your later code is expecting, or failing to check for NULL returned from malloc() or calloc(). In short, something else is broken, but it only causes a problem in your 'for' loop.

    Also, since your posted code fragment has nothing to do with opening or reading files, yet your complaint says you have problems doing that, it suggests that some code other than the posted 'for' loop is overrunning memory or otherwise writing things where it shouldn't.

    Those are just guesses, though, without seeing more code.
     
  4. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #4
    Here's the code.

    Here's the code. Sorry I don't remember the editing commands for displaying code. It should also be attached as a txt file.


    #include "p1640.h"

    int main (int argc, char * const argv[]) {

    //open spot images
    int i, n = 0, m = 0;
    FILE *spots[waves];
    char *fitsfile;
    float *spotbuf[waves];
    float *sumspots;
    struct dirent *laserfile;
    int row, col, fprow, fpcol;
    float *spect;
    int *indx;
    float *dbuf = NULL;
    char *testfile = "/data/intermediate/leo/hr8799_O_90-ds.fits";
    float *d, **matrixa;

    matrixa = (float **)calloc(1, sizeof(float*));
    for (n=0; n < (waves + 4); n ++)
    matrixa[n] = (float * )calloc((waves+ 4), sizeof(float));
    indx = (int *)calloc((waves + 4), sizeof(int));
    sumspots = (float *)calloc(3*waves,sizeof(float));
    d = (float *)calloc(1,sizeof(float));
    spect = (float*)calloc(waves + 4, sizeof(float));
    DIR *ld;
    ld = (DIR *) calloc (1, sizeof(DIR));
    ld = opendir(psfdir);
    naxes[0]=naxes[1] = fpsize;


    // load test file
    if ( (dbuf = load_simple_fits_float_data(testfile, &naxis, naxes, &ndatas) ) == NULL)
    {
    (void) stderr, fprintf(stderr, "main: load_simple_fits_float_data %s failed\n", testfile);
    return 0;
    }

    // spots = (FILE **)calloc(1,sizeof(FILE*));

    fitsfile = (char *) calloc(200, sizeof(char*));
    while ( (laserfile = readdir(ld)) != NULL)
    {
    if ( strstr(laserfile->d_name, "fits") != NULL)
    {
    strcpy(fitsfile, psfdir);
    strcat(fitsfile, laserfile->d_name);
    // printf("loaded %s\n ", fitsfile);
    if ( (spotbuf[n++] = load_simple_fits_float_data(fitsfile, &naxis, naxes, &ndatas) ) == NULL)
    {
    (void) stderr, fprintf(stderr, "main: load_simple_fits_float_data %s failed\n", fitsfile);
    return 0;
    }
    else
    {
    printf("loaded %s, size %d\n ", fitsfile, ndatas);
    // for (i = 0; i < 100; i ++)
    // printf( "%f \n", spotbuf[n][2110466+i]);

    }

    }
    else if ( strstr(laserfile->d_name, "txt") != NULL)
    {
    strcpy(fitsfile, psfdir);
    strcat(fitsfile, laserfile->d_name);
    if ( (spots[m++] = fopen(fitsfile, "r"))== NULL)
    {
    printf("failed to open txt file %s\n", fitsfile);
    return 0;
    }
    else
    {
    printf("opened %s\n ", fitsfile);

    }
    }



    } // end loading files.


    // make 1 - d spectrum and sumspots

    printf("here\n");
    for (n=0; n<waves; n++)
    {
    if(n==0)
    {
    fscanf(spots[n],"%d %d, %d %d\n", &col, &row, &fpcol, &fprow);
    for(i=-1; i<2; i++)
    {
    spect[2] += dbuf[(fprow)*fpsize + fpcol - 1+ i];
    spect[3] += dbuf[(fprow-1)*fpsize + fpcol - 1 + i];
    printf("%d\n",(fprow)*fpsize + fpcol - 1+ i);
    sumspots[3*n] += spotbuf[n][(fprow)*fpsize + fpcol - 1+ i];
    sumspots[3*n + 1] += 4;//spotbuf[n][(fprow - 1)*fpsize + fpcol - 1+ i];
    sumspots[3*n + 2] += 4;//spotbuf[n][(fprow -2)*fpsize + fpcol - 1 + i];
    }
    }
    /* else if ((n>0) && n<(waves-1))
    {
    fscanf(spots[n],"%d %d, %d %d\n", &col, &row, &fpcol, &fprow);
    for(i=-1; i<2; i++)
    {
    spect[n+3] += dbuf[(fprow-1)*fpsize + fpcol - 1 + i];
    sumspots[3*n] += spotbuf[n][(fprow - 2)*fpsize + fpcol - 1+ i];
    sumspots[3*n + 1] += spotbuf[n][(fprow - 1)*fpsize + fpcol - 1+ i];
    sumspots[3*n + 2] += spotbuf[n][(fprow)*fpsize + fpcol - 1 + i];
    }

    }
    else if (n== (waves - 1))
    {
    fscanf(spots[n],"%d %d, %d %d\n", &col, &row, &fpcol, &fprow);
    for(i=-1; i<2; i++)
    {
    spect[n+3] += dbuf[(fprow-1)*fpsize + fpcol - 1 + i];
    spect[n+4] += dbuf[(fprow-2)*fpsize + fpcol - 1 + i];
    sumspots[3*n] += spotbuf[n][(fprow - 2)*fpsize + col - 1+ i];
    sumspots[3*n + 1] += spotbuf[n][(fprow - 1)*fpsize + col - 1+ i];
    sumspots[3*n + 2] += spotbuf[n][(fprow)*fpsize + col - 1 + i];
    }
    }
    //printf("%d %d, %d %d\n", col, row, fpcol, fprow);
    */ }
    printf("here\n");
    /* for (n=0; n<(waves+2); n++)
    {
    matrixa[3*n+2][n + 2] = sumspots[3*n];
    matrixa[3*n+3][n + 2] = sumspots[3*n+1];
    matrixa[3*n+4][n + 2] = sumspots[3*n+2];
    }

    ludcmp(matrixa, waves + 4, indx, d);
    lubksb(matrixa, waves + 4, indx, spect);

    */



    for (n=0; n<waves + 4; n++)
    printf("%f\n", spect[n]);
    free(sumspots);
    for (n = 0; n < waves; n++)
    free(spotbuf[n]);
    free(matrixa);
    for (n = 0; n < waves + 4; n++)
    free(matrixa[n]);
    free(fitsfile);
    free(ld);
    free(d);
    free(indx);
    printf("done\n ");
    //open spot map files


    //open -ds data file
    //for pixel put data into arrays
    //do least square fits


    return 0;
    }
     

    Attached Files:

  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    matrixa needs room for waves+4 float *s, not just 1. There may be other problems, but start there.

    -Lee
     
  6. macrumors 65816

    Flynnstone

    Joined:
    Feb 25, 2003
    Location:
    Cold beer land
    #6
    Couple things:

    Where is "waves" defined? In p1640.h?
    I suspect a "#define waves 23" somewhere. 23 is just a number I threw in.
    Typically we use "WAVES" instead of "waves" in this context. It shows up better.

    The problem I see is:
    Code:
    for (n=0; n < (waves + 4); n ++)
    matrixa[n] = (float * )calloc((waves+ 4), sizeof(float));
    
    "n" can equal waves to waves+3. But matrixa is a pointer to a pointer of float.
    oops. Thats a potential problem.

    Problem is "matrixa" is a ... as above, so I suspect "matrixa[n]" is going to walk over memory.
     
  7. macrumors 6502

    Joined:
    Apr 24, 2008
  8. macrumors 6502

    Joined:
    Apr 24, 2008
    #8
    By the way, that's [ code]code goes here[ /code] (without the spaces in the square brackets - added because they'd otherwise be parsed :) )
     
  9. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    [CODE][/CODE]
    That's what the noparse tags are for! Note, someone else told me about these. i don't just have an intrinsic knowledge of these things.

    -Lee
     
  10. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #10
    Thanks guys.

    It was the calloc of only one float *
     

Share This Page