Static variables stored

cembedded

Suppose I am using a 8051. I compiled a code which has a static variable.
Where that Static Variable will be stored? In RAM? In Stack? On Heap?? In flash? Where?

Also, Correct me if I am wrong:
CONST are stored in ROM. And global variables are stored in Flash.(What if I don't have Flash?)

I am expecting answers with respect to Embedded Programming. It's not like in Code Segment or in .bss, but exact where on my board?

Best Answer

The following answer is based on my experience looking at mapfiles, if I'm wrong about sth. please correct me!

Static vars are definitely not stored on the heap, since this is only for variables allocated during run time (and static vars are allocated during compile time).

Static variables are stored in RAM, just like your global variables. The scope of a certain variable matters only to the compiler, at the machine code level nobody prevents you from reading a local variable outside of a function (as long as your controller doesn't have some fancy features to protect memory areas from being accessed).

And global variables are stored in Flash.

No. Think about it: To write a single bit of flash, you have to erase a whole block of data an then rewrite the whole thing with the modified data. And these steps do not execute in a single cycle like a simple store into RAM does. Global variables are stored in RAM, just like mentioned before. This solves also your confusion about flash-less systems.