LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Getting a C variable length array

Hello,

 

I'm working on a DLL library. One of the functions should return to LabView a lot of data.

By the moment I'm doing something like this (using 'by reference' parameters):

 

int getDataFromC(int* data1, char* data2, double* data3); 

 

The function returns an error code, which is used to know everything goes fine. The memory of each parameter is allocated by LabView when it calls the library function. In C, the function doesn't allocate memory, it just copy the information to the space allocated by LabView (*data1 = xxx;). Then LabView uses it by reading each parameter. It works fine.

 

The problem is that part of the data that comes from the DLL are arrays. These arrays are generally variable length arrays. Each array always comes with other value, which describes the length of the array. So the returned array can change its length.

 

By the moment I'm assuming LabView allocates enough memory to let the C code writes all the array data. But it's not a good practice...
For example:

 

int getArrayFromC(int* len, int array[]);

 

The C code writes the length value to the '*len' variable to inform LabView the size of the array, and writes the N ints to the array (assuming there is enough space allocated by LabView). 

 

What's the array's length that LabView uses in this case?

 

 

On the other hand, I'd prefer to use something like:

 

int getArrayFromC(int* len, int* array[]);

 

In this case I can control the length of the array. It is a array-by-reference situation. I could change the array used by LabView.

 

What do you recommend? I just need to get a variable length array.

 

Thanks!

 

Mauricio

0 Kudos
Message 1 of 2
(2,555 Views)

LabVIEW allocates as much memory for an array as is needed.  They are always variable length, as LabVIEW dynamically resizes the array if you add to it.  However, when you call a DLL, the DLL cannot tell LabVIEW to increase the size of the array as it builds it.  So, the array needs to be large enough for the DLL to fill it up, or you could cause memory corruption/crashing.

 

Depending on how your DLL has been written, you may be able to use the DLL to help you.  A lot of times, you specify the length as an input with the array, and the DLL checks to see if you have enough memory allocated.  If not, then the call generates an error, and the output of the len input is how big an array is needed.  So, you make a call with a size that typically is going to be large enough, and check for the error.  If you get the error, then you resize your LabVIEW array, and call the function again to get all the data.  Another option is to always call it with 0 elements, knowing you will get an error, then allocate the array.

 

If the DLL has not been written to verify you have specified enough memory, then you need to allocate an array you believe is big enough.

0 Kudos
Message 2 of 2
(2,536 Views)