PDA

View Full Version : memory problem in C




farmerdoug
Oct 6, 2009, 03:07 PM
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



lee1210
Oct 6, 2009, 04:16 PM
Where is matrixa allocated?

-Lee

chown33
Oct 6, 2009, 05:05 PM
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.

farmerdoug
Oct 7, 2009, 05:44 AM
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;
}

lee1210
Oct 7, 2009, 08:21 AM
matrixa needs room for waves+4 float *s, not just 1. There may be other problems, but start there.

-Lee

Flynnstone
Oct 7, 2009, 08:25 AM
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:
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.

Sander
Oct 7, 2009, 09:32 AM
Also, what's with the (void) stderr thing?

Sander
Oct 7, 2009, 09:34 AM
Sorry I don't remember the editing commands for displaying code.

By the way, that's [ code]code goes here[ /code] (without the spaces in the square brackets - added because they'd otherwise be parsed :) )

lee1210
Oct 7, 2009, 09:55 AM
By the way, that's [ code]code goes here[ /code] (without the spaces in the square brackets - added because they'd otherwise be parsed :) )


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

farmerdoug
Oct 8, 2009, 06:27 AM
It was the calloc of only one float *