Optimizing

Kalle Olavi Niemitalo kon@iki.fi
Sun Jan 30 20:24:00 GMT 2011


Magnus Fromreide <magfr@lysator.liu.se> writes:

> Is it a valid optimization for the compiler to generate the same
> instructions for f2 as it generates for f1?

It isn't.

(1) foo could modify the data:

int foo(int writable, const void *p, int size)
{
        if (writable) {
                memset((void *) p, 0, size);
	}
        return 0;
}

Although the second parameter is const void *, the language still
allows the function to modify the object to which that points,
provided that the object was not defined as const.  This is okay
with f2 but not with f1.

(2) In f2, buf must have a different address at each level of recursion:

int foo(int flag, const void *p, int size)
{
        static const void *outer = NULL;
        if (outer == NULL) {
		outer = p;
        	f2();
                outer = NULL;
	} else {
		assert(p != outer);
	}
        return 0;
}



More information about the Gcc-help mailing list