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

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

Consider the following C code: -gate computer science 2017


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);
        x = (int*) malloc(sizeof (int));
        if (NULL == x) return;
        x = assignval (x, 10);
    printf("%d\n", *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

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

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,
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]

Answerd By:deepuckraj

Be first to like this answer

Be first to dislike this answer
Talk about this  Delete  Like  Dislike

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

You may also like our videos

Below are some of the videos from our collection. We saw that students not only needed content but also videos. So, we decided to build a video platform for you also an algorithm which shows best videos suites to you related to the content you are browsing check out some videos which suit best for you.

Lets together make the web is a better place

We made eduladder by keeping the ideology of building a supermarket of all the educational material available under one roof. We are doing it with the help of individual contributors like you, interns and employees. So the resources you are looking for can be easily available and accessible also with the freedom of remix reuse and reshare our content under the terms of creative commons license with attribution required close.

You can also contribute to our vision of "Helping student to pass any exams" with these.
Answer a question: You can answer the questions not yet answered in eduladder.How to answer a question
Career: Work or do your internship with us.Work with us
Create a video: You can teach anything and everything each video should be less than five minutes should cover the idea less than five min.How to upload a video on eduladder
Donate: Ad revenue alone is not able to take care of our server cost consider donating at least a dollar Click here to donate.

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

Ask your question?