| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Sachin" |
| Date: |
20 Dec 2007 11:55:05 PM |
| Object: |
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
.
|
|
| User: "Michael DOUBEZ" |
|
| Title: Re: Const Varible recognisation by C++ Compiler |
21 Dec 2007 12:08:15 AM |
|
|
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
.
|
|
|
| User: "James Kanze" |
|
| Title: Re: Const Varible recognisation by C++ Compiler |
21 Dec 2007 06:56:10 AM |
|
|
On Dec 21, 7:08 am, Michael DOUBEZ <michael.dou...@free.fr> wrote:
Sachin a =E9crit :
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 =3D 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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
| User: "Howard" |
|
| Title: Re: Const Varible recognisation by C++ Compiler |
22 Dec 2007 04:08:22 PM |
|
|
"James Kanze" <james.kanze@gmail.com> wrote in message
news:2fbda4d9-cf49-4993-8008-a16b367b3567@q3g2000hsg.googlegroups.com...
On Dec 21, 7:08 am, Michael DOUBEZ <michael.dou...@free.fr> wrote:
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
.
|
|
|
|
|
|

|
Related Articles |
|
|