edited by
23,844 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$
edited by

3 Answers

Best answer
109 votes
109 votes

$[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
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.

 

 

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.
Answer:

Related questions

37 votes
37 votes
6 answers
4
Kathleen asked Sep 14, 2014
17,886 views
Which of the following statements is false?An unambiguous grammar has same leftmost and rightmost derivationAn LL(1) parser is a top-down parserLALR is more powerful than...