Upon hitting Line 6, you just have a memory leak (harmless except wasted heap).
On Lines 5 and 6, there is no automatic delete of memory - there is no built-in gc() in C++ (and a decent gc() with correct reference counting still would not delete on Line 5).
I don't really understand why anyone would expect a crash. sohguanh's example shouldn't even compile since a and b are not in scope for the cout. Even if b was created before the first shown block, there would be no problem since the integer was never destroyed.
I ran this code through the VC++ debugger to see what would happen. All that happened was nothing...the program continued as normal after this code.
1
2
3
4
5
6
{
int* a = newint;
{
int* b = a;
}
}//Should explode here, right?
So my question is, why didn't my computer explode? (not physically, of course)
Show a complete example that compiles. The only thing that I see is a memory leak. a and b are destroyed automatically after the second, shown block so it would not be possible to use them incorrectly since they wouldn't exist.