LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

dynamic array memory allocation

I'm not very familiar with how labview does memory allocation, so here's the question.

I have a while loop that dynamically builds a 2D array.  I know the size of the 2D array that is going to be generated a priori.  Based on my knowledge of text languages (java specifically), it would be best to declare a new 2D array of the correct dimensions initialized to zeros outside of the while loop, and have the while loop then fill that array as it iterates.

but in labview, I'm not sure -- I could imagine just declaring a constand 1D array  and then dynamically building the 2D array inside the while loop. ---

I assume the first solution is correct, but, as I said, I'm not very familiar w/ LV yet, so please let me know your thoughts!

thanks
-z  
0 Kudos
Message 1 of 5
(4,604 Views)
Z -

You're saying "LabView" but do you mean "Lab Windows" ? - you've posted this in the Lab Windows discussion forum.

Pre-allocating the array off the heap (top level C value or a static value) means that you don't have to spend time doing memory allocation within your loop and yes it will be faster.  In C, you're guaranteed that static storage class variables are initialized to zero (numeric types).  But then they're static extent so they retain whatever you last wrote into them.  Using a local (stack) variable for your array means that it won't be initialized automatically - you have to do it explicitly.  And the array gets allocated on the stack every time your thread of execution enters the block scope for the local array.

Many things in computer science trade space for time.  With modern PC's with gigabytes of memory and gigahertz core speeds, we often have plenty of both.   Do you really have to be that concerned about execution time?

Dynamically allocating heap storage in C (malloc and free) can be error prone if you're not careful.  NI has a ListType in the programmer's toolbox that's better than malloc's / free's for many things.  Lists will dynamically allocate memory as needed, in chunks, and you don't have to do a lot of pointer manipulation.

Menchar
0 Kudos
Message 2 of 5
(4,596 Views)
I'm sorry, I meant to post this in the LabView forum.  That is my mistake.

and to answer your question, I'm not very concerned with it,  but I'm just curious to learn more about how LV works.  I'm looking to transfer some of my knowledge from text languages to the data-flow model of LV.

based on your response, I assume then that I was correct, and that the trade off in LV is the same as in other languages...

thanks
-Z
0 Kudos
Message 3 of 5
(4,594 Views)
Z -

Well, I was speaking about Lab Windows / CVI, not LV.  I have no specific knowledge about Lab View - I don't use it.

But it's entirely possible it's the same answer, seeing as how LV at some point must in fact become procedural code and execute.

Menchar
0 Kudos
Message 4 of 5
(4,583 Views)

zskillz,

You are correct.  LabVIEW is just like C++/CVI in this respect.  If you create the full array before your loop and then use your loop to fill in the values, this will run faster than if you dynamically build the arrays inside your loop using the build array function.  This is usually only important if you are working with very large arrays or you are calling this routine very quickly and the timing has to be as fast as possible.  Most people use the "Initialize Array" function to create the pre-allocated array before their loop and use the "Replace Array Subset" function to insert values into this array.

LabVIEW does try to be smart though and will do this for you in some circumstances.  One example of this is if you create a for loop and wire a constant to the iteration count terminal (like 10) and then wire a constant inside the for loop to the edge and enable auto indexing.  In this instance, LabVIEW knows that your loop is going to execute exactly 10 times and you will add one element to your array for each iteration.  LabVIEW is smart enough to pre-allocate an array of size 10 before your loop runs.

Justin D
Applications Engineer
National Instruments

0 Kudos
Message 5 of 5
(4,555 Views)