Discussion
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));
}
Answer: C
Correct answer: i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap
Each of the three lives in a different region. i is a global with no initialiser, so it has static storage duration and is zero-initialised - such objects go in the BSS, which the loader clears rather than reading from the file.
j is an automatic variable inside main, so it is on the stack. The object k points to comes from malloc and so lives on the heap; note that k itself, the pointer, is on the stack alongside j.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development