Null pointer check elimination

Jeff Law law@redhat.com
Wed Mar 9 05:34:00 GMT 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 03/08/11 22:23, richardcavell@mail.com wrote:
> 
> foo (char *x)
> {
>  char ret = *x;
> 
>  if (x)
>    com();
>  else
>    bar();
>  return ret;
> }
> 
>> It is expected that the compiler will eliminate the pointer check with 
> the option '-fdelete-null-pointer-checks' or with >the option -O2, -O3,
> or -Os.
> 
> Hang on, why would it eliminate the null pointer check, if the null
> pointer check is in the source code?  Are you assuming that the compiler
> knows for certain that x is non zero when it compiles the function?
X was dereferenced in the initialization of ret.  Dereferencing NULL is
not a ANSI/ISO confirming program and the compiler is free to optimize
away any checks of X against NULL that are dominated by the dereference
of X.

jeff
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJNdxFIAAoJEBRtltQi2kC7SWIIAKPE/8CH70og472RvWy5oACZ
FyjxiYyPJJqwpd1TPwap0eAjrEPubXwTWCo3RDpfFlAeDTR5kTWrZVjh2isqgdsd
zYQwwvLDtQXCgBNBocEzRU+1ldXrq3evW92KndxueWG/e3QvCHYHrJD+Alg4jPZt
brHLFq5ElscxCcaRxeXpgl3zRZ9KwdgDST7zciBMFlwCknAKpQROg9iYAH7W6TL0
/DDjjyHL+BG4Xw4I3E1aYyDQbf4jE32TufFpXIcqDVh8MolmuE3ziijmwmYcDXxj
1QB2HWnFTsDEj6O7UuX4eomuczRfsfTFfmxro5SXToaWzabFW55KZrXlmEd9xtE=
=QFJH
-----END PGP SIGNATURE-----



More information about the Gcc-help mailing list