understanding local variable memory use and braces

I think I knew this years ago.

Well, at least how it worked on a PDP-11 ::slight_smile:

If I enclose a block of code in braces, I know that the scope of variables declared there is confined to the braces.

e.g.,

void someFucntion(){

  some code

  {
    byte bracesVar=7;
    doSomeThing(bracesVar);
  }
}

Are the variables declared in such blocks only allocated upon hitting the block, or are they allocated when the underlying function begins?

An example is a section which takes commands from bluetooth, but would rarely get a command to set the time, or to manually turn on a sprinkler.

Are the variables declared in such blocks only allocated upon hitting the block

Yes.

An example is a section which takes commands from bluetooth, but would rarely get a command to set the time, or to manually turn on a sprinkler.

I can't imagine why local variables would be a concern when turning a pin on or off (if the pin controls a solenoid that opens a valve) or when setting the position of a servo that manually operates a valve.

Local variables would be useful to save the values parsed from a command to set the time/date, but they are very small in comparison to the space used to hold the command.

It is possible to look at the generated code. You may want to learn how to do that.

PaulS:
Yes.
I can't imagine why local variables would be a concern when turning a pin on or off (if the pin controls a solenoid that opens a valve) or when setting the position of a servo that manually operates a valve.

I have, for example, a function which parses received wireless (currently bluetooth) messages. There is a switch which parses by the first character, leaving up to 15.

Some of these need processing. For example, 'r' takes two more ascii characters, the first to indicate which sprinkler, and the second for how long. so "r13" would result in turning on bank 1 for 2^3 seconds. (This isn't normal operation, it's there so I can be out there to work on them and tap my phone for a quick burst, instead of hollering to someone on the porch.). The global variables for currentSprinkler and sprinklerShutoffTime are then set, and the sprinkler turned on.

If I have a dozen different commands on the branches of the switch, each of which has one or two variable to calculate, allocating all of them every time the function is called.

[/quote]
Local variables would be useful to save the values parsed from a command to set the time/date, but they are very small in comparison to the space used to hold the command.
[/quote]

But those come out of different pools :). While I have comfortable RAM left, I have ROM to burn . . .

vaj4088:
It is possible to look at the generated code. You may want to learn how to do that.

shudder

Someday, maybe, but I have so many projects with higher priority . . .