class A; //Any class, just for the example
void f(A* a){
a = new A();
}
int main()
{
A x;
f(&x);
/* What's happens with x now? */
return 0;
}
When I declare x his memory is reserved in the stack, right? But function f allocates dynamic memory for the pointer.
So, what's happens with the memory reserved for the x in the stack at the first place? It is any way to have access to it again?
Is it right to think that (&x)-> will access the elements of the object allocated dynamically and "delete (&x)" would release the memory allocated at f()?
This is a c/c++ black hole, isn't it? And should be avoided, right?
Thank you for your attention!
Tiago
In the function f, you pass in a temporary object; a pointer.
Then, that pointer gets changed, so it points to a the new object. NOTHING happens to the original object. All you've done is make a pointer to the original object, and then change that pointer. You have not touched the original object.
Is it right to think that (&x)-> will access the elements of the object allocated dynamically
No.
"delete (&x)" would release the memory allocated at f()?