LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to initialize typedef array without loosing the typedef

Solved!
Go to solution

Hi,

 

I'm running into problems with initializing a typedef that containts an array of clusters. The attached example updates the Clusters and outputs the new array.

It works fine but I would rather preallocate the memory for the whole array.

 

I came up with indexing the typedef and using the element for the initialize array vi but with this approach I loose the typedef.

 

Is there a good way to initialize the typef?

 

Thanks,

Lukas

Download All
0 Kudos
Message 1 of 21
(6,231 Views)

Why not just define the cluster's type?  You don't gain anything by type-def-ing the array.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 2 of 21
(6,215 Views)

Hi Jim,

 

Thanks for your fast reply, I forgot to mention that I'm working on some sub VIs in a bigger application that already exists. I will check how much work it would be to change the definitions and everything that relys on them.

 

Any solution that works with the typedef array would be welcome!

 

Lukas

 

0 Kudos
Message 3 of 21
(6,193 Views)

Rule of thumb: Never type define an array.  Type define the data type of the array.  Why?  So that you can do whatever it is you need to on a single element of the array and keep your type defs in order.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 21
(6,190 Views)

Yes, as Jim already said, you should typedef the array element, not the array.

 

In any case, your code is overly complicated and potentially nullifies certain compiler optimizations. Unconditionally building an array in a loop is best done using autoindexing, because it is much easier to determine the final array size, esepcially if the code is a bit more complicated. Now the output array is guaranteed to be allocated at the correct size at the start of the loop. (case A in picture). (Yes, the compiler will probably recognize your code construct and create the same output. Still, it helps to keep the diagram simple. Fewer places where bugs can hide ;))

 

If you want to work with a pre-allocated array (e.g. it it already exists but some elements need to be updated), you would use something similar to case (B). OF course you would replace "initialize array" with the wire from the existing array.

 

 

0 Kudos
Message 5 of 21
(6,186 Views)

Hi,

 

thanks for the input! I see that this could be achieved a lot easier in my example, but I have to use the given application for now.

 

I am looking for a solution like the one mentioned in (B) but I am missing the preallocated array, because I am transferring data from one larger tyepef to a smaller one (only certain elements). So my inputs are a typedef array narrowed down to the elements I need, the size of this array and a constant of the typedef where the data should go. I thought it would be easiest to somehow initialize the smaller typedef array and then pass the cluster elements I need over in a for loop. The result is then passed to a shared variable of the smaller typedef (Thats why I dont want to loose the typedef).

 

If I go for something like mentioned in (A), would it be good practice to typecast the resulting array into the typedef format?

 

Still, I will look into changing the typedefs as it makes sense what you all say with the arrays. For now I would need a simpler solution though.

 

Lukas

0 Kudos
Message 6 of 21
(6,178 Views)

@LukasWörle wrote:

If I go for something like mentioned in (A), would it be good practice to typecast the resulting array into the typedef format?

 


No, probably not. The elements of the output array are still typedefd, because that's how we define them at the bundle node.


@LukasWörle wrote:

I am looking for a solution like the one mentioned in (B) but I am missing the preallocated array, because I am transferring data from one larger tyepef to a smaller one (only certain elements). So my inputs are a typedef array narrowed down to the elements I need, the size of this array and a constant of the typedef where the data should go. I thought it would be easiest to somehow initialize the smaller typedef array and then pass the cluster elements I need over in a for loop. The result is then passed to a shared variable of the smaller typedef (Thats why I dont want to loose the typedef).

 


If I understand you right, you probably want to do A, autoindexing on an array of indices of the elements you want to retain and using index array inside the loop, autoindexing at the output tunnel.

 

Otherwise please provide more detail, maybe with some example data and expected result.

 

0 Kudos
Message 7 of 21
(6,156 Views)

I just made a new example with a bit more information.

 

As you can see I have a config cluster that is mainly used to setup daqmx channels and an output cluster that should hold some of the config information and the measurement values. Both of them are typedefed and unfortunately already contain the array in the typedef.

 

A: This code works but I do not really like how the array has to be indexed and then put into another array plus the typedef gets lost on the way.

TD_Array_wo_typedef.PNG

 

B: Looks better to me, but now I can not initialize the array before passing it to the loop. While posting this I just came up with a new question: Is the shift register already preallocated because of the for loop?

TD_Array_wo_allocation.PNG

 

I also attached both VIs plus the typedefs. Btw I couldn't find a guideline to which LV version to use for examples, is 10 alright? (I'm using 14)

 

Thanks for your help,

Lukas

0 Kudos
Message 8 of 21
(6,113 Views)

Again, you are doing this way too convoluted.

 

You are still not typedefing the array element instead of the array, so change that!

 

As I said, all you need is autoindexing.

 

In A,, you don't need to initialize the upper array, just get the element and don't autoindex on top. Right?

 

 

Simplfy! Here's how it could look like.

 

0 Kudos
Message 9 of 21
(6,091 Views)

>You are still not typedefing the array element instead of the array, so change that!

 

As I said that's just not possible at the moment. I would have to change an application with 100+ VIs. I will look into that as it is the best solution but that will take some time (first I have to understand the whole application).

 

>In A,, you don't need to initialize the upper array, just get the element and don't autoindex on top. Right?

 

Good point, I didn't realize that!

 

In (B) will the shift register be preallocated because of the autoindexed for loop? If yes this could be my workaround for now.

0 Kudos
Message 10 of 21
(6,062 Views)