473,387 Members | 1,892 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,387 software developers and data experts.

'const' keyword problem

Hi All,
Where is an identifier with a 'const' keyword given physical space in
RAM ?
Is it on stack or heap or data segment depending on storage class or is
it treated as a part of code ,so kept in code segment?
What is the case with #define statements?

Nov 15 '05 #1
9 1897
On 1 Aug 2005 21:19:16 -0700, "siliconwafer" <sp*********@yahoo.com>
wrote in comp.lang.c:
Hi All,
Where is an identifier with a 'const' keyword given physical space in
RAM ?
First, identifiers don't need physical space anywhere, anytime. They
are a source code, compiler, and linker thing. They do not exist at
runtime. Now if the identifier is used to name a defined object, than
that object might or might not need some space somewhere.

Who says it is in RAM at all? On some platforms it will wind up in
ROM or EPROM or some other type of physically read only memory. In
many cases, if its address is never taken and it does not have
external linkage, it will not occupy any memory at all. The compiler
will merely substitute its initial value directly into any code that
uses the value.
Is it on stack or heap or data segment depending on storage class or is
C does not define things like "stack" or "heap" or "data segment". It
defines the characteristics of an abstract machine that has "storage",
without even specifying what that is. In face, in modern
multi-tasking desktop operating systems, it is quite possible that
your const object might be living on a hard drive in a swap file while
some other program is executing and yours is suspended.
it treated as a part of code ,so kept in code segment?
Again, the C language does not specify such details, leaving them up
to the implementation. Your const object, if it occupies storage at
all, is in whatever area of storage the writers of your compiler
decided it should occupy.
What is the case with #define statements?


Macros are handled by the preprocessor, a separate phase of the
compilation process that runs before the compiler proper. It works
primarily by the equivalent of simple text substitution. The compiler
proper never sees the macro identifier, only its replacement.

--
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 15 '05 #2
In article <11**********************@g14g2000cwa.googlegroups .com>
siliconwafer <sp*********@yahoo.com> wrote:
Where is an identifier with a 'const' keyword given physical space in
RAM ?
Is it on stack or heap or data segment depending on storage class or is
it treated as a part of code ,so kept in code segment?
Yes, it is on a stack or heap or data segment or kept in the code
segment. Or perhaps in a register. Or maybe written down on one
of the Dead Sea Scrolls. The C standard makes no requirements
beyond those of "behavior observable within a strictly conforming
program".

In practice, most C compilers compile "const T var = value" as if
the "const" qualifier were not present, with a few exceptions:

- If the item would go in read/write space, it *may* go in
read-only space.

- Later occurrences of the named object *may* be replaced with
an in-line expansion of the (presumed) value of the variable,
on the assumption that a correct program cannot change the
value of a "const"-qualified object.

Note, however, that "const" is a type *qualifier*, not a storage
class specifier -- you can write things like "int *const p = &other;"
to make p itself read-only but *p read/write, or "const int *p;"
to make p itself read/write but *p read-only.
What is the case with #define statements?


"#define"d identifiers are expanded textually during phase 4
of compilation, and preprocessor directive lines are not statements.
Hence:

#include <stdio.h>
#include <stdlib.h>

#define HELLO if (
#define THERE {

int main(int argc, char **argv) THERE
HELLO argc < 2 ) THERE
fprintf(stderr, "need arguments\n");
exit(1);
}
printf("thanks.\n");
return 0;
}

is valid (albeit useless and strange) C code. The "#define"d
identifiers are replaced before "compilation proper" (which occurs
in Translation Phase 7).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 15 '05 #3
siliconwafer wrote:
Hi All,
Where is an identifier with a 'const' keyword given physical space in
RAM ?
Is it on stack or heap or data segment depending on storage class or is
it treated as a part of code ,so kept in code segment?
Any, all, or none of those. The compiler can do what it like or even
under some circumstances if it wants completely eliminate it.

There is also no reason for you to nead to know the answer.
What is the case with #define statements?


#defines are textual replacements in the source that occur during
compilation. In some cases there may not be anything to store.
#define FRED int
FRED i;
What has to be stored for FRED? Nothing. Although i requires storage.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #4
siliconwafer wrote:
Hi All,
Where is an identifier with a 'const' keyword given physical space in
RAM ?
Is it on stack or heap or data segment depending on storage class or is
it treated as a part of code ,so kept in code segment?
What is the case with #define statements?


