GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Classify uninitialized, expired and lost pointer targets

4 questions solved the same way, newest first. Back to Programming in C

How these are solved

At every relevant pointer use, check initialization, target lifetime, and whether the allocated object still has an accessible owning pointer. Match errors or choose the applicable diagnosis.

2 marks · MCQ
Report issue

Pointers & Memory

Question 35, GATE 2021: C Node has int value; Node *next. Allocate head=boxE, set value=0. For index=1..3 allocate boxN, set boxE->next=boxN, boxN->value=index, boxE=boxN. Then for index=0..3 print head->value, assign head=head->next, and print head->value again. No final next initialization occurs. A five nodes created; B infinite loop; C missing return causes compile error; D uninitialized pointer dereferenced, possibly runtime error.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

The initial allocation plus three loop allocations create four nodes with values 0, 1, 2, 3. The final node’s next field is never initialized. The second loop advances head and dereferences it again each iteration; on the fourth iteration it reads that uninitialized next pointer and uses it for head->value. This is undefined behavior and may cause a runtime error, D. The loops themselves have bounded iteration counts.

Before the second loop, the chain is 0→1→2→3→uninitialized. The first three iterations safely advance to nodes 1,2,3 if allocation succeeds. The last iteration prints 3, then follows the indeterminate pointer and dereferences it. Initializing next to NULL alone would still leave the unconditional extra dereference invalid; correct traversal tests the pointer before using it. Allocation failure is another issue, not needed for the intended diagnosis.

Original paper, page 75 · Answer key

1 mark · MCQ
Report issue

Pointers & Memory

Question 13, GATE 2017: Printed C source:
#include <stdio.h>
int *assignval(int *x,int val) { *x=val; return x; }
void main() { int *x=malloc(sizeof(int)); if(NULL==x) return; x=assignval(x,0); if(x) { x=(int *)malloc(sizeof(int)); if(NULL==x) return; x=assignval(x,10); } printf("%d\n",*x); free(x); }
Choose A compiler error because malloc lacks a cast; B compiler error because NULL==x must be reversed; C compiles but may leave dangling pointer; D compiles but may leak memory. The missing stdlib.h and void main are literal source issues.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Our answer: D under declared-library interpretation

1. Name the first allocated block A. If its allocation fails the function returns. Otherwise assignval(x,0) changes *x, not x: x still points to A and is nonnull.

2. Thus if(x) is entered even though the integer stored is zero. The second malloc result overwrites x, so no live variable retains A’s address.

3. If allocation B succeeds, printing/freeing uses B only. If it fails, the early return still does not free A. Either way after the second assignment A is leaked. This is intended D, subject to the source-declaration caveat below.

stdlib.h is omitted, so malloc/free lack their required declarations in modern C; void main is not a standard hosted entry point. The intended leak analysis assumes these routine issues are repaired, rather than endorsing option D’s literal “compiles successfully” wording.

Original paper, page 8 · Answer key

2 marks · MCQ
Report issue

Pointer lifetime and initialization errors

Pointers & Memory

Question 2.18, GATE 2001: Three C functions return int pointers. P1 returns &x for automatic local int x=10. P2 declares uninitialized int *px, stores *px=10, then returns px. P3 allocates sizeof(int) with malloc, stores 10 through the pointer, and returns it. Which have pointer problems: only P3; P1/P3; P1/P2; all three? Distinguish the intended successful-allocation model from failure handling.
Question 2.18, GATE 2001: Three C functions return int pointers. P1 returns &x for automatic local int x=10. P2 declares uninitialized int *px, stores *px=10, then returns px. P3 allocates sizeof(int) with malloc, stores 10 through the pointer, and returns it. Which have pointer problems: only P3; P1/P3; P1/P2; all three? Distinguish the intended successful-allocation model from failure handling.
Tap to enlarge
Your answer
Answer & solution

Answer: C under successful allocation for P3 (no official key was published)

Step 1: P1 returns the address of an automatic local int. Its lifetime ends when the function returns, so the returned pointer cannot be used to access that former object.

Step 2: P2 declares an uninitialized pointer and immediately writes through it. No valid target object has been established, causing undefined behavior.

Step 3: P3 allocates sizeof(int), writes 10 into the allocated object, and returns its pointer. Successfully allocated storage remains alive until freed, even after the function returns. Thus the intended contrast is P1 and P2, C.

Practical qualification: P3 must check malloc’s result before dereferencing it and the caller must eventually free it. If allocation fails, the unchecked P3 is also erroneous; appropriate declarations/headers are assumed.

Original paper, page 6

1 mark · MCQ
Report issue

Matching pointer errors to code

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