LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Determine the size of a cluster in memory

Hi all,

is there an equivalent to the sizeof() function in C? I am trying to read/write an array of clusters to file using the binary r/w functions. When reading the file, I'd like to determine the number of entries by dividing the file size by the cluster size in bytes. I do not want to hard-code the memory size of the cluster into the VI, since the cluster is a type def that can be changed any time.

Regards,
Matthias
0 Kudos
Message 1 of 13
(3,251 Views)
You can flatten cluster to string and then check length of the string. Not very optimal (especially for large clusters), but will give you the size. Andrey.
Message 2 of 13
(3,242 Views)
Works great. Thanks!
0 Kudos
Message 3 of 13
(3,228 Views)

Hi Andre,

You mean to say like this... ?

- Partha ( CLD until Oct 2024 🙂 )
0 Kudos
Message 4 of 13
(3,221 Views)

Hi, parthabe,
Exactly!

0 Kudos
Message 5 of 13
(3,201 Views)

STOP!

You can't do this. If you change the typedef, you will not be able to load older versions of the cluster from the file, because the structure of the current structure doesn't match what's read from the file.

There are several options around this:

  1. I have read that LabVIEW classes have the ability to automatically convert older versions of the class data into newer versions. I don't have any practical experience with this, so I don't know how good this will be, but it could definitely have limitations.
  2. OpenG has the variant configuration VIs, which parse the structure of the cluster and write it to an INI file. That way, when you have the new version of the cluster, which has elements which don't appear in the file, the default value of the new elements will be used.
  3. MGI has a similar set of VIs which work faster, but only work in 8.x. Both options are free and open source.

___________________
Try to take over the world!
Message 6 of 13
(3,183 Views)

Hi tst,

Thank you for your information & explanation. Smiley Happy

First of all, I m not very clear about what is the size of the cluster in memory. Smiley Indifferent

Can you explain a bit more of it?

- Partha ( CLD until Oct 2024 🙂 )
0 Kudos
Message 7 of 13
(3,166 Views)
Hi tst,

being the original poster, I would like to say that I am fully aware of that. Thanks for the reminder!

Regards
Matthias
0 Kudos
Message 8 of 13
(3,153 Views)


parthabe wrote:

I m not very clear about what is the size of the cluster in memory. Smiley Indifferent

Can you explain a bit more of it?


One of the great things about LabVIEW is that you don't actually have to understand these to use LabVIEW. Under most circumstances, they just don't matter. That said, there are cases where it's useful to know these things, and it's good for advanced LabVIEW users at least to be aware of the basic details, even if not know every single one of them.
 
The cluster is made up of several elements. Each of those elements takes up a certain amount of memory. For example, an I32 uses 32 bits, or 4 bytes. A DBL uses 8 bytes. A string uses [len+4] bytes.
 
When you put all of them together in a cluster, the total amount of memory needed to hold the data in the cluster is the sum of those numbers. When held in memory, this data can be held in several different places and the cluster will maintain pointers to those memory locations. When you want to save to a file, you need to flatten the data, which means bringing it all into a single memory location, in this case a string.
 
Here we have an array of clusters, so if you divide the file size by that number, you will know how many elements (clusters) are in the array, because you know the size of each cluster. You should note that this will not work with variable length data (like strings) unless you guarantee that the strings in each element of the array will have the same length.
 
In any case, be aware that this is just the data of the cluster, not the information about the cluster itself (like element names and types). That information is held in a type descriptor, which is a 1D array of 16 or 32 bit numbers (depending on the LabVIEW version) which have a certain format. This information is not saved when you save the file and you are expected to provide it if you want to get the data back.
This was just a very short explanation. If you want more details, you should search for the LabVIEW data storage white paper, which goes into some more details. This covers most of those, but I believe the white paper has some more (although it's quite old and doesn't talk about newer data types).

___________________
Try to take over the world!
Message 9 of 13
(3,141 Views)


tst wrote:

I have read that LabVIEW classes have the ability to automatically convert older versions of the class data into newer versions.


OK, it turns out this is documented. Also, here's some documentation for the class representation format.

___________________
Try to take over the world!
Message 10 of 13
(3,135 Views)