GATE CS 2017 (Forenoon) — Question 13

Programming in CDynamic Memory Allocation & File HandlingMCQ1 mark · −1/3 if wrongMedium

Consider the following C code:
c
1#include <stdio.h>
2int *assignval (int *x, int val) {
3    *x = val;
4    return x;
5}
6void main ( ) {
7    int *x = malloc (sizeof(int));
8    if (NULL == x) return;
9    x = assignval(x, 0);
10    if(x) {
11        x = (int *) malloc (sizeof(int));
12        if (NULL == x) return;
13        x = assignval (x, 10);
14    }
15    printf ("%d\n", *x);
16    free (x);
17}
18
The code suffers from which one of the following problems:
  1. A
    compiler error as the return of malloc is not typecast appropriately
  2. B
    compiler error because the comparison should be made as x == NULL and not as shown
  3. C
    compiles successfully but execution may result in dangling pointer
  4. D
    compiles successfully but execution may result in memory leak
Practise the full 2017-FN paper
Challenge a friend: WhatsApp Telegram
From RENYXERAWhat rank do your marks get?GATE score, likely All-India Rank and qualifying marks for your category.
Practise more on Dynamic Memory Allocation & File HandlingTimed, in the real exam interface · free to start

A GATE question every morning

One real past-paper question every morning for your branch, answer hidden until you tap. Free on Telegram.

Join
Stuck? Ask in the student group