Reputation: 167
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?
Upvotes: 3
Views: 3197
Reputation: 18751
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
Upvotes: 3
Reputation: 29566
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.
Upvotes: 1
Reputation: 6488
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.
Upvotes: 1
Reputation: 19052
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".
Upvotes: 9
Reputation: 58617
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
.
Upvotes: 2