Wednesday, 24 June 2009

Kinds Of Memory

There are roughly 5 kinds of memory:

1) Statically allocated
2) Automatically allocated (stack/registers)
3) Dynamically allocated (heap)
4) Read-only memory
5) Program memory


1) All global variables and all variables declared with the keyword static. ANSI C/C++ states that they will be initialized to zero before the program starts. These are the only variables that have a known value at program start, without the programmer explicitly initializing them. RAM.

2) All local variables. (Local constants might end up here too, though that depends on the type of system.) RAM.

3) Memory explicity allocated by the programmer with malloc or new. RAM.

4) All global constants. Strings end up here since they are a special case treated as constant character arrays. On a PC: RAM. Non-PC systems: non-volatile read-only memory.

6) The code itself, as well as constant numeric values written in the code (#defines etc). On a PC: RAM. Non-PC systems: non-volatile read-only memory.