How can I prevent the following code crashing my program?
::MessageBox(NULL, L"before", NULL, MB_OK);
void* x = ::calloc(1, -1);
::MessageBox(NULL, L"after", NULL, MB_OK);
When I run this code I get the “before” message box, then the program crashes with an “unhandled win32 exception” (the “after” message box never appears).
I know the args to calloc are making it crash by asking for way too much memory! What I want to know is how to prevent the crash so that I get control back and can handle the lack of memory some other way?
(Windows XP, Visual Studio 2005)
Looks like this is a bug in Microsoft calloc (it’s supposed to return null, not crash):
http://connect.microsoft.com/VisualStudio/feedback/details/356599/calloc-crashes-if-total-size-heap-maxreq
So you need to use a newer version of their C runtime.