I was reading Moai source code, and I became curious about why this should cause a crash (or not…)
I do not really understood that snippet.
In file A:
#define UNUSED(p) (( void )p)
In file B:
//----------------------------------------------------------------//
/** @name crash
@text Crashes Moai with a null pointer dereference.
@out nil
*/
int MOAISim::_crash ( lua_State* L ) {
UNUSED(L);
int *p = NULL;
(*p) = 0;
return 0;
}
EDIT:
I think part of what I was not understanding is what “deference” means. So if you put that in your answers it would be awesome.
The crash is caused by the dereference of the null pointer:
Also as pointed out in the comments, the UNUSED macros is only there to suppress the “unused parameter” warning that most compilers will give.
It is usually also possible to prevent the warning by, simply, not specifying the variable name as follows:
It’s also worth bearing in mind that the above is not a guaranteed crash. On one of the 32-bit consoles de-referencing a null pointer actually resulted in the number “3”. This did make null dereferences quite hard to find, but generally if you saw a 3 sitting around in a register you could hazard a good guess as to what had just gone wrong.
Dereferencing is essentially asking for the value stored at a given pointer. If the pointer is not valid (that is, pointing at a memory location that the process does not own) then it results in a crash. In Windows this is called an Access Violation (0xC0000005). Under Linux it’s a Segmentation Violation, SIGSEGV.
See also