I know this won\'T work because the variable x gets destroyed when the function returns:
int* myFunction() { int x = 4; return &x; }
For C++, you can use a smart pointer to enforce the ownership transfer. auto_ptr or boost::shared_ptr are good options.
auto_ptr
boost::shared_ptr