Memory Segments in C

May 10, 2021 0 Comments

Memory Segments in C

Memory Segments in C, There are different types of memory segments in the userspace.

1. Text segment: 

text segment will store all the instructions which are part of the program.

2. Stack: 

stack segment is used to store all local variables and is used for passing arguments to the functions along with the return address of the instruction which is to be executed after the function call over. The stack and heap grow in opposite directions. If you want to see the stack memory allocation on the process run the below command.

cat /proc/127/smaps (127 is process id)

Memory Segments in C 2
Memory Segments in C 3

3. Initialized Data: 

Memory Segments in C, Initialized data stores all global, static, and const that is initialized beforehand. All global, static, and const variables memory allocated at compile time.

Memory Segments in C 4

See the below-relocated file, x and y variables showed .data, and z variable showed .rodata (read-only data).

Memory Segments in C 5

4. Uninitialized Data or BSS segment: 

uninitialized data stores global and static that are uninitialized beforehand.

Memory Segments in C 6

See the below-relocated file memory.o, x, and y variables showed .bss

Memory Segments in C 7

5. Heap: 

Heap memory uses to store the dynamic memory allocation. If you want to see the heap memory allocation on the process run the below command.

cat /proc/127/smaps ( 127 is process id)

Memory Segments in C 8
Memory Segments in C 9

    

Share This: