GCC 3.4 optimisation of if (&ptr == NULL)

John Neil jneil@atrove.com
Thu Dec 4 01:44:00 GMT 2003


I recently spent time upgrading my embedded application form gcc 3.2 to the
current cvs version of gcc 3.4.  Whilst doing this I found that gcc 3.4 was
removing the "if (&ptr == NULL) as shown in the following test case

extern void* g_ptr; 

int test()
{
    if (&g_ptr == 0)
        return 1;
    else
        return 0;
}

Always returns 0 which can be seen in the generated assembler 
Generaged with (m68k-rtems-gcc).

NO_APP
	.file	"test.i"
	.text
	.align	2
	.globl	test
	.type	test, @function
test:
	link.w %a6,#0
	clr.l %d0
	unlk %a6
	rts
	.size	test, .-test
	.ident	"GCC: (GNU) 3.4 20031125 (experimental)"

Now I would agree normally that the address of a variable cannot be zero,
however in the embedded environment this technique is used to pass
information from the linker file to the C application, as was the case with
my application.  In this case the address can indeed be zero.  I tried
various different levels of optimization to no avail, so I was wondering if
anybody could shed some light on this change

Cheers
John






More information about the Gcc mailing list