Re: const volatile

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 26/09/18 14:54, Kalamatee wrote:
> Hi
> 
> I am wondering if there is a reason globals marked const volatile are put
> into the data section and not read-only as const should imply?
> 

"const" says /you/ promise not to change the value from your code.
"volatile" says that something else might change its value unknown to
the compiler.

It is rare to define "const volatile" variables.  It is usually more
useful to declare pointers to them.  They are used for things like
read-only hardware registers in microcontrollers, or for data that is
set from outside a program but is constant from within it (perhaps you
patch your binaries with a serial number, or a checksum - you could use
a pointer to const volatile for reading the number in the code).

The only real use of defined "const volatile" variables I have had is
for debugging - these would be variables that cannot be changed by the
code, but you might change them via a debugger.  Usually such things are
temporary during software development, and it's easier just to make them
normal volatile variables.

> I am trying to compile code for m68k AmigaOS which is resident using the
> mathlib functions which need certain constants as volatile to prevent
> incorrect sequence re-ordering in gcc's optimizations - however doing so
> puts the value in bss data without const.
> 

That is almost certainly not the best way to get the sequencing you
want.  Post more details on what you are trying to do, and I am sure
someone can give you advice.

> How can we have a global that is both volatile (to prevent incorrect
> sequencing) and const (so it is read only and stored in read only data
> section) ?

Why does it matter where the data is stored?

> 
> Thanks for any insight that may be provided.
> 




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux