![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
allenhuffman
Joined: 17 Jun 2019 Posts: 540 Location: Des Moines, Iowa, USA
|
[Explained] Are there no "temporary"/scope variabl |
Posted: Thu Jan 30, 2020 9:13 am |
|
|
Quick question... I'm used to doing temporary stack variables for things like for loops. Consider this, inside main:
Code: | for (int i=0; i<10; i++); |
...though probably actually doing something instead of just a loop ;-)
I noticed multiple instances of an index variable I was using showing up in the symbol table. If I did something silly like this:
Code: | for (int i=0; i<10; i++);
for (int i=0; i<10; i++);
for (int i=0; i<10; i++);
for (int i=0; i<10; i++);
for (int i=0; i<10; i++);
for (int i=0; i<10; i++);
for (int i=0; i<10; i++);
for (int i=0; i<10; i++);
for (int i=0; i<10; i++); |
...the symbol table would show it created 8 copies of i at different memory locations:
Code: | 8CC-8CD main.i
8CE-8CF main.i
8D0-8D1 main.i
8D2-8D3 main.i
8D4-8D5 main.i
8D6-8D7 main.i
8D8-8D9 main.i
8DA-8DB main.i
8DC-8DD main.i |
We also see instances where memory is reused, with multiple variables showing the same location -- which is more or less what I'd expect. In the case of this, I expect the compiler is just not sensing the scope of that variable (it only lives within those braces).
If I know I'm not ever using the variable outside of the loops, I can simply do an "int i;" at the top (no, I don't use "i" -- I like things I can search for, so at least "idx" or something more 'clean code' descriptive like "buttonNumber"... but I digress.).
It was just something I noticed and was curious about. If it's really always allocating that memory when in main (which is fine), it's an easy behavior to change if we ever get severely RAM constrained and need to start saving bytes (like I have to do regularly on the 4K Arduinos I code on).
Cheers. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
Last edited by allenhuffman on Thu Jan 30, 2020 10:08 am; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
allenhuffman
Joined: 17 Jun 2019 Posts: 540 Location: Des Moines, Iowa, USA
|
|
Posted: Thu Jan 30, 2020 9:17 am |
|
|
Actually, I wonder if this is a debugger thing, to allow watching different instances of scope variables? Alot of embedded tools I have used simply can't do that because of the nature of a scoped variable. I'm guessing this tool could, since it's able to see them all separately. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19381
|
|
Posted: Thu Jan 30, 2020 9:30 am |
|
|
Though they only exist when the code is inside the code block after the
braces, since they all exist inside the 'scope' of the external main function,
they all have to have unique memory areas in this scope. If these were inside
a subroutine, their areas could be re-used in a function 'higher' up the
calling tree. Remember potentially code anywhere in the main can jump
to any other code inside the main. |
|
![](templates/subSilver/images/spacer.gif) |
allenhuffman
Joined: 17 Jun 2019 Posts: 540 Location: Des Moines, Iowa, USA
|
|
Posted: Thu Jan 30, 2020 9:30 am |
|
|
Ttelmah wrote: | Though they only exist when the code is inside the code block after the
braces, since they all exist inside the 'scope' of the external main function,
they all have to have unique memory areas in this scope. If these were inside
a subroutine, their areas could be re-used in a function 'higher' up the
calling tree. Remember potentially code anywhere in the main can jump
to any other code inside the main. |
So we can blame this on the existence of "goto" ![Smile](images/smiles/icon_smile.gif) _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|