In the following code, *(long*)0=0;
is used along with the if
clause, but what is its purpose?
if(r.wid*r.ht < tot)
*(long*)0=0;
See Question&Answers more detail:osIn the following code, *(long*)0=0;
is used along with the if
clause, but what is its purpose?
if(r.wid*r.ht < tot)
*(long*)0=0;
See Question&Answers more detail:osIt writes 0 to 0 interpreted as the address of a long
, i.e. the NULL
pointer. It's not a valid thing to be doing, since NULL
is never an address at which you can validly have data that your program can access. This code triggers undefined behavior; you cannot rely on it to have any particular effect, in general.
However, often code like this is used to force a segmentation fault-type crash, which is sometimes handy to drop into a debugger.
Again, this is undefined behavior; there is no guarantee that it will cause such a fault, but on systems that have segmentation faults, the above code is pretty likely to generate one. On other systems it might do something completely different.
If you get a segfault, it's sometimes more convenient to trigger one this way than by manually setting a breakpoint in the debugger. For instance if you're not using an IDE, it's often easier to type those few tokens into the code in the desired place, than it is to give the (textual) command to the debugger, specifying the exact source code file and line number manually can be a bit annoying.