10-12-2015 12:29 PM
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
Solved! Go to Solution.
10-12-2015 12:42 PM
10-12-2015 01:10 PM
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
10-12-2015 01:13 PM
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.
10-12-2015 01:18 PM - edited 10-12-2015 01:18 PM
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.
10-12-2015 01:32 PM
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
10-12-2015 01:45 PM
@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.
10-13-2015 02:58 AM
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.
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?
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
10-13-2015 07:37 AM - edited 10-13-2015 07:38 AM
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.
10-14-2015 02:36 AM
>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.