Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7944 Discussions

How to create a segmentation fault ?

romant73
Beginner
329 Views
Hi,

I have a problem - it is necessary to simulate a segmentation fault in the code in order to make the application crash, however, it does not crash when compiled with Intel Compiler :-)

Code like this:
int* p = 0;
*p = 10;

or this:
puts((const char*)(void*)((unsigned long long)-1));

crashes the application perfectly when it is compiled with MS compiler, however, no crash occurs and some sort of exception is generated when it is compiled with Intel.

How to knowingly make the Intel-compiled code crash in any manner ?
0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
329 Views
The compiler optimizations may have seen that your program did not use *p and thus removed it.

Try this:

int* p_CrashTheProgram = 0;

__declspec(noinline)
void CrashTheProgram(int info=0)
{
*p_CrashTheProgram = info;)
}

Jim Dempsey
0 Kudos
romant73
Beginner
329 Views
Jim,

I have checked - my code is not optimized out, it works. I have also tried your solution out with same result - no crash, some exception generated (it is caught in catch (...) block).
0 Kudos
jimdempseyatthecove
Honored Contributor III
329 Views
Your first post did not indicate you were using exception handling. With exception handling enabled, your program does "crash", but then "recovers" under an exception condition.

With exception handling enabled, it is your responsibility to add a "catch" for segmentation fault (in this case a write to write protected/or non-mapped memory).

Jim Dempsey
0 Kudos
Reply