Search code examples
c++memory-managementheap-memoryramflash-memory

What is stored in Code memory and Data memory


Can some one please explain difference between Code and Data memory. I know code is stored in Flash and Data is stored in RAM but i am confused.

#include <iostream>
using namespace std;

int main()
{
    int a =10, b=20;
    int c = a+b;
    return 0;
}

Here a,b,c are stored in data memory(RAM), but whats get stored in Code memory? Is this entire code is stored in Code memory? if yes, then does this mean we are storing a,b,c in both data and code memory.


Solution

  • In your example, many scenarios based on the optimization level of your compiler.

    Constants placed in "code memory"

    In the code below:

    int a =10, b=20;
    int c = a+b;
    return 0;
    

    The variables a and b are constants, they don't change. A compiler could optimize this and optimize them to be: int c = 10 + 20; So the values 10 and 20 can be placed into code memory, eliminating the variables a and b.

    Registers not Memory

    The compiler is allowed to assign the variables a and b to registers. Registers are within the processor, so don't take up any RAM or memory space. Registers are not part of the code space either. (This can happen because there are no statements that require the addresses of a or b).

    All code dropped

    On higher optimization settings, the compiler can delete all your code and replace with a return 0.
    The variables a and b are not changed.
    The variable c is changed but not used by any other statements.
    Your program has no effect (nothing is printed, there are no external actions like writing to hardware).
    Thus your program can be reduced down to return 0;.

    Code Memory vs. Data Memory

    In general, processor instructions are placed in a segment you will call "code memory". This may actually reside in RAM and not in Flash or ROM. For example, on a PC, your code could be loaded from the hard drive into RAM and executed in RAM. Similarly with Flash, your code could be loaded from Flash into RAM and executed in RAM.

    Constants, like numbers, can be placed into a Read-Only segment or in the Code Segment. Many processors can load constants from the Code Segment (see ARM and Intel assembly instructions). The Read-Only segment can live on a Read Only device, (ROM or Flash) or may live in RAM (or on a device like hard drive). All you can guarantee, is that the code will not write to the Read-Only segment.

    Data Memory is different. The C++ language has at least 3 areas of "data" memory (where variables live): 1) Local (a.k.a. stack), where short lifetime variables reside; 2) Dynamic memory (a.k.a. heap), allocated by using new or malloc and 3) Automatic/Global variables. These memory areas can be placed anywhere, as long as the memory has read and write capabilities. They don't need to be fast, only read & write (for example, the hard drive can be used as data memory).

    Memory organization is more complicated than having Code, Stack and Heap. In the embedded systems world, memory can be place in non-standard locations and there may be a need to have more detailed memory segments so they can be placed in different areas. For example, an embedded system may want to place the constants into Flash so that they can be changed easily (even though they may be more efficiently accessed in the Code Segment). Some code may want to be placed into the Boot Area of the processor (which is programmed by the processor manufacturer). Some embedded systems may have non-volatile memory (e.g. battery backed memory), which can behave like Read-Only memory.

    Trust Your Compiler

    Trust in your compiler to place code, data and variables in the most efficient areas as possible. Your compiler knows your platform and will make the best decisions for you. If you need to change your compiler's settings, you can, but you should really know what you are doing and why you need to change them. Most PC platforms load code from a hard drive (or SSD) into RAM and execute the code from RAM. Embedded systems are different and depend on the hardware devices. Code may be run from flash because the platform has minimal RAM. Some may store the code compressed in a serial access read-only device and have to decompress into RAM before executing. In these situations, the compilers are configured for these specializations. So, trust in your compiler and let it place the code and data into the correct segments and locations.