I am having difficulties to remember whether in c++ a local (non static, and inside a block) variable defined in the block is destroyed or not as long as a pointer points to it, even after the execution leaves the block.
So if I created a int inside a block, and have a global pointer, and I leave the block, can my pointer still find that int?
I didn’t finding a clear answer to this online, though it’s probably been answered more than once, sorry about that.
No, you’re in undefined behavior territory.
I’m assuming you mean something like:
If you attempt to dereference
pafter}, you’ll run into trouble (or, worse, you won’t and it will look like it’s working).You can however re-assign the pointer, so something like
is perfectly OK.
The following would be legal though (as an alternative to allocating dynamic memory with
newormalloc):