0

I have a 2D array that I'm dynamically allocating at runtime, like so

accData = calloc(nbox, sizeof(double *));
for(bb = 0; bb < nbox; bb++)
   accData[bb] = calloc(usedTime * usedChan, sizeof(double *));

and I want to only pass the second dimension to my function. This array represents data defined in several different "boxes", and for each box, I want to pass the relevant information to the function, process it and store it in the same array. Currently this is how I'm doing it -

for(bb = 0; bb < nbox; bb++) 
   fftAndsubtract(accData[bb], ntime, nchan, nsigma, bb);

where fftAndSubtract performs an FFT (fast fourier transform) and a few other operations. The function definition is like so:

int fftAndsubtract(double accData[], ntime, nchan, nsigma, bb);

but accData doesn't seem to hold the modified values that fftAndSubtract produces. I've verified this, because I'm printing the outputs of the operations done in the function itself. The compiler isn't complaining, so I didn't think this was wrong. Is there a better way to do this?

Question: Is there a way I can pass accData[bb] to the function so that the output of the operations done by the function are stored in the same array?

Kitchi
  • 1,874
  • 4
  • 28
  • 46

2 Answers2

0

I'm guessing that you're on a 32-bit machine. Then pointers are 32-bits, while double are 64 bits.

This means that your allocation of the "second dimension" in the loop is wrong, and only allocates half of the data that is needed. To solve this, change to sizeof(double) in the calloc call in the loop.

Technically you might want to change the type of the accData argument in fftAndsubtract to a pointer instead, as that's what your passing it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I'm on a 64-bit machine. I changed it to `sizeof(double)` and I also changed the function definition to `double *accData`, but the problem is still there. The problem could be in the function, I can check that. Thanks! – Kitchi Jul 09 '13 at 12:02
0

I like the way you're doing it, when I first read the question I was thinking you'd have to do some fancy indexing to hop from value to value... but I looked closer and I like the array of array idea. This is how I would implement it:

double *accData = calloc(nbox, sizeof(double *)); // Alloc array of double* pointers
for(bb = 0; bb < nbox; bb++)
   accData[bb] = calloc(usedTime * usedChan, sizeof(double)); // Alloc array of doubles

for(bb = 0; bb < nbox; bb++) 
   fftAndsubtract(accData[bb], ntime, nchan, nsigma, bb);
   // Remember accData is an array of an array of doubles
   //     accData[bb] is an array of doubles

...

int fftAndSubtract(double* accData, int nTime, int nchan, int nsigma, int bb) {
    ... do fancy fft stuff ...
    for(int i=0; i < nTime * nchan; i++) { // loop through entire array
         double result = ... do stuff with accData[i] ...
         accData[i] = result;
    }
    return someReturnValue;
}

This is how I think about how your accData is laid out in memory (this is bad though because the first row is contiguous data and the columns (after the first row) are also contiguous data, but the columns have no correlation to each other):

double* accData[0]     accData[1]     accData[2]     accData[3]     accData[4]
double  accData[0][0]  accData[1][0]  accData[2][0]  accData[3][0]  accData[4][0]
double  accData[0][1]  accData[1][1]  accData[2][1]  accData[3][1]  accData[4][1]
double  accData[0][2]  accData[1][2]  accData[2][2]  accData[3][2]  accData[4][2]
double  accData[0][3]  accData[1][3]  accData[2][3]  accData[3][3]  accData[4][3]
double  accData[0][4]  accData[1][4]  accData[2][4]  accData[3][4]  accData[4][4]
George Mitchell
  • 1,158
  • 2
  • 11
  • 24