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

Const Varible recognisation by C++ Compiler

Hi Folks,
I have one query in regarding const varible interpretaion by C/C++
compiler.
How Compiler actually differentiate between normal stack varible
and const varible during program execution?

Regards,
Sachin
Dec 21 '07 #1
3 1410
Sachin a écrit :
Hi Folks,
I have one query in regarding const varible interpretaion by C/C++
compiler.
How Compiler actually differentiate between normal stack varible
and const varible during program execution?
It doesn't. Constness is an additional relevant information for type
resolution and const correctness that is processed at compile time.
Additionnaly, the compiler may use the const qualification to put static
data into a RO area of the objet bytecode or to perform optimisations
but that's all.

Michael
Dec 21 '07 #2
On Dec 21, 7:08 am, Michael DOUBEZ <michael.dou...@free.frwrote:
Sachin a écrit :
I have one query in regarding const varible interpretaion by C/C++
compiler.
How Compiler actually differentiate between normal stack varible
and const varible during program execution?
It doesn't. Constness is an additional relevant information for type
resolution and const correctness that is processed at compile time.
Additionnaly, the compiler may use the const qualification to put static
data into a RO area of the objet bytecode or to perform optimisations
but that's all.
Actually, the compiler *could* differentiate. If the address of
the variable is never taken, or if the compiler could establish
that the function cannot be called recursively (e.g. if it was a
leaf function), then the compiler could put the variable in the
text segment as well.

More generally, the compiler will differentiate when optimizing;
it will exploit its knowledge of the fact that the value is
known and cannot be changed. Thus, for example, if you write:

extern void g( int const& ) ;

int
f()
{
int const i = 42 ;
g( i ) ;
return 3 * i ;
}

, the compiler will probably generate the last statement as if
you'd written "return 126;". Without the const, it can't
(unless it can see into the body of g, and assert that g doesn't
cast away const and modify the value); it must read the actual
value and multiply it by 3.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Dec 21 '07 #3

"James Kanze" <ja*********@gmail.comwrote in message
news:2f**********************************@q3g2000h sg.googlegroups.com...
On Dec 21, 7:08 am, Michael DOUBEZ <michael.dou...@free.frwrote:
Sachin a écrit :
I have one query in regarding const varible interpretaion by C/C++
compiler.
How Compiler actually differentiate between normal stack varible
and const varible during program execution?
It doesn't. Constness is an additional relevant information for type
resolution and const correctness that is processed at compile time.
Additionnaly, the compiler may use the const qualification to put static
data into a RO area of the objet bytecode or to perform optimisations
but that's all.
Actually, the compiler *could* differentiate.
Well, not "during program execution", as originally phrased. Compilers
don't do anything at execution time. :-)

-H

Dec 22 '07 #4

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

Similar topics

7
by: johny smith | last post by:
Well, I thought that I was creating a constant pointer, and would not be able to increment it without a compiler error. But this example compiles fine. The const is to the left of the * so...
11
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
2
by: krishna | last post by:
Hi, It is request u to reply soon if possible Is there any free Dragon Voice Recognisation System if it is there please give me the details. Krishna
19
by: pavan734 | last post by:
Hello, I dont know exact subject line for this question. In my class construcor I will read a value into a variable(member variable or a global variable). I need that variable to be constant...
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...
12
by: copx | last post by:
I remember reading somewhere that C's 'const' keyword is almost useless, except for maybe triggering some additional compiler warnings. 'Useless' as in: a conforming C compiler cannot assume that a...
11
by: Christopher Key | last post by:
Hello, Can anyone suggest why gcc (-W -Wall) complains, test.c:22: warning: passing arg 1 of `f' from incompatible pointer type when compiling the following code? Change the declation of f...
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...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.