01-25-2016 11:54 AM
hi,
which methode is best to use for memory optimization and avoide memory leakage for this program array 1 initialization or array 2 .
01-25-2016 12:31 PM - edited 01-25-2016 12:32 PM
Using the Initialize Array function is best when you know what size your array is going to need to be. But you haven't done that, you just initialized an empty array, so there is no memory optimization here. Both arrays are pretty much exactly the same.
In order to prevent memory leakage, you would need to set an array size limitation so that the user couldn't infinitely insert elements in to the array.
Cheers
--------, Unofficial Forum Rules and Guidelines ,--------
'--- >The shortest distance between two nodes is a straight wire> ---'
01-25-2016 01:26 PM - edited 01-25-2016 01:56 PM
Your problem is not with the initialization, but with the constant array resizing. There are no memory leaks, put potential for memory fragmentation.
Is this just some test code to be used later in an automatic procedure that shrinks and grows gigantic arrays or do the sizes remain small?
If you are asking about the "INT" even case, there is absolutely no code needed, because all output tunnels can be set to "use default if unwired", giving you two empty arrays and a scalar that is zero.
Since you are always appending at the end, you should use "built array" inside the INC event.
You also don't need the two value property nodes. Just go to the "VI properties...execution" and check "clear indicators when called". However, since this seems to be an interactive VI, the indicators would belong to the left of the event structure inside the loop. This way they clear automatically when called and correctly update whenever vales are added or removed.
Also note that latch action buttons belong inside their respective event cases so they correctly reset once the code has read their value. If you leave them outside the loop, they only get read once at the start of the program and will not properly reset when pressed.
To delete the last element, leave the index and size unwired for "delete from array". No need to keep track of the index.
There is also a logical problem that you allow negative values in the index (e.g. if you would press DEC more times than INC). This needs to be handled.
Simplify!
01-25-2016 01:41 PM - edited 01-25-2016 01:42 PM