LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Returning an array of pointers to array from DLL in LabVIEW

 I am stuck in setting a Call Library Function Node in LabVIEW, not sure how to set a returning array of pointers to 1D array. I have tried several different ways to do it, still not working. The C code is shown below. This function aims to generate a 2D array of data, with 3 rows and user-controlled columns. 

 

I expect the output parameter **data to be an array of pointers (with 3 elements), and then I can deference each row.  I allocated memory for it, but the output value are all 0. Should I also allocate memory for each row? And how to do that?

 

Could anyone help me.  Many thanks. 

 

generatedata(int a, int b, double **data) {

     data = (double **)malloc(3*sizeof(double *));

     for (i=0; i<2; i++)

        data[i] = (double *)malloc(a[i]*b[i]*sizeof(double));

     dx = 0;

 

    for (j = 0; j < b[0]; j++)

        for (i = 0; i < a[0]; i++)

        {

            data[0][dx] = sin(i);

            dx++;

        }

     dx = 0;

    for (j = 0; j < b[1]; j++)

        for (i = 0; i < a[1]; i++)

        {

            data[1][dx] = sin(j);

            dx++;

        }

    dx = 0;

    for (j = 0; j < b[2]; j++)

        for (i = 0; i < a[2]; i++)

        {

            data[1][dx] = sin(j);

            dx++;

        }

}

 

 

0 Kudos
Message 1 of 8
(3,454 Views)

I have attached all the relevant files, including .c and .dll files. 

please can anyone help me? many thanks.

Download All
0 Kudos
Message 2 of 8
(3,418 Views)

You forgot to attach the VI...

0 Kudos
Message 3 of 8
(3,377 Views)

As a general advice, it's not a great idea to allocate memory in DLLs. As some point you will need to deallocate, and that can only be done in the DLL, as memory allocation is not standard.

 

As long as you pass the dims to the dll, why not allocate the data in LabVIEW, and pass it to the DLL? That will be much more convenient.

 

There's no standard for 2D arrays, but a flattened 1D array would work. It won't complicate the C code much. If you do pass a LabVIEW 2D array to C, IIRC, you will simply get one pointer to data, and the data is simply flattened.

0 Kudos
Message 4 of 8
(3,375 Views)

Hi wiebe@CARYA, thank you so much for your reply. 

 

This routine is a small part of a big programme, i take it out to show what my question is. thank you for pointing out that  no memory allocation should be in the C code. I have corrected that (please see attached the test.c in my last reply ). 

 
 

In Call Library Function Node, I set the output **data as an array expecting to get a array of address value (each address value points to a row), but the returned value are all 0. 

 

 

 

0 Kudos
Message 5 of 8
(3,367 Views)

@SDLisa wrote:

Hi wiebe@CARYA, thank you so much for your reply. 

 

This routine is a small part of a big programme, i take it out to show what my question is. thank you for pointing out that  no memory allocation should be in the C code. I have corrected that (please see attached the test.c in my last reply ). 

 
 

In Call Library Function Node, I set the output **data as an array expecting to get a array of address value (each address value points to a row), but the returned value are all 0. 


The prototype says "int32_t calc(int32_t *a, int32_t *b, int32_t *data);", not "int32_t calc(int32_t *a, int32_t *b, int32_t **data);".

 

I think ** means pointer to pointer. If you tell LabVIEW to use a handle (**)  you will get a pointer to a pointer to the array. That's not the same as a pointer to an array of pointers.

 

If you want to get an array of pointers, the LabVIEW side seems OK (on a 32 bit machine). I'd still try to preallocate the values, and let the dll fill them. Setting those pointers in the array from C++ size should be as simple as setting the values of the array. But I don't think the parameter should be defined as int ** data.

0 Kudos
Message 6 of 8
(3,361 Views)

The prototype says "int32_t calc(int32_t *a, int32_t *b, int32_t *data) because I set output parameter   data    as  Array Data Pointer. I thought I could get a array of address value. Each address value points to a 1D array, and then I can derefence each 1D array. 

0 Kudos
Message 7 of 8
(3,337 Views)

@SDLisa wrote:

The prototype says "int32_t calc(int32_t *a, int32_t *b, int32_t *data) because I set output parameter   data    as  Array Data Pointer. I thought I could get a array of address value. Each address value points to a 1D array, and then I can derefence each 1D array. 


You are getting a pointer to an array.

 

Your C code is expecting int ** data which seems to me a pointer to a pointer to data. Not a pointer to an array of pointers.

0 Kudos
Message 8 of 8
(3,315 Views)