Question

Match the following:

Item Description
(P) static char var; (i) Sequence of memory locations to store addresses
(Q) m = malloc(10); m = NULL; (ii) A variable located in data section of memory
(R) char *ptr[10]; (iii) Request to allocate a CPU register to store data
(S) register int var1; (iv) A lost memory which cannot be freed
Choose one option
Official answer and solution
Official answer
A
From
Official GATE 2017 answer key

Solution

Match each C declaration or code snippet with its corresponding memory segment, storage class, or memory behavior.

Analyze each item:

  1. static char var;: Static variables with file or block scope reside in the data section (BSS or initialized data) of the process address space. This matches (ii).
  2. m = malloc(10); m = NULL;: Dynamic memory allocated on the heap loses its only pointer reference when m is set to NULL, resulting in a memory leak where the allocated block cannot be freed. This matches (iv).
  3. char *ptr[10];: This declares an array of 10 pointers to char, which is a sequence of memory locations holding addresses. This matches (i).
  4. register int var1;: The register storage class is a hint/request to the compiler to store the variable in a CPU register for faster access. This matches (iii).

Combining the pairs gives P →\to (ii), Q →\to (iv), R →\to (i), S →\to (iii), which corresponds to option A.

Final answer

A

Common trap

Confusing an array of pointers char *ptr[10] with a pointer to an array char (*ptr)[10].

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2017 Computer Science and Information Technology (CS) Session 2
Question
2 of 65, printed as CS Q.2
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.