In many embedded systems, they have RAM and some type of Read Only Memory, often times called Flash (it can be programmed multiple times without being removed from the Printed Circuit Board).
Simple embedded applications place the executable and read-only data sections in Flash and execute out of Flash. The read/write variables are placed into RAM. Let us consider this model for your example code fragment:
char * c = "Hello World!";
In the above statement, the variable c
lives in RAM because the default setting for variables is read & write access. If you specified that the variable was constant, it would live in ROM {Actually, it would represent a location in ROM.}: enter code here
char * const c = "Hello World!"; // A constant pointer that lives in ROM.
The compiler treats the literal text "Hello World!"
a little more complex. The actual text lives in ROM, either in the executable area or in a data area; depends on the translator. Many compilers will allocate memory in RAM and copy the literal into RAM and make the variable c
point to the copy in RAM. This is because the literal was not specified as constant.
To avoid the copying of the literal into RAM, declare the variable pointing to constant data:
const char * c = "Hello World"; // A pointer to constant data.
The definition above still allows the pointer to point to different things during execution. If you want to refer to one instance of a text literal throughout the program, declare a constant pointer to constant data:
const * char * const c = "Hello World!"; // A constant pointer to constant data
This technique allows executables to load into RAM (for faster execution) and still access read-only data from ROM (which frees up SRAM for true read/write variables).
On most PCs, everything lives either on Non-volatile memory (hard drive, BIOS, etc) or in RAM. Common method is to load programs from ROM (includes hard drive) and execute in RAM. When loading the executable into RAM, the OS usually loads the Read-Only data into RAM also. The read-only data may be guarded by the OS so an exception is generated when the application writes to this area.