Reputation: 68738
On Linux, the following code:
long* p = (long*)reinterpret_cast<void*>(0x634963963496034) // random memory address
std::cout << *p;
will likely cause a read of memory that hasn't been allocated, and the program will raise a SIGSEGV signal. Otherwise known as a segfault. (An action can be attached via sigaction(2)
).
Is there an equivalent mechanism on Windows / MSVC ?
What will the above code do on Windows? Is there any way to trap this memory access violation, and run a user-provided function?
Upvotes: 2
Views: 1024
Reputation: 68738
What I was looking for was SetUnhandledExceptionFilter
. SetUnhandledExceptionFilter
on Windows is equivalent to sigaction
on Linux for this use case.
Upvotes: 0
Reputation: 25396
On Windows, when catching the exception using Structured Exception Handling, the macro GetExceptionCode() will return EXCEPTION_ACCESS_VIOLATION
.
It is also possible to use C++ exception handling to catch SEH exceptions, as described here.
Upvotes: 2