Const Varible recognisation by C++ Compiler

S

Sachin

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
 
M

Michael DOUBEZ

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
 
J

James Kanze

Sachin a écrit :
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.
 
H

Howard

Sachin a écrit :
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top