I am using bcc32 command line compiler from Borland Embarcadero. Consider this program:
int main(int, char **)
{
try
{
std::string *a = new string(0xf0000000, ' ');
...
delete a;
}
catch(const std::bad_alloc &)
{
...
}
}
When the std::string constructor throws a memory exception, the stack is unwound and control is passed to the catch-block. Gnu compilers build in code to delete the memory allocated for the std::string object 'auto-magically', as was stated by someone who commented on the answer in Who deletes the memory allocated during a "new" operation which has exception in constructor? which I wrote. I ran the program in http://ideone.com/IRxHX and the result is that nobody frees the memory allocated by 'operator new' if an exception is thrown before the result of 'new' is stored in an lvalue. In the above case the variable 'a'.
Questions are: 1 Is there a way to delete the memory generated by 'new' in case of an exception, as a part of the stack unwind procedure? 2 What does the C++ standard demand from compilers in this case