LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to allocate memory for string

Solved!
Go to solution

How to allocate memory in LabVIEW for char[]?

The array is:

char ProjectName[260].

I always get 4 byte when read from Unflatten from string, any suggestions?

TIA.

 

*************************************************
CLD,CTD
*************************************************
0 Kudos
Message 1 of 11
(2,796 Views)

There is no need to allocate the memory for the string. LabVIEW takes care of that for you. It will handle all of the memory management for you. The only time you need to worry about memory management is when you are dealing with very large data sets. For the size string you are working with there is nothing you need to do.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 2 of 11
(2,789 Views)

You don't want to provide an array for the element type to Initialize Array. Simply wire a U8 and you'll get an array of 260 U8 values, equivalent to char[260].

 

 

Mark: The char ProjectName[260] implies they're passing this to a DLL, so they need to pre-allocate the memory for the DLL.

0 Kudos
Message 3 of 11
(2,787 Views)

@smercurio_fc wrote:

You don't want to provide an array for the element type to Initialize Array. Simply wire a U8 and you'll get an array of 260 U8 values, equivalent to char[260].

 

 

Mark: The char ProjectName[260] implies they're passing this to a DLL, so they need to pre-allocate the memory for the DLL.


I didn't think you needed to preallocate strings for DLLs unless you are dealing with pointers to a memory location. It was my understanding LabVIEW still handled that for you. If you are passing the string to the DLL you need to validate it's length and assure that it fits within the expected size specified by the DLL. Granted, I haven't done a ton of stuff with DLLs but I never worried about pre-allocating strings when I have. I may be wrong so if that is the case I'm willing to learn something new.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 4 of 11
(2,779 Views)

Well, a string is a pointer, so you still need to allocate the memory. Technically, here the OP is not dealing with a string, but an array of char, which are two different things. Unless the DLL handles adding a NULL somewhere in the array you're not really going to get a string. Either way, the memory needs to be allocated. You can do it yourself in code (as shown), or you can configure it in the CLFN dialog in the parameters.

0 Kudos
Message 5 of 11
(2,766 Views)

Even after initializing array with 260 unflatten from string reads only 4 byte.

I am not using CLFN, it is the data received over UDP and then trying to parse and display in LV.

Not sure what is happening or how unflatten from string is handling the initialized string?

*************************************************
CLD,CTD
*************************************************
0 Kudos
Message 6 of 11
(2,764 Views)

Why are you trying to unflatten a string from a string?  When you flatten a string, you get the same string but with the length prepended.  If your string isn't in that format then the length information isn't there, so you won't get the correct amount of data back.  If you're reading from UDP, LabVIEW will properly set the length based on the amount of data that you read - but then you just have a LabVIEW string, not a flattened string.  Please try to clarify what you're trying to do.

0 Kudos
Message 7 of 11
(2,759 Views)

I receive the data from UDP which is in structure and that is the reason of using unflatten from string. Here is the post that has the original VI and suggestion to use Unflatten from string.

Now I am trying to parse out each strucutre member individually after which I remove the remaining bytes form data to use the rest of the data to parse the next structure member.

Based on what you mention for string and unflatten from string function I suppose I can use string subset.

TIA

*************************************************
CLD,CTD
*************************************************
0 Kudos
Message 8 of 11
(2,749 Views)

I'm still not completely clear on what you're trying to do, but if you're receiving a data structure that contains embedded fixed-size arrays (such as character arrays), you need to use a corresponding fixed-size structure in LabVIEW - a cluster.  For your string, you would need a cluster containing 260 U8 elements (any combination of sub-clusters is OK too, so you could have a cluster containing 5 clusters of 52 U8s).  You can embed that cluster into your larger structure, then typecast the string to that datatype and use unbundle to get the data out.  You may need to use cluster to array followed by array of bytes to string, or typecast the string/cluster to a string (for example, if you do have a cluster of 5 clusterts of 52 U8s, you can typecast that directly to a string).  If you can supply a better example of what you are trying to accomplish, it will make it easier to help you.

0 Kudos
Message 9 of 11
(2,745 Views)

You know, I really irritates me when people cross-post to LAVA and don't say so. On the cross-post the OP claims to have solved the problem. Although the "solution" makes no sense.

0 Kudos
Message 10 of 11
(2,701 Views)