473,385 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

About Constant Variables

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.
Nov 14 '05 #1
6 12330
Prasad <ma*******@indiya.com> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am lying."
- Anon
Nov 14 '05 #2
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.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #3
On 10 Aug 2004 02:26:51 -0700, ma*******@indiya.com (Prasad) wrote in
comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
On Tue, 10 Aug 2004 12:27:38 +0200, Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote in comp.lang.c:
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.
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.


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #5
Jack Klein wrote:
On Tue, 10 Aug 2004 12:27:38 +0200, Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote in comp.lang.c:
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

Nov 14 '05 #6
In <Ev*****************@nwrddc04.gnilink.net> Rich Grise <nu**@example.net> writes:
Jack Klein wrote:
On Tue, 10 Aug 2004 12:27:38 +0200, Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote in comp.lang.c:
Prasad wrote on 10/08/04 :
> Where are the const variables actually stored in memory. I mean If
Whereever the implementation wants to.
> they are stored in data segment(external & static variables)or stack
> segment(local) how the compiler knows that it is read only memory.
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: George Thorogood | last post by:
Hello all. When I do a print_r of a domelement object I get something like this: domelement Object ( => 1 => category => 6 => 1007543104 )
5
by: b-blochl | last post by:
I wonder about the huge traffic in the question how many tuples are dispensible. I find that a question of only ternary or quarterny order - use it or use it not (free after Shakespears "to be or...
13
by: Dave win | last post by:
howdy.... plz take a look at the following codes, and tell me the reason. 1 #define swap(a,b) a=a^b;b=b^a;a=a^b 2 3 int main(void){ 4 register int a=4; 5 register int b=5;...
5
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
4
by: Hans | last post by:
Hi, I want to define a couple of constant strings, like in C: #define mystring "This is my string" or using a const char construction. Is this really not possible in Python? Hans
3
by: mimi | last post by:
1.Instead of using macro, I would like to use static const variables in such situations. class Foo { public: static const int SOMEVALUEWITHFOO = 1; } int main() {
8
by: fabian.lim | last post by:
Hi, I have a question on constant variables. In the following code snippet, I have a function assign() that takes in an iterator to the private variable v, the number of stuff to assign (int n),...
7
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.