From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with array pointer datatype in C++ dlls to labview

Hi everyone,

We are trying to import C++ dll to Labview. As labview can't handle complex data types of C++ we have written a wrapper so that labview supports it. Now we are facing a problem while creating library .lvllb for this dll. 

1. We have few functions which would return an array of values out of dll as output.

2. When we create a library, labview is not able to detect it as an array, so we made manual change to Array as the data type. In the configure window of Call library node-> parameter properties.

 

We are using Labview 2013 version. for importing the dlls

While importingWhile importingAfter manually selecting datatype to arrayAfter manually selecting datatype to array

 

Because of this we are seeing that labview crashes randomly at different places of execution.

0 Kudos
Message 1 of 3
(2,719 Views)

I think the problem you're having here is related to your "Minimum Size" value.

 

The comment suggests that you should have a pointer to an array containing two doubles, eg

 

 

double a[2];
a[0] = 0.241;
a[1] = 251.3211;
long result = calcDarkSignal_DT(&a[0], ...);

 

However, your minimum size is set to 1, so LabVIEW will allocate either a) the minimum size (1) or b) the size passed in, if larger.

 

  • Minimum size—Indicates the minimum size of a 1D array and allocates the correct amount of memory. You can enter a numeric value, or, if you configure an integer parameter in the Parameters list, you can select the parameter from the pull-down menu. The default is none. This option is available only for array data pointers. If you pass in an array that is smaller than the minimum size, LabVIEW enlarges the size of the array to the minimum. If you pass in an array that is bigger than the minimum, the array retains the larger size.

 

Since you don't wire an input array, your output will probably be outside the bounds of the memory. AFAIR, this is Undefined Behaviour. 

 

Try increasing the minimum size to 2, if that is the returned size, or alternatively wire an input array with at least 2 elements.

 

Edit: I checked quickly and the C++ standard (or at least, the draft I found via Google) specifies as follows (regarding pointer arithmetic):

 

Spoiler

Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.

 

Here we note that it's not undefined behaviour to have arithmetic involving the last+1 element (although I don't believe it is dereferenceable). As I understand it, that means probably your code won't tell you there's an error when you have an off-by-one error, but when you try and get the value, it will produce rubbish/memory corruption as you write in someone else's memory, etc, etc...

 


GCentral
0 Kudos
Message 2 of 3
(2,698 Views)

As a follow-up, the external code guide has the following to say on using arrays as output data from external code...

 

To return an array of data, you should allocate an array of sufficient size in LabVIEW, pass the array to your function, and have this array act as the buffer. If the data takes less space, you can return the correct size as a separate parameter and then, on the calling diagram, use array subset to extract the valid data.

 

See http://www.ni.com/pdf/manuals/370109b.pdf for more.


GCentral
0 Kudos
Message 3 of 3
(2,691 Views)