Exit code with no corresponding return statement

I have a program that, when run with the same parameters, will sometimes complete its task successfully and sometimes crash with code 3 instead. My code does not contain a "return 3;".

I did some research and found suggestions that this indicates failure to allocate memory. Where would I look to confirm this theory? What determines the exit code when a program crashes due to an unhandled exception?

IDE is MSVC2010 on Windows7
I did some research and found suggestions that this indicates failure to allocate memory. Where would I look to confirm this theory?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 // ...
#include <exception>
#include <iostream>
// ...

int main()
{
    try {
        // ...
    }
    catch (std::exception& ex)
    {
        std::cerr << "Exception encountered: " << ex.what() << '\n' ;
        throw;
    }
    catch(...)
    {
        std::cerr << "Unknown exception encountered.\n" ;
        throw;
    }
}
Thanks but can't I be lazy and look it up on some list like win system error codes?
can't I be lazy and look it up on some list like win system error codes?

That would be fine if the error was some unique number that was easily recognized as a Windows error. Unfortunately, 3 could come from any number of places.

cire's suggestion would work if your program or a library is throwing an exception.
I'm not so sure that's the case. If that were the case, you should see an "unhandled exception" message in the output window. Most run time implementations have the equivalent of what cire suggested.

I suspect that exit(3) is getting called somewhere. IMO, the best way to find this is to implement an atexit() callback. You register your callback function at the beginning of your program. The callback is called when exit() is called. With a debugger you can trace back the call to exit() and see where it is getting called.
http://www.cplusplus.com/reference/cstdlib/atexit/

Last edited on
Alright, will do. Thank you
Topic archived. No new replies allowed.