2

Lets say I have following function, and it has an argument which is type T. What is life time of that argument.

int* f()
{
   int x=0;
   int* ptr=&x;
   return ptr; //i know this is undefined behavior.
}

So in f() function when it s called, local expressios will run and end of scope pointed value will be deleted. But my question is for following function

void f2(int* y)
{
}
int main()
{  
   int p=0;
   f2(&p);
   cout<<p<<endl;
   return 0;
}

Here, when we call f2() when that parameter int* y will be deleted? And so if its deleted logicaly pointed value will be deleted which is p, why I can see value of p same using cout? So when f2's argument will be deleted? when function's end scope? or what?

Vito Carleone
  • 157
  • 3
  • 8
  • 1
    Strictly speaking, that is not undefined behavior. `ptr`'s value will be the address that `x` had and that is what `f` will return. There's an opportunity for undefined behavior to occur if something should dereference the pointer returned by `f`. – Sion Sheevok Nov 08 '13 at 18:35
  • @SionSheevok http://stackoverflow.com/questions/19488317/is-my-code-undefined-behavior – Vito Carleone Nov 08 '13 at 18:37
  • "I wrote some code with a bug in it. Why doesn't it do what I expect?" – David Schwartz Nov 08 '13 at 18:37
  • 1
    @VitoCarleone Precisely, look at the marked answer: "Yes, returning a pointer to a temporary local object and **dereferencing** that is undefined behavior." – Sion Sheevok Nov 08 '13 at 18:39
  • 1
    `y`'s lifetime ends when the function `f2` ends. The value that `y` points to exists independently from `y`. It's possible to have a pointer pointing to a value that no longer exists. In that case, trying access the value through the pointer will cause problems – A.B. Nov 08 '13 at 18:42

5 Answers5

9

In void f2(int* y), you have a copy of a pointer. The lifetime of that pointer object y, extends to the end of the function f2.

In your main function, you have an integer p. The lifetime of p extends to the end of main.

When calling f2(&p), a temporary pointer is created to be passed to f2. It does not modify the lifetime of p. So, when f2 returns, the temporary pointer is no longer in scope, however p is still in scope (and therefore, valid).

All of the variables shown have automatic storage duration and do not require explicit "deletion".

Chad
  • 18,706
  • 4
  • 46
  • 63
  • Your's has the most markdown that must be how you got the votes – aaronman Nov 08 '13 at 18:41
  • ok think int* a=&b; when i delete or assign another variable to int* like int* a =&s; b still will be same value. so thats why i can reach p in question. is that? – Vito Carleone Nov 08 '13 at 18:51
  • A pointer is a simple construct, yes. It doesn't (automatically) do anything to the "pointed-to" object. – Chad Nov 08 '13 at 18:56
  • 1
    @aaronman: Just like questions, well-formatted answers are easier to read, easier to understand, and therefore more helpful. – John Dibling Nov 08 '13 at 19:08
3

The function does no memory management on arguments you pass it unless the argument is passed by value (technically everything is passed by value in c++, except a reference, but in the case of a pointer there isn't much to clean up). In the case of your code p is destructed when main's scope ends. Remember this is because p has automatic storage if it had dynamic storage it would be deleted only when you call delete on it.

Now in general there is no reason to pass around an automatic variable as a ptr because c++ has this great thing called pass by reference

void f2(int& y)  

This is what you should be doing in most cases

aaronman
  • 18,343
  • 7
  • 63
  • 78
  • Nit: "technically everything is passed by value in c+" References aren't. – John Dibling Nov 08 '13 at 19:10
  • @DavidSchwartz yeah I retracted that last comment (I guess you can see deleted ones) because I remembered how java's references are passed by value and how they behave differently than c++'s (but IT there is still some cleanup done for references even in c++) – aaronman Nov 09 '13 at 00:56
2

A function parameter is just a local variable in the block scope of the function body. The difference between a function parameter and an ordinary block scope variable is that before the body begins executing, the function parameter is initialized from the corresponding function call expression.

The lifetime of the variable y extends only over the execution of the body of function f.

The object which the pointer object named y points to has a lifetime which is completely unrelated to y.

Kaz
  • 55,781
  • 9
  • 100
  • 149
1

p is created on stack when main is entered. you call f and pass the address of p to it. The f's parameter y gets this address. y is also stored on stack. When f returns, y is deleted because it is a local variable, but p still exists and you print its value. Similar to y, the local variable p is deleted when main exits.

akonsu
  • 28,824
  • 33
  • 119
  • 194
1

Pointers are not "deleted" automatically (in the sense that the delete operator is applied to them) at end-of-scope. The variable containing the address itself will be deleted, but the data it points to will not.

You might look at std::unique_ptr, which does what you're describing.

alecbz
  • 6,292
  • 4
  • 30
  • 50