Consider the following piece of code running on a 16-bit microcontroller (16 bit address bus width). My questions all concern what happens before main() is called.
unsigned short foo = 156;
const unsigned short bar = 12;
int main()
{
return 0;
}
Is my understanding correct that:
- The address locations in the heap for foo and bar are known at compile time and are stored in the data segment of the binary file
- The sizes (memory consumption) of foo and bar are known at compile time and are stored in the data segment of the binary file
- The values of foo and bar are stored in the data segment of the binary file, separate from the size declaration of foo and bar
- Before main() is called, the memory for foo and bar is allocated and filled with their respective values on the heap, always at the same offsets
- In the case of an embedded system (microcontroller), memory space for "bar" is not allocated in RAM, but is directly read from ROM when required, since it was declared const and cannot change its value.
- In the case of an embedded system (microcontroller), memory space for "foo" is allocated in RAM and its value is copied from ROM into RAM before main() is called, from which the value can be read and written to when required later on.
My two main questions are:
- Without starting this program, how much "disk space" do "foo" and "bar" actually consume? There obviously has to be information on what they are, what value they have, and where they will be stored in memory.
- When hard-coding values (such as "foo=2"), how is that number "2" stored?
Thanks