Yes, this will give a dangling pointer. If you want the value to persist, you have a couple of possibilities. One is that since you apparently really want to return a value, do exactly that -- return the value instead of a pointer. Another is to allocate the object involved with new, and return a pointer to that dynamically allocated space. This makes deleting the object the responsibility of the caller, however, which can lead to memory leaks and such.
In the end, you generally want to figure out what scope the variable really needs, and have it owned by something (most often an object) with that scope, so it'll be created automatically on entry to the scope, and destroyed automatically on exit from the scope.
If you can't do that, you still need to figure out who "owns" the object, and will be responsible for destroying it as needed. Once in a while that's hard to pin down, and you need something like a reference-counted pointer to keep track of things. At least in my experience, these are used much more often than they really should be though. I've been writing C++ almost as long as anybody outside of AT&T, and haven't needed a shared_ptr yet. A few times, I've thought I'd need it, but eventually figured out a cleaner design the eliminated the requirement.