After writing a C program this is the first question that comes in our mind.
- Code Section: This part of memory contains the program code,Its usually read only.
- Data Section: This section contains all the static,global and extern variables defined by the programmer.
- BSS(uninitialized data):If we are not initialing the static variable or global variable.Lets say we declare an variable: int i; this is initialized to zero default & stored in this section.
- Initialized data :If we initialize a global or static variable say int i=10;It is stored in this section.
- Heap Section: Dynamic allocated memory using functions such as calloc() and malloc() is stored in this part of the memory.
- Stack Section: Whenever a function is called in C program,respectively a function stack frame is created,which is eventually destroyed after the call.This function stack frame is stored in stack section.All auto variable which are declared inside the function becomes the part of this memory section.
FAQ in interviews:
- Why do we need a BSS segment? Can't we just use memset() to clear the memory and use only one segment in data section?
- Why do we need a heap section?
- Where will extern int i=10 gets stored?
- Where are function arguments stored in C?