I think this discussion is getting off-topic from the forum ESP32 IDF, but it's interesting...
In the trivial case that you provided, I don't think there is much if any difference in function, performance, or memory usage.
But in some cases you may want to wait to put variables on the stack until they are needed. This is good practice to keep the stack small.
Code: Select all
void function()
{
blah;
blah;
{
char big_buffer1[255] = {0};
int idx;
for (idx = 0; idx < PROFILE_NUM; idx++) {
blah; // i.e. do something with big_buffer1
}
} // big_buffer1 is released from the stack automatically when execution exits this section
{
char big_buffer2[255] = {0};
int idx;
for (idx = 0; idx < PROFILE_NUM; idx++) {
blah; // i.e. do something with big_buffer2
}
} // big_buffer2 is released from the stack automatically when execution exits this section
}
Notice that there is never a time that both big_buffer1 and big_buffer2 are both on the stack.
Note you could have achieved the same by making each section into a function, but for style/readability this explicit inline may be preferred.