The answer for the first would be a question:
Where is a const variable stored in system x using compiler y?
--
one's freedom stops where other's begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #5
The reason for above question is b'coz I did somthing like this:
const static float c = 5;
.....

c = some expression;

c++;
.....
I used gcc(2.95.1) to compile the above code.It gave me a warning
saying its only a "read only variable".
I neglected the warning and executed the program.
It gave me a *segmentation fault* error and exited.
I thought that the warning is b'coz const object is part of *code
segment * and static objects must be on *data segment*.So there is an
ambiguity as to which segment to refer to.
So what is the reason for *segmentation fault*?

Nov 15 '05 #6
"siliconwafer" <sp*********@yahoo.com> writes:
The reason for above question is b'coz I did somthing like this: const static float c = 5;

c = some expression;

c++; I used gcc(2.95.1) to compile the above code.It gave me a warning
saying its only a "read only variable".
You should pay great attention to the warnings of the compiler :-)
I neglected the warning and executed the program.
It gave me a *segmentation fault* error and exited.
This is a good reason why.
I thought that the warning is b'coz const object is part of *code
segment * and static objects must be on *data segment*.So there is an
ambiguity as to which segment to refer to.
So what is the reason for *segmentation fault*?


This particular behavior is platform and implementation-dependent.

On some implementations or under particular circumstances, you *may* be
able to modify the value of an object through an identifier declared as
``const''. This is not something you should depend on, though.

Nov 15 '05 #7
siliconwafer wrote:
Hi All,
Where is an identifier with a 'const' keyword given physical space in
RAM ?
Is it on stack or heap or data segment depending on storage class or
is it treated as a part of code ,so kept in code segment?
What is the case with #define statements?

Why to do you care? Is this a quest for abstract knowledge, or are you
trying to do something for which you think this will be important?


Brian
Nov 15 '05 #8
In article <11**********************@o13g2000cwo.googlegroups .com>, siliconwafer wrote:
The reason for above question is b'coz I did somthing like this:
const static float c = 5;
....

c = some expression;

c++;
....
I used gcc(2.95.1) to compile the above code.It gave me a warning
saying its only a "read only variable".
I neglected the warning and executed the program.
It gave me a *segmentation fault* error and exited.
I thought that the warning is b'coz const object is part of *code
segment * and static objects must be on *data segment*.So there is an
ambiguity as to which segment to refer to.
So what is the reason for *segmentation fault*?


AFAIK gcc for i386 normally produces tiny mode code DS=CS=ES=FS=GS and
pointers are 'near' ie 32 bit offset only

The cuase of the segmentation fault is that the memory page holding the
constant was not writable

Bye.
Jasen
Nov 15 '05 #9

Jasen Betts wrote:
In article <11**********************@o13g2000cwo.googlegroups .com>, siliconwafer wrote:
The reason for above question is b'coz I did somthing like this:
const static float c = 5;
....

c = some expression;

c++;
....
I used gcc(2.95.1) to compile the above code.It gave me a warning
saying its only a "read only variable".
I neglected the warning and executed the program.
It gave me a *segmentation fault* error and exited.
I thought that the warning is b'coz const object is part of *code
segment * and static objects must be on *data segment*.So there is an
ambiguity as to which segment to refer to.
So what is the reason for *segmentation fault*?


AFAIK gcc for i386 normally produces tiny mode code DS=CS=ES=FS=GS and
pointers are 'near' ie 32 bit offset only

The cuase of the segmentation fault is that the memory page holding the
constant was not writable

Bye.
Jasen


Hi once more,

who takes the responsiblity for maintaining the 'const' variable a
'read only' variable?
Is it the compiler ? So when such a variable is mapped to physical
memory location,that location is maintained as 'read only' location
even if its a RAM location.OR being a RAM location,it can be *written*
even if its meant for a const variable or a readf only variable?

sorry for some trouble.
but help required.
Siliconwadfer

Nov 15 '05 #10

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

Similar topics

2
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this...
2
by: Lorenzo Castelli | last post by:
This is an old problem of mine. Basically I have an abstract base class which represents a generic iterator over a collection of elements, and various derived classes that implement the...
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
17
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer...
23
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
29
by: Rohit kumar Chandel | last post by:
Hi all, I have a doubt in const keyword. My understanding says that a variable declared const can not be modified by the module it is defined in. Then consider the following code segment:...
6
by: Gestorm | last post by:
Hi everyone, I have a problem. If I declare a struct with a const member, what will happen?For example: if I declared a struct like following: struct{ const int a; char c; }aStruct; then such...
9
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen...
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.