Messing up with pointers!

I was wandering what happens in this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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()?
No.
All you do there is leak some memory.
Thank you for the reply!
That is true, i forgot that &x only returns the address value.
Thank you!
Topic archived. No new replies allowed.