Disable optimizations on one function (was: 'pragma optimize' ...)

Andrew Haley aph@redhat.com
Thu Jul 16 09:00:00 GMT 2015

On 16/07/15 06:58, Jeffrey Walton wrote:
> Many folks try and cast ptr to volatile, but that's an abuse because
> GCC considers volatile something for memory mapped hardware. Volatile
> should not be used in an attempt to tame the optimizer.

GCC does not consider volatile to be something for memory mapped
hardware.  Volatile objects are simply those which may be modified in
ways unknown to the implementation or have other unknown side effects.
In other words, volatile is exactly the right thing use in the
ClearAndFree case.  But passing a volatile pointer to memset() is
pointless because the volatile qualifier will be discarded at the
point of call: you'll have to write the memory yourself.

If you don't mind being nonportable, a memoryclobber should do it:

    memset(ptr, 0x00, n);
    asm volatile("": : : "memory");

or even:

    memset(ptr, 0x00, n);


More information about the Gcc-help mailing list