Page 1 of 1

Value of Local Variable at start of Macro.

Posted: Thu Jan 06, 2022 8:18 am
by jay_dee
Hi,
In a user macro, I'm aware any local variable is not retained after the macro exits.
However each time a macro start, can I always guarentee the local values will start with a Value of 0? and is the bahaviour the same between FC and FAD.
or could it be some random value.

If a local value is created with a default value, is this used as the starting value?
Or should I always explicitly set the values at the start of the macro.

Tiying up the tattered edges of my knowledge!
Thanks, J.

Re: Value of Local Variable at start of Macro.

Posted: Thu Jan 06, 2022 8:51 am
by medelec35
Hi J.
If uninitialised, A local variable will be automatically initialised at 0.

Re: Value of Local Variable at start of Macro.

Posted: Thu Jan 06, 2022 9:02 am
by mnfisher
To be sure - if you need 0 then set an initial value of 0.. I don't think any value is 'guaranteed' otherwise. Local variables are usually allocated on the stack by moving the stack pointer and values will depend on what is in the memory at the time...

Re: Value of Local Variable at start of Macro.

Posted: Thu Jan 06, 2022 9:35 am
by medelec35
mnfisher has a valid point, thanks Martin.
I have tested for uninitialised local on an Uno and it is 0 every time after power-up.
Of course, this can't be guaranteed.
So it is best to initialise in practice.

Re: Value of Local Variable at start of Macro.

Posted: Thu Jan 06, 2022 9:48 am
by Steve-Matrix
I think in Flowcode simulation and deployed Flowcode App Developer projects, uninitialised numbers will be zero and uninitialised strings will be empty.

For Flowcode projects downloaded to a chip, uninitialised variables could potentially be anything.

However, I will echo what others have said: it is always good practice to initialise variables if you require or expect them to have a certain value.

Re: Value of Local Variable at start of Macro.

Posted: Thu Jan 06, 2022 11:12 am
by jay_dee
Thanks guys.
For embedded (and general good practice) I shall expicitly set/reset the value at the start of the macro, since there is no guarentee.
J.

Re: Value of Local Variable at start of Macro.

Posted: Fri Jan 07, 2022 11:17 am
by BenR
A rule of thumb with variables is always write before you read :D

I think the very strict ESP32 compiler will generate an error if it thinks you are trying to do a read before a write.