tags:

views:

45

answers:

1

Hi everyone,

I've got a program that worked until recently. The offending code is shown here:

void writeTimeInfo(fitsfile *fptr, QList<QList<double> > &in)
{
 double data[in.size() * in[0].size()];
 long naxes[2];
 int status = 0;

 naxes[1] = in.size();
 naxes[0] = in[0].size();

 for (int i=0; i<naxes[1]; i++)
 {
  for (int j=0; j<naxes[0]; j++)
  {
   data[j+i*naxes[0]] = in[i][j];
  }
 }


 fits_insert_img(fptr, DOUBLE_IMG, 2, naxes, &status);
 fits_write_key(fptr, TSTRING, "EXTNAME", (void*)"HJD", "Extension name", &status);
 fits_write_2d_dbl(fptr, 0, naxes[0], naxes[0], naxes[1], data, &status);

 if (status)
  fits_report_error(stderr, status);
}   

The specifics of the program are not important (all the fits stuff, it is used to manipulate the astronomy standard fits files), but currently the program segfaults on the line

naxis[1] = in.size();

I cannot see what's wrong - before this step I can use gdb to see in.size() and in[0].size() *the two array dimensions) and I've checked the array values. It just segfaults here.

in is a 2d QList array as you can see by the function argument list. The array is passed by reference so as to not duplicate memory. The only change I've made is running the program on a larger input set where before (the working stage): in was a 2d double array of 515*1508 elements, whereas now it is an array of 515*2480 elements. Could it be there is not enough memory on the stack?

Cheers

+1  A: 

An array of 515 * 1508 doubles is roughly 6MB -- a lot for the stack. This is probably a stackoverflow. Try setting the stack limit by using --stack option of ld to ~10 MB (if possible) and test with the value of 515 * 2480.

On Windows, using VS2010 Beta, the following crashes the stack:

int main() { double x[ 515 * 1508 ]; }

Create the array on the heap. Use a scoped_array if you can use Boost.

dirkgently
No need to use boost, Qt has `QScopedPointer` that he can use.
Adam W
@Adam W: Yes, by passing in the appropriate `QScopedPointerArrayDeleter` deleter.
dirkgently
Thanks for this answer, just changed my allocation of data to be on the heap (`double *data = new double[515*2840]`) and it works fine. Tried using the `QScopedPointerArray` but it seemed like more trouble than it's worth - just a simple new/delete[] was enough. Have tried allocating 500*500*500 elements and it works fine on the heap, though 500*500*500*100 didn't. I know i might be asking too much for a system to have that much memory but nevermind. Wish I knew more about this all!
Simon Walker
`int main() { QScopedPointer<double, QScopedPointerArrayDeleter> x(new double[ 515 * 1508 ]); }` should work.
dirkgently