GATE / CS ARCHIVE

SUBJECT COLLECTION

Programming in C · Other / Mixed Topics

1 questions, newest first

Browse topics
1 mark · MCQ
Report issue

Other / Mixed Topics

Question 2, GATE 2017: Match C constructs P: static char var; Q: m=malloc(10); m=NULL; R: char *ptr[10]; S: register int var1; with i: sequence of locations storing addresses; ii: variable in data section; iii: request for CPU-register storage; iv: lost allocated memory that cannot be freed through the overwritten pointer. Options give (P,Q,R,S): A:(ii,iv,i,iii), B:(ii,i,iv,iii), C:(ii,iv,iii,i), D:(iii,iv,i,ii).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

1. P has static storage duration, conventionally placed in the static data/BSS region rather than a function-call stack, so P→ii. This is the exam's memory-layout description; the C language itself does not mandate a particular section name.

2. Q obtains a heap allocation and then overwrites its address with NULL. Assuming allocation succeeds and no other pointer retains the address, the allocation becomes unreachable and cannot be passed to free: Q→iv. Assigning NULL does not free it.

3. R declares an array of ten pointers to char, so its elements hold addresses: R→i. S uses register as a request/hint for register allocation, not a guarantee: S→iii. The complete mapping is A.

Original paper, page 2 · Answer key