1,315 views
2 votes
2 votes
void fun(int **pptr)
{
   int q = 10;
  *pptr = &q;
}

int main()
{
  int r = 20;
  int *p = &r;
  fun(&p);
  printf("%d", *p);
  return 0;
}

The output of the program is 10 . But as q here is an automatic variable, the result shouldn't be 10.

1 Answer

Best answer
5 votes
5 votes

@siddhartha You are absolutely right that it should print the garbage value. But it is printing 10. 

Here is the reason. This program is printing 10, because you are running this program in the windows environment. Windows does not clear the memory when the stack gets deleted. It just deletes the reference. What happens that you have the address in P, which is pointing to value 10, which has not been replaced by any other program, hence you are getting the 10. 

Run this program in Linux environment and i am 100% sure you will get 0 as output. In the Linux, when a stack gets deleted it reset all the memory as 0. Hence you get 0. 

Here, I run this program at the ideone.com which is a Linux based system. I get 0.

selected by

Related questions

0 votes
0 votes
1 answer
2
Nitesh Choudhary asked Apr 22, 2017
3,419 views
i want to read Tanenbaum Datastructure book. Can anyone tell me the source of pdf Tanenbaum Datastructure book ?
0 votes
0 votes
1 answer
3
Nitesh Choudhary asked Apr 16, 2017
477 views
There are many questions related to size of pointer and it's also depend on machine so anyone tell me source of reading pointer. (for Gate)
–2 votes
–2 votes
0 answers
4
israel biruk asked Dec 21, 2015
873 views
Source code for building Hotel Management system in java