The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+24 votes
2.8k views

Consider the following three C functions:

$[P1]$ 

int *g(void)
{
    int x = 10;
    return (&x);
}

$[P2]$ 

int *g(void)
{
    int *px;
    *px = 10;
    return px;
}

$[P3]$ 

int *g(void)
{
    int *px;
    px = (int*) malloc (sizeof(int));
    *px = 10;
    return px;
}

Which of the above three functions are likely to cause problems with pointers?

  1. Only $P3$
  2. Only $P1$ and $P3$
  3. Only $P1$ and $P2$
  4. $P1, P2$ and $P3$
asked in Programming by Veteran (59.4k points)
edited by | 2.8k views
0
P3 also fails when all malloc return NULL pointer right ??

2 Answers

+47 votes
Best answer

$[P1]$ may cause an error because function is returning the address of locally declared variable.

$[P2]$ will cause a problem because px is an int pointer that is not assigned with any address and we are doing dereferencing.

$[P3]$ will work because memory in bytes of size of int will be reserved and its address will be stored in px that can be further use, once function execution completes, this m/m will still exist in Heap until we free it using $free()$ function.

hence answer is (C)

answered by Boss (39k points)
edited by
+5
Yes, we can return. But we shouldn't use that address and access its contents as that can lead to undefined behaviour in C- process can crash or give garbage value.
0
It is supposed to return address of int, and it is doing the same. So, why type casting needed?
0
P3: isnot that a memory leak problem how come u will be ensure that it will work fine ?
+9

It is not necessary P3 should cause memory leak. This can be handled by the caller as follows.

int *g(void)
{
    int *px;
    px = (int*) malloc (sizeof(int));
    *px = 10;
    return px;
}
int main()
{
        int *p = g();
        printf("%d\n", *p);
        free(p);
}

 

+3
P3 also has problem. what if malloc returns null then? you are accessing *px, which gives runtime error.
+7
Again "likely" is there in question :)
0
Where can we read more about this kind of function behaviour and examples??
+5
This is about knowing the properties of variables- like scope and lifetime. No other concept involved. For questions, may be you can take GO Programming test, but previous GATE ones are also enough.
0
@arjun sir. whether p1 will always throw an error or not. I understand x is a local variable and its scope is limited to the lifetime of function g. once the function g is executed variable x will be no longer available in the memory. sir, i tried to execute the same type of program on codechef compiler but I didn't get any error. plz correct me where am i wrong

#include <stdio.h>
int *call(void)
{
    int x=10;
    return(&x);
}
int main(void) {
    int *p;
    p=call();
   printf("%u",p);
   printf("%d",*p);
    return 0;
}
0
Please explain why P1(returning address of locally declared variable) will cause error, since the return type match is not violated.
+1
if u write a c program of p1 and use an extra function in main section then it has the maximum probabilty of showing garbage values rather than value 10 ...
0
dhanyawad mam
0
g is returning address of x , and address are hexadecimal ..but the return type of g is int. Is this the reason of showing error ? Am I correct ?
+2
P1 is wrong because, we r taking a local variable x. Scope of x will scope to that function only. Now, at the end of function we are returning x. But as function ends, scope of x also ends. So, it might return garbage value .
+1
In P2 we are assigning a value inside integer pointer, where no space is allocated. So, there may be a case px access a value from OS, which should not use by general user. That is why it is a illegal access.
+1
$P_3$ is correct and will not throw error on return because memory in $P_3's$ case is allocated by heap whose lifetime spans  throughout the  execution of program.
0
int main()
{

int y=g();
    printf(y);

    
}
int *g(void)
{
    int x=10;
    return &x;
}

it is showing compile time error for P1.pls check
+5 votes
p1  will cause problem as x is a local variable and the return address will be returned the value of x which will be null after function ends.

p2 will cause problem as the pointer is not initialised.

p3 will cause error as if no memory is allocated by the OS the malloc function will return Null and it will be dangling pointer problem.

 

so right answer is D.
answered by (69 points)
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

34,770 questions
41,729 answers
118,875 comments
41,381 users