網頁

2011年1月8日 星期六

Memory Allocation Sections

The concept of memory allocation section is very important for a firmware engineer.


Stack
Stores automatic variable such as local variable, function parameters and function return address. Data stored here is automatically allocated and released in execution time.

Heap
Stores data that is dynamically allocated by programmer using function such as malloc() and calloc() (in C++ we use "new" operator). Operating system might or might not release the memory after the program exit.
p1 = (char*)malloc(10);
p2 = new char[10];

Allocate char array with size of 10 in heap section. Be careful, p1 and p2 themselves are automatic variable stored in stack section.
Disadvantage:
(1) memory leak
(2) it can cause external fragmentation, especially when we dynamically allocate variables with various size


Static
Stores global variable and static variable. This section can be further separated to Data section and Bss section.
Data section stores initialized global and static variable, while Bss section stores uninitialized global and static variable.

String Constant
Stores string constant. Memory is released after program exit.

Text
Stores binary codes.

沒有留言:

張貼留言