This is the mail archive of the gcc@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: Volatile qualification on pointer and data


On 25/09/2011 13:56, David Brown wrote:

> 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".
> 
> 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.

  Just because it's static doesn't mean the address can't escape:

/* May read or write to dest, according to direction flag.  */
extern void start_dma_xfer (void *dest, unsigned int size, bool direction,
                            uint64_t bus_addr);

/* We don't want to change this ourselves, and nor do we want the symbol
   to be externally visible.  */
static const volatile char *dma_buffer[BUFFERSIZE];

[ ... later, in a function ... ]

  start_dma_transfer (&dma_buffer[0], size, DIRECTION_DMA_TO_MEMORY, devaddr);


  A bit contrived perhaps, but start_dma_transfer (or some similar function)
might be part of the OS or written in assembly and so necessarily C-type-safe.

    cheers,
      DaveK


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