This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Optimizing


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;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]