public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Null pointer dereference and side effects
@ 2004-07-18 19:30 Archie Cobbs
  2004-07-19 12:37 ` Eljay Love-Jensen
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Archie Cobbs @ 2004-07-18 19:30 UTC (permalink / raw)
  To: gcc-help

I have a question about GCC optimizations and null pointers.
Consider this program:

    #include <stdlib.h>

    int
    main(int ac, char **av)
    {
	    int *r1;
	    int *r2;

	    r1 = &ac;
	    *r1 = 0;
	    r2 = (void *)0;	/* or (void *)1, etc. */
	    if (*r1 != *r2)
		    return 1;
	    return 1;
    }

When run & compiled with gcc -O2 (gcc 3.2.2) it does not segfault.
Clearly this is because the optimizer realizes that *r1 need not be
read in order to know that the function should return 1.

However, in my application I'm relying on the side effects of
dereferencing a null pointer (ie., SEGV signal), so that causes
bugs for me.

I understand that according to the C standard dereferencing a null
pointer results in "undefined" behavior, so gcc is not behaving
incorrectly.

However, is there some way to get the desired semantics? It appears
omitting the -O2 does it, but I'd prefer to not do that.

Thanks,
-Archie

__________________________________________________________________________
Archie Cobbs      *        CTO, Awarix        *      http://www.awarix.com

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2004-07-22 20:24 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-18 19:30 Null pointer dereference and side effects Archie Cobbs
2004-07-19 12:37 ` Eljay Love-Jensen
2004-07-19 14:30   ` Archie Cobbs
2004-07-19 15:28 ` Jeffrey A Law
2004-07-19 15:45   ` Archie Cobbs
2004-07-19 16:04     ` Eljay Love-Jensen
2004-07-20 15:30       ` Archie Cobbs
2004-07-20  9:55 ` Alexandre Oliva
2004-07-20 15:15   ` Archie Cobbs
2004-07-21  6:44     ` Alexandre Oliva
2004-07-21 11:33       ` Eljay Love-Jensen
2004-07-22 20:24         ` Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).