The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+30 votes
3.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.6k points)
edited by | 3.8k views
+2
P3 also fails when all malloc return NULL pointer right ??
0
what in the case when heap is full and malloc returns a null?
0
I executed P1 on Turbo C and it is running fine without any error. Can any1 explain me P1 ?

2 Answers

+52 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 (41k points)
selected by
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 ?
+4
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 .
+2
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.
+3
$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
+2
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??
+2
Oh!  I got it...In the calling function free() will be used...and memory leak can be avoided...
0

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

+1

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.

+10 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)
0
can u please explain why memory is not allocated by os and why is dangling problem there @MohitDubey1
+1
I think that the [p3] will work fine because the memory will be allocated by the malloc and the address of that new memory will be saved inside the px, in the next statement the newly allocated memory will contain 10.

Please correct me if i am wrong anywhere.
0
how to know that in this que (p3) space is sufficiant?
0
How do we got to know that the space is notenough. There is nothing mentioned in question.
0
yes nothing is mentioned here thats why there is a case of not having sufficient memory , I think D is the option.
0
I don't know whom to trust as one answer is supporting C and other one is supporting D. Can you please share something to support your explanation. It will be really helpful to all.
0
I saw the solution in Kiran sir video lectures (youtube).
0
Please share the link.
+3

0
Thanks for the link :D
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

42,619 questions
48,614 answers
155,858 comments
63,845 users