GCC optimization re-order statements

Segher Boessenkool segher@kernel.crashing.org
Sat Jun 12 01:45:38 GMT 2021


On Fri, Jun 11, 2021 at 01:56:40PM +0200, David Brown wrote:
> If the compiler knows that on the target in question, it is perfectly
> safe (i.e., no signals or anything else) to dereference a null pointer,
> then in your original "f" the compiler could read *p before it tests p
> for null, but it could not do the printf before the test.  (Sometimes
> for embedded systems the compiler knows that reading via a null pointer
> is safe.)

But note that this is undefined behaviour in standard C (a null pointer
is required to compare unequal to any pointer to any object or function
(6.3.2.3/3), and the indirection operator on any operand that does not
point to a function or object is undefined behaviour (6.5.3.2/4)).

It can be extremely useful to do support this, of course :-)


Segher


More information about the Gcc-help mailing list