Memory Layout of C program Flashcards

1
Q

the memory layout of C program contains X segments

A

five segments

these are the stack segment, heap segment, BSS (block started by symbol), DS (Data Segment) and text segment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A segmentation fault

A

A segmentation fault is a common problem that causes programs to crash. Each segment has own read, write and executable permission. If a program tries to access the memory in a way that is not allowed then segmentation fault occurs. A core file (core dumped file) also associated with a segmentation fault that is used by the developer to finding the root cause of the crashing (segmentation fault).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Stack

A

Stack:
It located at a higher address and grows and shrinks opposite to the heap segment.
The stack contains local variables from functions and related book-keeping data.
A stack frame will create in the stack when a function is called.
Each function has one stack frame.
Stack frames contain the function’s local variables arguments and return value.
The stack contains a LIFO structure. Function variables are pushed onto the stack when called and functions variables are popped off the stack when return.
SP(stack pointer) register tracks the top of the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Heap

A

Heap:
It is used to allocate the memory at run time.
Heap area managed by the memory management functions like malloc, calloc, free, etc which may internally use the brk and sbrk system calls to adjust its size.
The Heap area is shared by all shared libraries and dynamically loaded modules in a process.
It grows and shrinks in the opposite direction of the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

BSS(Uninitialized data segment):

A

BSS(Uninitialized data segment):
It contains all uninitialized global and static variables.
All variables in this segment initialized by the zero(0) and pointer with the null pointer.
The program loader allocates memory for the BSS section when it loads the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

DS(Initialized data segment)

A

DS(Initialized data segment):
It contains the explicitly initialized global and static variables.
The size of this segment is determined by the size of the values in the program’s source code and does not change at run time.
It has read-write permission so the value of the variable of this segment can be changed at run time.
This segment can be further classified into an initialized read-only area and an initialized read-write area.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Text:

A

Text:
The text segment contains a binary of the compiled program.
The text segment is a read-only segment that prevents a program from being accidentally modified.
It is sharable so that only a single copy needs to be in memory for frequently executed programs such as text editors etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly