-4
#include<stdio.h>
int main()
{
        int var=100;
        int *ptr=&var;
        fun(&ptr);
        printf("%p",ptr);
        printf("%d\n",*ptr);
}
int fun(int **var)
{
    int j=10;
    *var=&j;
    printf("%p\n",*var);
    printf("%d\n",**var);
}

Output:

0x7fff2c96dba4 10 0x7fff2c96dba4 10

How is value getting retained even after function completing execution? I executed it several times in gcc and in online compiler it gives the same result. please help me in understanding this...Thanks In advance.

Ravi teja
  • 26
  • 4
  • 4
    Returning (one way or another) a pointer to an object with automatic storage duration, j, and accessing said object after it has gone out of scope has [undefined behaviour](http://port70.net/~nsz/c/c11/n1570.html#J.2): "An object is referred to outside of its lifetime (6.2.4). The value of a pointer to an object whose lifetime has ended is used (6.2.4)." So that's 2 counts of UB. – Ilja Everilä Jan 09 '18 at 16:36
  • 2
    You also never return a value from your non-void function. – Christian Gibbons Jan 09 '18 at 16:41
  • @ChristianGibbons btw, that is *not* UB but very standard conforming behaviour – Antti Haapala -- Слава Україні Jan 09 '18 at 16:43
  • @llija Everila Thank you for your reply.. but the thing which I'm not getting it is it printing the values even after function completion.will it get stored for some time or is it undefined behaviour (UB) – Ravi teja Jan 09 '18 at 16:43
  • In any case, these would be explained in a good C book, read the pages 1 to 483. – Antti Haapala -- Слава Україні Jan 09 '18 at 16:44
  • 1
    this is `c++`, but the same concepts apply: Bottom line, undefined behavior is undefined, _not_ defined to fail. Just because it "works" this time doesn't mean it always will. https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope – yano Jan 09 '18 at 16:45
  • @Raviteja Undefined Behavior means that anything can happen. Ranging from the code doing what you wanted it to do, to blowing up the moon (people seem to love hyperbole for expressing the possibilities of Undefined Behavior). In this case basically there's no guarantee the data will still be there for you to read, but also no guarantee that it won't. It's simply garbage that happens to hold the value you last set to that location. – Christian Gibbons Jan 09 '18 at 16:50
  • After you return from the function `fun()`, the memory where `j` was stored is still there. It even still _may_ have the value which was stored in `j`. It is considered as [UB](https://stackoverflow.com/a/4105123/1505939) to access it after return as the resp. memory of `j` was released (i.e. "declared as free") and might now be unused or otherwise used (for something else). – Scheff's Cat Jan 09 '18 at 16:52
  • @AnttiHaapala Well it may be legal in the C standard, I wouldn't consider it VERY standard conforming. Just as it is standard practice on here to tell people not to cast the result of malloc, I figure it something of this nature would also be worth pointing out. Unless there's a use-case I am unfamiliar with, I do not believe there is a scenario where it is beneficial to have a non-void function with no return. – Christian Gibbons Jan 09 '18 at 17:04
  • I'm voting to close this question as off-topic because OP asks to explain UB – qrdl Jan 09 '18 at 17:32

2 Answers2

0

The value isn't being retained the method 'fun'*sets the address pointed to by main variable 'ptr' to the address of fun variable J, essentially copying the address *var=&j , it doesn't mean that it still points to 'j'

newbie
  • 558
  • 7
  • 12
0

How is value getting retained even after function completing execution?

Undefined behavior (UB). It might appear to "work", but that is not specified to be so by C.

fun(&ptr); runs fine, yet printf("%p",ptr); is UB as the value ptr is no longer valid. Many systems will tolerate this UB.

De-referencing ptr, with printf("%d\n",*ptr); is even worse UB. More likely to behave badly. Best not to attempt either of these.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256