The most appropriate matching for the following pairs is: X: m = malloc(5); m…

2000

The most appropriate matching for the following pairs is:

X: m = malloc(5); m = NULL; -> 1: using dangling pointers
Y: free(n); n->value = 5; -> 2: using uninitialized pointers
Z: char *p; *p = 'a'; -> 3: lost memory

  1. A.

    X-1, Y-3, Z-2

  2. B.

    X-2, Y-1, Z-3

  3. C.

    X-3, Y-2, Z-1

  4. D.

    X-3, Y-1, Z-2

Attempted by 15 students.

Show answer & explanation

Correct answer: D

In X, memory is allocated by malloc, but the only pointer m is then overwritten with NULL. The allocated block can no longer be reached, so it is lost memory. Hence X matches 3.

In Y, n is freed and then dereferenced using n->value. After free(n), n is a dangling pointer, so Y matches 1.

In Z, p is declared but not initialized before *p is used. This is use of an uninitialized pointer, so Z matches 2.

Therefore the matching is X-3, Y-1, Z-2.

Explore the full course: Gate Guidance By Sanchit Sir