i've a code that shouldn't work, but it works. Can you tell me why?
#include <iostream>
void f ( int** a, int b ) ;
int main (void) {
int ** a ;
a = new int* () ;
f(a,5) ;
std::cout << **a << std::endl ;
return 1 ;
}
void f ( int** a, int b ) {
*a = &b ;
}
I declare a pointer to pointer named a, i allocate a pointer to it and then i pass it to f(). The other f() argument is a constant literal, so it is supposed to haven't static memory allocated in main(), ergo it shouldn't exist after f(). Inside f() i assign the memory direction of the local variable b to the pointer to pointer on main copied by f(), then when main() executes f() all local variables should be deleted and then proceed, so a should point to trash, or nothing, but it doesn't and points to 5,value of already deleted b.
What does realy happen? Why this code works?