C | Dynamic Memory Allocation | Question 8
Consider the following program, where are i, j and k are stored in memory?
int i; int main() { int j; int *k = (int *) malloc (sizeof(int)); } |
(A) i, j and *k are stored in stack segment
(B) i and j are stored in stack segment. *k is stored on heap.
(C) i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap.
(D) j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on heap.
Answer: (C)
Explanation: i is global variable and it is uninitialized so it is stored on BSS part of Data Segment (http://en.wikipedia.org/wiki/.bss)
j is local in main() so it is stored in stack frame (http://en.wikipedia.org/wiki/Call_stack)
*k is dynamically allocated so it is stored on Heap Segment.
See following article for more details.
Memory Layout of C Programs
Quiz of this Question
Recommended Posts:
- C | Dynamic Memory Allocation | Question 7
- C | Dynamic Memory Allocation | Question 5
- C | Dynamic Memory Allocation | Question 8
- C | Dynamic Memory Allocation | Question 3
- C | Dynamic Memory Allocation | Question 2
- C | Dynamic Memory Allocation | Question 1
- C | Dynamic Memory Allocation | Question 6
- Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
- How to restrict dynamic allocation of objects in C++?
- MCQ on Memory allocation and compilation process
- IPC through shared memory
- How are variables scoped in C - Static or Dynamic?
- How to deallocate memory without using free() in C?
- What is Memory Leak? How can we avoid?
- Memory Layout of C Programs



