volatile const int foo

M

Mantorok Redgormor

strictly speaking we can't say that const
means "read only" but instead that it means
"do not modify me please" since an object
can be both modifiable and not modifiable
or volatile and const, right?

and if an implementation chose to put
things that are const in read only memory
if that const thing was also volatile
then in that case, it would not be in
read only memory, correct?
 
M

Martin Dickopp

strictly speaking we can't say that const means "read only" but
instead that it means "do not modify me please" since an object can be
both modifiable and not modifiable or volatile and const, right?

Not sure what you mean by that. If you have something like

const int i = 5;

then every attempt to modify `i' is an error. OTOH, if you have

int i = 5;
const int *ip = &i;

you can modify `i' directly, but it's an error to modify it through `ip'
(e.g. by assigning to `*ip').
and if an implementation chose to put things that are const in read
only memory if that const thing was also volatile then in that case,
it would not be in read only memory, correct?

Not necessarily. If you have

const volatile int i = 5;

the implementation could still put `i' in read only memory. Perhaps a
better example is

const volatile int *ip = some_system_specific_initialization ();

It would still be an error to modify `*ip' in any way, but the
implementation would have to assume that the object pointed to can be
changed by something beyond its control. Imagine, for example, that
`some_system_specific_initialization ()' returns the address of a
hardware timer, which is incremented regularly by the hardware, but
cannot be changed by the C program.

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top