[Unit 4] Memory Layout

binh12A3
4 min readOct 1, 2023

How C program structure the memory area ? Memory Leak ? Stack Overflow ?

1. Overview

1.1 Text Segment (Code Segment)

  • Contains executable instructions

e.g : we have c = a + b then this command(instruction) is stored in text while the address of a,b,c variables are strored elsewhere.

  • Sharable : if we run many instances of a program, then we could share and use the same code segment
  • Read-only : contains constant variable

1.2 Initialized Data Segment (Data Segment — DS)

  • Contains global variable, static variable if they are initialized ≠ 0 by the programmer.
  • Example : global, x will be stored in DS
int global = 100;
int foo()
{
static int x = 10;
return 0;
}

1.3 Uninitialized Data Segment (BSS)

  • Contains global variable, static variable if they are uninitialized or initialized = 0 by the programmer.
  • Example : global, x will be stored in BSS
int global;
int foo()
{
static int x = 0;
return 0;
}

1.4 Heap (Dynamic Memory Allocation)

  • Contains the dynamic allocation by malloc(), calloc(), realloc(), free()
  • Contains the dynamic allocation by new, delete
  • This segment grows upward which means it’ll be increased every time we ask for a memory allocation.
  • After using, we need to release the allocated memory by free() or delete, then the released memory could be re-allocate later if needed.
  • Every time, we free the memory the heap will be decreased, if we ask for the allocation but forget to realease, then the heap will grows upward constantly.
  • Forget to deallocate memory in heap cause “Memory Leak

1.5 Stack (Automatic Variable Storage)

  • LIFO structure.
  • Run out of stack memory is called “Stack Overflow”
  • Contains function frame, every a function is call then it’s function frame will be pushed to stack. When a function returns, then it’ll be poped out from stack

2. How a function frame on stack look like ?

  • A function frame has 4 main sections : Function parameter, Return address, Saved previous frame pointer (EBP), Local Variable
  • EBP base pointer points to the entry-point of a func frame
  • ESP stack pointer points to the end-point of stack
  • EIP instruction pointer

Example

  • When we call main(), the function frame of main() will be pushed to stack
  • When we call foo(), the function frame of foo() will be pushed to stack, with the Return address is the address of p which is a local variable of main(). Because the function frame stores this Return address so after foo() is called, the compiler knows output of foo() will be written into which memory.
  • Saved previous frame pointer (EBP) points to the entry-point of foo() which also means the end point of main().
  • Because the addresses of variables in function frame on stack are stored with offset so the compiler need to know where is the base pointer in order to calculate the exact address of each variables on stack
  • Beside that, we also have ESP stack pointer points to end-point of stack

3. Dynamic memory allocation and “Memory leak”

void* malloc(size_t size);
void free(void *ptr);
  • When the program ends, the OS will automatically free all the allocated memory of that program.
  • The memory leak exists over the life cycle of the process, even when we cause a memory leak for several hundred of MB, but the program just run for a few minutes, hours,… then it’s not a matter. But it’ll become major when the program runs for the long term (year, month).
  • Especially in the long-run system like embedded, when we boot the system, the program will be started and ended only when we shutdown the system, so we need to pay attention for the memory leak.

How to detect the memory leak ?

There are techniques to debug the memory leak like :

  • Execute 100, 200 functions in the program, then check the memory before and after run to see if it gradually increases, if it is increasing continuously without decreasing, then probably we have a memory leak.
  • Check how many regions are allocated with how much memory e.g there are 1000 memory regions are allocated with the same 10 bytes, then probably there is a memory leak in one malloc 10 bytes area of those.

Note

The program’s memory area on RAM is discrete and not seamless (virtual memory mechanism)

--

--