GateAI
Programming in C

GATE CSE 2017 Set 1 · Q13

MCQ1 markMedium

Time

Consider the following C code:#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 ", *x); free(x); }The code suffers from which one of the following problems:

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: D) compiles successfully but execution may result in memory leak

Programming in C
  1. In C, dynamic memory allocated with malloc() returns a void *, which implicitly converts to any data pointer type without explicit typecasting. Thus, lines with or without (int *) compile correctly.

  2. Writing if(NULL == x) is completely valid C syntax (often known as a Yoda condition) and prevents accidental assignment.

  3. In the program:

    • Memory is dynamically allocated for the first integer, and its address is stored in x.

    • Inside the if(x) block, malloc() is called again and its address is assigned to x without first freeing the previously allocated block of memory.

    • This causes the pointer to the first allocated block to be overwritten and lost, meaning that memory can no longer be reached or freed. This creates a memory leak.

  4. Hence, the code compiles successfully but execution may result in a memory leak (Option D).

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q13 of the GATE CSE 2017 Set 1 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 20 of them are 1-mark MCQs like this one, and GATE CSE 2017 Set 1 has 4.

How GATE tests Programming in C

More from GATE CSE 2017 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2017 Set 1 · Q13 · MCQ

Consider the following C code: #include 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 ", *x); free(x); } The code suffers from which one of the following problems:

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.