About Constant Variables

P

Prasad

Where are the const variables actually stored in memory. I mean If
they are stored in data segment(external & static variables)or stack
segment(local) how the compiler knows that it is read only memory.
 
J

Joona I Palaste

Prasad said:
Where are the const variables actually stored in memory. I mean If
they are stored in data segment(external & static variables)or stack
segment(local) how the compiler knows that it is read only memory.

It depends entirely on the implementation.
 
E

Emmanuel Delahaye

Prasad wrote on 10/08/04 :
Where are the const variables actually stored in memory. I mean If
they are stored in data segment(external & static variables)or stack
segment(local) how the compiler knows that it is read only memory.

Let's make things clear.

There are no 'Constant Variables' in C. A constant [expression] can't
be a variable. Its a datum that is not an object. It has no address.

A variable can have the 'const' qualifier. In that case, it is defined
read-only (not 'constant'). Being a variable, it's an object, and yes,
it has a memory location, hence an address.

The 'const' qualifier can be used by the linker to place the data in a
'read-only' region when possible.

The details of the memory mapping depends on the implementation. You
probably can ask your linker utility to generate the mapping file in
which you will find all the answers to your questions *for your
implementation of C*. Don't draw general conclusions from this mapping.
 
J

Jack Klein

Where are the const variables actually stored in memory. I mean If
they are stored in data segment(external & static variables)or stack
segment(local) how the compiler knows that it is read only memory.

Constant objects are stored in memory wherever a particular
implementation decides to store them, if they are stored at all. If
an initialized constant object has internal linkage or no linkage, and
its address is not taken, it might not be stored anywhere, the
compiler may merely substitute its initialization value when it is
used.

Not all implementations even have read only memory.
 
J

Jack Klein

Prasad wrote on 10/08/04 :
Where are the const variables actually stored in memory. I mean If
they are stored in data segment(external & static variables)or stack
segment(local) how the compiler knows that it is read only memory.

Let's make things clear.

There are no 'Constant Variables' in C. A constant [expression] can't
be a variable. Its a datum that is not an object. It has no address.

There are no 'variables' in C, the term is not defined by the
language, although it is used casually in a few places.
A variable can have the 'const' qualifier. In that case, it is defined

No, on object can have the 'const' qualifier. In that case, it is
defined as both read-only and constant. Attempting to modify a const
object produces undefined behavior, but there are systems where no
exception will happen and the only result is ... nothing. No change
to the object at all. That is certainly constant.
read-only (not 'constant'). Being a variable, it's an object, and yes,
it has a memory location, hence an address.

The 'const' qualifier can be used by the linker to place the data in a
'read-only' region when possible.

There is no requirement that an implementation place anything in a
read-only region, even if it has such a thing.
 
R

Rich Grise

Jack said:
Prasad wrote on 10/08/04 :
Where are the const variables actually stored in memory. I mean If
they are stored in data segment(external & static variables)or stack
segment(local) how the compiler knows that it is read only memory.

Let's make things clear.

There are no 'Constant Variables' in C. A constant [expression] can't
be a variable. Its a datum that is not an object. It has no address.

There are no 'variables' in C, the term is not defined by the
language, although it is used casually in a few places.
A variable can have the 'const' qualifier. In that case, it is defined
Well, then, what do you call those things where you can put a value
and refer to them by name? I've apparently been wrong all lo these many
years.

Thanks,
Rich
 
D

Dan Pop

Whereever the implementation wants to.

The compiler couldn't care less. It could allocate them together with
ordinary variables, in a writable memory segment. If you change their
value behind the compiler's back, you deserve *anything* you get.
Furthermore, the automatically allocated ones may have their values
computed at run time, so the compiler cannot allocate them in read only
memory.
Let's make things clear.

There are no 'Constant Variables' in C. A constant [expression] can't
be a variable. Its a datum that is not an object. It has no address.

There are no 'variables' in C, the term is not defined by the
language, although it is used casually in a few places.
A variable can have the 'const' qualifier. In that case, it is defined
Well, then, what do you call those things where you can put a value
and refer to them by name? I've apparently been wrong all lo these many
years.

If the value can be changed, they're usually called variables, whether
Jack Klein likes it or not. Even the C standard calls them like this.

OTOH, if the value cannot be changed, the term "constant variable" is the
canonical example of an oxymoron. Unfortunately, the obvious name for
them, "constants", does not really reflect their semantics in a C program,
as they are (named) read-only objects, rather than genuine constants, i.e.
they cannot be used in constant expressions.

Dan
 

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
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top