GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 1.12

Matching pointer errors to code

Programming in C · Pointers & Memory · Full GATE CSE 2000 paper

GATE 2000, Q1.12 1 mark · MCQ
Report issue

Pointers & Memory

Question 1.12, GATE 2000: Classify X: m=malloc(5);m=NULL; Y: free(n);n->value=5; Z: char *p;*p='a'. Categories: 1 dangling-pointer use,2 uninitialized-pointer use,3 lost memory.
Tap to enlarge
Your answer
Answer & solution

Answer: D: X3,Y1,Z2 (no official key was published)

Step 1: In X, assigning NULL over the sole saved address of a successful allocation loses the ability to free/access that block, creating lost memory or a leak:3.

Step 2: In Y, free ends the allocated object’s lifetime; the later dereference uses a dangling pointer:1. In Z, an uninitialized pointer has not been made to point at valid writable storage, so dereferencing it is an uninitialized-pointer error:2.

The mapping X3,Y1,Z2 is D. Successful allocation, no second saved reference, and initially valid n are the conventional premises for the first two examples.

Original paper, page 2