GATE 2017 Previous Year Paper Solution Question 11 – Computer Science
In this video, the question which was asked in the exam of GATE 2017, has been discussed.
Question :- 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(“%dn”, *x);
free (x);
}
The code suffers from which one of the following problems :
1.compiler error as the return of malloc is not typecast appropriately.
2. compiler error because the comparison should be made as x==NULL and not as shown.
3. compiles successfully but execution may result in dangling pointer
4. compiles successfully but execution may result in memory leak.
Solution is given in the video.