473,830 Members | 2,045 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'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 1917
On 1 Aug 2005 21:19:16 -0700, "siliconwaf er" <sp*********@ya hoo.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #2
In article <11************ **********@g14g 2000cwa.googleg roups.com>
siliconwafer <sp*********@ya hoo.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 "compilatio n 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
"siliconwaf er" <sp*********@ya hoo.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************ **********@o13g 2000cwo.googleg roups.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************ **********@o13g 2000cwo.googleg roups.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
2503
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 section to flash memory and that would be OK. I have a structure with one member of it being const char** array;
2
2020
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 traversing on specific data structures. For each iterator I want to be able to specify the four possible const combinations, corresponding to the various void* for pointers, with the possibility to perform conversions from non-const to const just like...
20
2480
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 doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const liberally and then later goes back and changes the functions, headaches will inevitably occur as...
4
6699
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 = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
17
2450
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 and then dereferencing that pointer. Should the dereferenced pointer have the same constness of the pointer as the pointer has the same constness as the class object? Yes, I am aware that C++ does not do this. I just want to know everyones...
23
2343
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
1877
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: main() { const int p =5; int* pp = &p; ++(*pp);
6
1796
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 statement as aStruct.a = 0; is illegal.
9
2064
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 below, you can use the 'const' keyword in the function / method declaration. But how to do this in C#? *for example: "void Foo() const;"
0
10206
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9315
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7746
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5617
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.