How to free memory in try-catch blocks?
- by Kra
Hi,
I have a simple question hopefully - how does one free memory which was allocated in the try block when the exception occurs? Consider the following code:
try
{
char *heap = new char [50];
//let exception occur here
delete[] heap;
}
catch (...)
{
cout << "Error, leaving function now";
//delete[] heap; doesn't work of course, heap is unknown to compiler
return 1;
}
How can I free memory after the heap was allocated and exception occurred before calling delete[] heap? Is there a rule not to allocate memory on heap in these try .. catch blocks?
Thanks