Is using an invalid pointer value legal in C?
The following code is undefined behavior in C++ (although i开发者_如何转开发t will work okay on almost any widely used implementation):
int* pointer; //uninitialized - likely illegal pointer value
pointer++; //incrementing an illegal pointer is UB
Is the above code legal in C?
It's undefined behavior in C as well because on certain architectures, loading an invalid pointer into a register triggers a hardware fault.
See Is storing an invalid pointer automatically undefined behavior?
It is undefined behavior in C99. The value of pointer
is "indeterminate" (6.7.8.10) and an indeterminate value can be a trap value that causes undefinedness when used.
Not legal. Code like this will compile, but with warnings. Don't ignore them. Don't write code like this. It can affect your system in many not so nice ways. My university teacher once told us he managed to erase one machine's BIOS using code with undefined behaviour.
精彩评论