LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

dynamic allocation and GenLSFit

I'm using LabWindow/CVI V7.1 and am having trouble using GenLSFit with a dynamically allocated 2d array (when allocated statically everything works fine)

 

Some code snippets:

 

*******************************

 

double **H;

 

H = (double **)malloc(numpoints*sizeof(double *));
H[0] = (double *)malloc((order+2)*numpoints*sizeof(double));
for (j=1; j<numpoints; j++)
{
     H[j] = H[j-1]+(order+2);
}

 

//uninimportant stuff is here

 

for (k=0; k<numpoints; k++)
{
      H[k][0] = -1/(trdata[k]*trdata[k]);
      H[k][1] = 1;
      H[k][2] = trdata[k];
      for (l=3; l < order+2; l++)
           H[k][l] = power(trdata[k], l-1);
}

 

GenLSFit(H, numpoints-j, order+2, pdata, stdDev, 4, fitvals, params, covar, &MSE);

 

******************************************************************

 

I get an error that my array H is (order+2)*2 times too small, which suggests that GenLSFit only knows about the initial allocation I did and only seems to consider H as an array of pointers and not a 2d array.  Again, if I allocate memory statically I get the right answer.  

 

Two questions:

 

1)  Is there some other way to fix this problem?

2)  How does GenLSFit know how big my array is anyway?

 

Thanks for any help!

 

 

 

Message Edited by jayrandom on 08-19-2009 12:07 PM
0 Kudos
Message 1 of 4
(3,584 Views)

jayrandom wrote:

...GenLSFit only knows about the initial allocation I did and only seems to consider H as an array of pointers and not a 2d array...


 

Because you used two separate malloc() operations, you ended up with two memory regions, not one. They may be contiguous, but this is not guaranteed and so the system cannot assume that H points to one big region. You need to re-arrange your malloc() so that a single region, big enough for the whole array, is reserved. (Personally, I always use calloc(). A bit easier, especially in circumstances like this one, with the added bonus that the memory space is zeroed to begin with.)

 

JR

Message 2 of 4
(3,562 Views)

Thanks for the reply. 

I do actually declare one region that is a single block of memory the right size:

 

H[0] = (double *)malloc(numpoints*(order+2)*sizeof(double));

 

and it turns out the solution is as simple as calling the function with the very minor modification:

 

GenLSFit(H[0], numpoints-j, order+2, pdata, stdDev, 4, fitvals, params, covar, &MSE);

 

causes the code to suddenly start working without any warnings or errors.  From what I was able to read this shouldn't necessarily work with a general compiler (i.e. it's not standard ANSI C) but it seems to work in LaWindows/CVI 7.1, at least with these functions. 

 

I still wonder how GenLSFit knows how big my array is, but now that my code works I guess it's just academic.

 

0 Kudos
Message 3 of 4
(3,534 Views)

If you are using the CVI Debug build, a lot more information is generated by the system to aid the debugging process. Some of this extra information is passed transparantly to special debug versions of the library routines, and includes facts about the size of the arrays. This is all done behind the scenes, so the programmer is not aware of it. (This is one reason why debug builds are bigger and slower than release ones.)

 

JR

0 Kudos
Message 4 of 4
(3,512 Views)