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

#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|

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.



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


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.
Career: Work or do your internship 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.
Donate: Ad revenue alone is not able to take care of our server cost consider donating at least a dollar Click here to donate.

If you want the help of doing any of these never hesitate to drop an email to me arunwebber[at]gmail[dot]com, Or you can ping us on WhatsApp ph: 8147433408

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



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

Join eduladder!