From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10203 invoked by alias); 5 Mar 2013 09:28:03 -0000 Received: (qmail 10033 invoked by uid 22791); 5 Mar 2013 09:28:01 -0000 X-SWARE-Spam-Status: No, hits=-4.3 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,KHOP_RCVD_TRUST,KHOP_THREADED,RCVD_IN_DNSWL_LOW,RCVD_IN_HOSTKARMA_YE X-Spam-Check-By: sourceware.org Received: from mail-wg0-f54.google.com (HELO mail-wg0-f54.google.com) (74.125.82.54) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 05 Mar 2013 09:27:57 +0000 Received: by mail-wg0-f54.google.com with SMTP id fm10so5213282wgb.21 for ; Tue, 05 Mar 2013 01:27:56 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.194.121.6 with SMTP id lg6mr24541489wjb.22.1362475675944; Tue, 05 Mar 2013 01:27:55 -0800 (PST) Received: by 10.194.80.36 with HTTP; Tue, 5 Mar 2013 01:27:55 -0800 (PST) In-Reply-To: References: Date: Tue, 05 Mar 2013 09:28:00 -0000 Message-ID: Subject: Re: Question on volatile functions and GCC From: David Paterson To: noloader@gmail.com Cc: gcc-help@gcc.gnu.org Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2013-03/txt/msg00029.txt.bz2 On 4 March 2013 23:40, Jeffrey Walton 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.