Question on volatile functions and GCC

David Paterson dnpaterson@gmail.com
Tue Mar 5 09:28:00 GMT 2013


On 4 March 2013 23:40, Jeffrey Walton <noloader@gmail.com> wrote:
> Hi All,
>
> I was looking at some slides on OpenSSL and secure memory wiping using
> volatile (Slide 36 at
> http://www.slideshare.net/guanzhi/crypto-with-openssl).
>
> I believe GCC's interpretation of the use for 'volatile' is memory
> mapped hardware.

In addition to Jonathan's answer on the use of "volatile", it's worth adding
that it's not only used for memory mapped hardware. There are many other
uses, such as inter-thread communication, or indeed the example you
show below.

> I think Ian stated it for me some time ago when I was
> trying to understand different interpretations among compilers. If
> volatile is for memory mapped hardware, why does GCC compile the
> following:
>
> volatile void clean_memory(volatile void* dest, size_t len)
> {
>   volatile unsigned char* p;
>   for(p = (volatile unsigned char*)dest; len; dest[--len] = 0)
>     ;;
> }

In this case, the use of "volatile" prevents an over-eager optimiser from
discarding this function completely, since it could assume that it does
nothing useful.

Regards,

David P.



More information about the Gcc-help mailing list