This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: telling GCC that a pointer cannot be NULL
- From: Ian Lance Taylor <iant at google dot com>
- To: Igor Bukanov <igor at mir2 dot org>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 10 May 2011 07:42:53 -0700
- Subject: Re: telling GCC that a pointer cannot be NULL
- References: <BANLkTimU35Ss1Rfc8CG0=AKur9zzj2C+_A@mail.gmail.com>
Igor Bukanov <igor@mir2.org> writes:
> is it possible to tell GCC that a pointer cannot be NULL at a
> particular point so the compiler can optimize the control flaw better
> and avoid redundant NULL checks on the fast paths?
As of gcc 4.5 you can do this using __builtin_unreachable.
In your example:
if (n != a->end) {
a->start += 16; /* size of the thing to allocate. */
if (n == 0)
__builtin_unreachable();
return n; /* n cannot be NULL here */
}
Use this with care. If you get it wrong, the results will be wholly
unpredictable.
Ian