Skip to content
Official PYQ · 2000

GATE CS 2000 Q1.12 — Programming in C

Explain with AI

The most appropriate matching for the following pairsX:m = malloc(5); m = NULL;1:using dangling pointersY:free(n); n -> value = 5;2:using uninitialized pointersZ:char *p , *p = ‘a’ ; 3:lost memory\begin{array}{|ll|ll|}\hline X: & \text{m = malloc(5); m = NULL;} & 1: & \text{using dangling pointers} \\ \hline Y: & \text{free(n); n -> value = 5;} & 2: & \text{using uninitialized pointers} \\ \hline Z: & \text{char *p , *p = ‘a’ ; } & 3: & \text{lost memory} \\ \hline \end{array} is:

Choose one option
View worked solution

Answer: D

We can determine the correct matching by analyzing each scenario step-by-step:

  1. Scenario X: m = malloc(5); m = NULL;
    Memory of 55 bytes is dynamically allocated on the heap, and its address is stored in the pointer m. When m is reassigned to NULL without freeing the allocated memory, the program loses the reference to that heap memory. This results in a memory leak, also known as lost memory.
    Hence, X→3\text{Hence, } X \rightarrow 3
  2. Scenario Y: free(n); n -> value = 5;
    The memory block pointed to by n is deallocated using free(n). After deallocation, n still holds the address of that memory block, making it a dangling pointer. Attempting to access or modify members using n->value is an operation on a dangling pointer.
    Hence, Y→1\text{Hence, } Y \rightarrow 1
  3. Scenario Z: char *p; *p = 'a';
    The pointer p is declared but not initialized to point to any valid memory address (it contains a garbage value). Dereferencing it with *p = 'a' attempts to write to an arbitrary memory location, which is a classic case of using an uninitialized pointer.
    Hence, Z→2\text{Hence, } Z \rightarrow 2

Combining these matches, we get: X−3,Y−1,Z−2\mathbf{X - 3, \quad Y - 1, \quad Z - 2} This corresponds to Option D.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions