in Programming in C edited by
24,025 views
62 votes
62 votes

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$
in Programming in C edited by
24.0k views

4 Comments

Then too its not a problem. 

I tried this 

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

it doesn't give error.

Its a fact that functions can return null pointers.

Anyways handling the NULL pointer is a must thing in case of malloc and it should be ignored.

0
0
Although P3 could also cause segmentation fault, but thats like once in a blue moon. P1 and P2 have logical problems, while P3 is not dependent on logic. So, yes, C looks more convincing here over D. But again, D is not wrong either implementation wise as malloc could yeild null. They should’ve provided some context on this.
0
0
in P1 there is a possibility of dangling pointer
0
0

3 Answers

109 votes
109 votes
Best answer

$[P1]$ may cause an error because the function is returning the address of a 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 the memory in bytes of the size of an int will be reserved and its address will be stored in px that can be further used, once function execution completes, this m/m will still exist in Heap until we free it using $free()$ function.

Hence, the answer is (C).

edited by

4 Comments

ohh yes! I forgot why I was confused that time.

but malloc returning NULL is very less likely to happen, so P3 will run properly.

3
3

@Pranavpurkar happens with me also xD

1
1
but in p3, we are giving integer as input to a pointer rather than int address. will that not create a problem??
0
0
33 votes
33 votes

P1 might cause issues.

int *g(void)
{
    int x = 10; 
/* "x" is a local variable.
The lifetime of x is only until the execution of g().
So, we're returning an address which will soon expire. It screams runtime error.
*/
    return (&x);
}

 

P2 might cause issues.

int *g(void)
{
    int *px; 
/* px is a local pointer variable.
 The value of a local variable, if not initialized, is garbage value.
Hence, the content of px is currently garbage*/
    *px = 10;
/* Here, we're trying to access the garbage contents of px. So, Runtime error. */
    return px;
}

 

P3 might cause issues.

int *g(void)
{
    int *px; 
/* Local pointer variable px. Currently, it's content is garbage value, because not initialized yet. */
    px = (int*) malloc (sizeof(int));
/* Now, we initialized px, hence it's content isn't garbage anymore.
What we did is, assigned a block of memory from heap. In case the heap is full,
initialization fails. So, in the worst case, contents of px remain garbage value */
    *px = 10;
/* Trying to access garbage value. Runtime error. */
    return px;
}

 

Correct Answer, Option D.

But if we focus on the word "likely" in the question, then almost all modern computing systems use Virtual Memory, which provides an illusion of a huge amount of addressing space to each process. Hence, they see an almost unlimited heap at their disposal. So, malloc() is extremely unlikely to return NULL. So, the answer could be Option C, too — and I'm more inclined to option C.

 

 

1 comment

good explanation
1
1
22 votes
22 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.

4 Comments

Please share the link.
0
0
Thanks for the link :D
1
1
Answer:

Related questions