The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+36 votes
4.9k 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 (52.1k points)
edited by | 4.9k views
+5
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 ?
0
@Dhiraj Raj

You won't get an error unless you use this returned address.
e.g. try to dereference this x, like int p = *x;
then you will get segmentation fault error.

2 Answers

+59 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 (42.4k points)
selected by
+4
$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...
+1

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

+2

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.

0

P3 might not work in case there isn't sufficient memory available in the system (in that case malloc would return NULL).

I agree that what I'm saying is a rare case, but a problem is still a problem, right ?

So, according to me, the correct answer should be (C).

0
What is the lifetime of dynamically allocated memory location????

Is it throughout the program execution or it will exist until we free() it even after program execution completes??
0
If it exists even after program termination we will be restarting our machines every day at least.
0

@Arjun sir  So, untill restart that memory is wasted??

+17 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.
+6

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
49,811 questions
54,533 answers
188,412 comments
75,554 users