# Where are static variables stored?

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?

-
 I expect static variables to be stored at the same place as global variables. After all, they are the same thing with different scope. (let me know if I'm wrong) – user606723 Jun 18 '12 at 16:08

Suppose I am using a 8051.

Then you are supposed to know about CODE, DATA, IDATA, XDATA and PDATA memory - 8051 is a multi Harvard architecture.

Where that Static Variable will be stored?

That is a good question. It will depend on the compiler settings - usually called "memory model"; But you can also explicitly say where the compiler will put it:

xdata unsigned int i;  // integer in XDATA memory


The compiler/linker should be able to crate a Map file, which will show you the addresses of your variables.

CONST are stored in ROM.

This probably depends on the compiler and needs to be checked against the Map file. I remember tagging those explicitly:

code const char fooStr[]="Foo";  // string constant in code = flash memory


And global variables are stored in Flash

Both true and false. They reside in one of the 8051's data memories, but the initial value will be loaded from Flash at startup time - unless the variable is initialised with zero.

-

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.

-
"static variables" known at compilation time are not variables, but constants. Static variables are assigned and changed at run-time. The difference with other variables is their scope and lifetime: they exist as long as the program exists. – stevenvh Jun 18 '12 at 13:06
@stevenvh: I can think of examples of static variables, which are certainly not constant. If you were right, would that not mean that the const keyword would be useless? – 0x6d64 Jun 18 '12 at 15:12
You've edited your answer, but the way it was worded it looked like being known at compile time was the basic criterion to call it static. You perfectly can define static variables whose value isn't known at compile time; you can declare them without assignment. – stevenvh Jun 18 '12 at 15:17

On processors where the code store is in the same address space as all other variables, compilers will typically place "const"-qualified global or static variables into their own link section, and linkers will typically be configured to place that section in the system's code store (flash, OTP, or whatever). This will reduce the amount of RAM required by the program, and will reduce the amount of work the startup code has to do.

On processors where the code store is in a different address space (e.g. the PIC or 8051), some compilers will use a const qualifier to signal that they should place the variables into the code store and use different instructions to access them, while others will not. Such compilers will require that only pointers with a const qualifier may be used to access const-declared variables, since without that requirement the compilers wouldn't know that special instructions had to be used to access such pointers.

On the 8051 compilers I've seen (Archimedes and Keil), there are 8051-compiler-specific keywords __data, __idata, __code, __bdata, _pdata, and_xdata available to indicate that variables should be loaded in a particular address space. By default, the names can be used with or without underscores; the non-underscore versions are more convenient, but may be disabled if, e.g. one is porting a program which uses identifiers named code or data). If a pointer is declared without applying one of those keywords to its target, the compiler will allocate three bytes: one to indicate what memory space the target is in, and two to hold a 16-bit address if one is required. Declaring a variable const without also applying a code qualifier will cause the variable to be placed in the default RAM address space, and loaded with the default value at startup; a variable declared that way may be passed to code expecting a pointer in the default address space (but will use RAM). Adding a __code (or code, if enabled) declaration will cause the variable to be placed in code space. It's usually better to use the code declaration than not, but in some cases, especially if the item in question is small, the comparative ease of accessing things in idata RAM might make up for the loss of a few bytes of that space.

-

Partial Answer: "Also, Correct me if I am wrong: CONST are stored in ROM."

Incorrect. The keyword 'const' is a directive to help the compiler. When a variable is declared as 'const' then the compiler knows it cannot change, but it is still a variable that is allocated a memory location.

If you need/want to store it in ROM, then you will need to use another keyword for the compiler that tells it which memory type or location to use. I don't think there is a standardized keyword for this. Probably because C started life on a PC with all program space and variable space being RAM.

For a PIC, you would use the keyword 'rom' in declaring the variable.

e.g. "rom int const x = 42;'

-
 The placement of a const variable is controlled by a non-standard keyword sometimes. Note that there is often a way to configure the linker to define where the const variables are stored. This, of course, varies with each tool chain. – semaj Jun 18 '12 at 15:12