LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

3D Graph of dynamic 2D array

Solved!
Go to solution

Hi,

Im in the process of adding an ActiveX 3D graph to an application I use.  I never know the number of data points I will have to graph ahead of time so I have been creating a 2D array with :

 

 

double **Data; Data = malloc(size_x * sizeof(double *)); for (i = 0; i < size_x; i++) { Data[i] = malloc(size_y * sizeof(double)); }

 my problem comes when I try to use CA_VariantSet2DArray, I always get the Array arguement too small runtime error. My best guess is that the space allocated in the array is not contiguous, but Im not sure.  I have not been able to resolve the issue.  Can anyone help me convert my 2D array to a Variant either the way I have it or suggest a better way to declare a dynamic 2D array that will work with CA_VariantSet2DArray?

 

 

0 Kudos
Message 1 of 4
(3,893 Views)
Solution
Accepted by topic author Hunter3

Hunter,

 

Your hunch is correct.  You do have to have a contiguous block of memory to pass to this function.  Passing the size_x and size_y allows the function to make your contiguous block into a 2d array.  Declare your array as such:

 

Data = malloc(size_x * size_y * sizeof(double));

 

Then you can pass this to the function and hvae it create the variant as expected.  However, when you directly access Data, note that you can't use the Data[2][3] shorthand anymore, and you'll have to do some pointer arithmetic to access elements of the array.

Eric B.
National Instruments
Message 2 of 4
(3,863 Views)
Yep, that did it.  A bit of pointer arithmetic and my graphs are working, thanks for the help.
0 Kudos
Message 3 of 4
(3,839 Views)

I have been experiencing the same problem. Even though the topic has been solved a while ago, I was wondering if somebody could post a sample code that would solve this problem. My background is in mechanical engineering, so I only use C as a tool and my knowledge seems to be limited in this area. I would also appreciate a few references where I could learn more about this topic.

 

Thank you in advance.

 

Regards,

 

Salim

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