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

Consider the following three C functions:


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


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


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.5k points)
edited by | 3.1k views
P3 also fails when all malloc return NULL pointer right ??

2 Answers

+49 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 (39.8k points)
edited by
dhanyawad mam
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 ?
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 .
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.
$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.
int main()

int y=g();

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

it is showing compile time error for P1.pls check
In P3, if the given function is called multiple times...there will be a memory leak?? As free(px) is not used inside the function.....So P3 may also cause problem??
Oh!  I got it...In the calling function free() will be used...and memory leak can be avoided...

In P3,can someone explain if malloc() returns null and we do initialization of  px there....won't that lead to an error as we are trying to initialize a memory which don't even exists??  Plz clarify then how P3 works fine here?

Anu007 plz can you look into it


Read question they asked: Which of the above three functions are likely to cause problems with pointers?

so this will clear that memory is there to allocate.

+8 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 (89 points)

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

37,110 questions
44,694 answers
43,752 users