Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.
7683 Discussions

How to create a segmentation fault ?

romant73
Beginner
135 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
Black Belt
135 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
romant73
Beginner
135 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).
jimdempseyatthecove
Black Belt
135 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
Reply