Consider the following C code gate computer science 2017
The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we solve previous year question papers for you.
See Our team
Wondering how we keep quality?
Got unsolved questions?

Ask Questions
Hey! Want to work with us? Our youtube channel See our Jd Want to apply? Do it today

Use Me  ?

New searches
GATE-Computer-Science-Engineering-2017-->View question


Consider the following C code: -gate computer science 2017

#include

int * assignval (int *x, int val)
{
    *x = val;
    return x;
}

int 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);
}
The code suffers from which one of the following problems:
 A) compiler error as the return of malloc is not typecast appropriately.
B) compiler error because the comparison should be made as x==NULL and not as shown.
C) compiles successfully but execution may result in dangling pointer.
D) compiles successfully but execution may result in memory leak


By:satyashiromani

Taged users:
|satyashiromani|alvin|vaishnavi-Deshpande|milanyoyoyogmailcom|Umang|ThreeRed|Msshikhil|user1|deepuckraj|Manisha12|milan-ransingh|batsam22|13priya|aksingh1818|metaphor|leo|Aaditi|madachod|tichaona-garaidenga|ronald|PerumalS|jinsnjjose|dillu550|sumesh|bino|anushree|Abdul-Wajid|Vinit|thegdx|Raju-bhai

Likes:
|deepuckraj

Dislikes:
Be first to dislike this question

Talk about thisDelete|Like|Dislike|


Answers

D) compiles successfully but execution may result in memory leak.

Explanation: 
The code will run and give output = 10, so option A and B are discarded.
 int * x= malloc (sizeof(int));
This statement assigns a location to x. Now,
(int*)malloc(sizeof(int));
again assigns a new location to x , previous memory location is lost because now we have no reference to that location resulting in memory leak. Therefore, option D is correct. Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate. Detailed article on Memory Leak Running code of the question: [sourcecode language="C"] #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\n", *x); free(x); } [/sourcecode]


deepuckraj

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer
Talk about this|Once you have earned teacher badge you can edit this questionDelete|Like|Dislike|
------------------------------------

Can you help us to add better answer here? Please see this

Loading...


Not the answer you're looking for? Browse other questions from this Question paper or ask your own question.

Join eduladder!