public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Brown <david.brown@hesbynett.no>
To: gcc@gcc.gnu.org
Cc: David Brown <david@westcontrol.com>, gcc@gcc.gnu.org
Subject: Re: Volatile qualification on pointer and data
Date: Sun, 25 Sep 2011 16:36:00 -0000	[thread overview]
Message-ID: <4E7F5185.3030101@hesbynett.no> (raw)
Message-ID: <20110925163600.BvZiLmeh952VYzspPieJjDHovNGRts4v8dj8kA53IyA@z> (raw)
In-Reply-To: <m28vpcemhr.fsf@igel.home>

On 25/09/11 16:16, Andreas Schwab wrote:
> David Brown<david@westcontrol.com>  writes:
>
>> There is a big difference between defining an object as "const", and
>> merely declaring it as const or accessing it as const.  When you access it
>> as const, you are saying "/I/ won't change the object with this access".
>> When you declare an object as const (such as an extern object), you are
>> saying "/I/ won't change this object".  When you /define/ an object as
>> const, as you do with a "static const", you are saying "this object is
>> constant.  It will never change value - you (the toolchain) can safely
>> place it in read-only memory that cannot ever change value".
>
> You are interpreting too much into const here.  The C standard says in
> footnote 112: "The implementation may place a const object that is not
> volatile in a read-only region of storage."  Note the use of "not
> volatile".  But in any case this is not part of the C standard: it's
> only a footnote.
>

I stand corrected on that point - thanks.

>> And then you make it volatile, telling the compiler "this object might
>> change unexpectedly, or use values written to it unexpectedly".
>>
>> If someone could explain to me how this could have real-world usage, I
>> think it would be easier for me (and others) to be sure of what it really
>> means.
>
> It's an object that is never written to, but may change unexpectedly.
>

Again, I can see that "volatile const" is useful for declaring external 
objects (or accesses to objects), but not when the address of the object 
is allocated by the toolchain (as is the case here with a "static const 
volatile".

The only scenario I have heard where this might be useful is for 
debugging - making what is normally a "static const" volatile so that 
it's value can be changed with a debugger.  Personally, I think that in 
such cases you would be better removing the "const" when you add the 
"volatile".


  reply	other threads:[~2011-09-25 16:07 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-20 16:08 Paulo J. Matos
2011-09-20 16:36 ` Ian Lance Taylor
2011-09-21  7:07   ` David Brown
2011-09-21  8:22     ` Paulo J. Matos
2011-09-21 10:20       ` David Brown
2011-09-21 13:57         ` Ian Lance Taylor
2011-09-21 14:25           ` David Brown
2011-09-21 14:57             ` Paulo J. Matos
2011-09-22  8:39               ` David Brown
2011-09-22 21:15               ` Richard Guenther
2011-09-23 11:33                 ` Paulo J. Matos
2011-09-23 11:51                   ` Paulo J. Matos
2011-09-23 13:17                 ` Paulo J. Matos
2011-09-21 18:51             ` Georg-Johann Lay
2011-09-22  8:53               ` David Brown
2011-09-24 15:10                 ` John Regehr
2011-09-24 15:49                   ` David Brown
2011-09-24 16:26                     ` David Brown
2011-09-24 19:38                     ` John Regehr
2011-09-25 13:03                       ` David Brown
2011-09-25 15:15                         ` Andreas Schwab
2011-09-25 16:33                           ` David Brown [this message]
2011-09-25 16:36                             ` David Brown
2011-09-25 16:06                         ` Dave Korn
2011-09-25 22:05                           ` David Brown
2011-09-25 22:05                             ` David Brown
2011-09-26  7:14                             ` Miles Bader
2011-09-26  8:53                               ` David Brown
2011-09-26  8:58                                 ` David Brown
2011-09-21  8:14   ` Paulo J. Matos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4E7F5185.3030101@hesbynett.no \
    --to=david.brown@hesbynett.no \
    --cc=david@westcontrol.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).