Connecting Tech Pros Worldwide Help | Site Map

Const Varible recognisation by C++ Compiler

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 21st, 2007, 05:05 AM
Sachin
Guest
 
Posts: n/a
Default 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

  #2  
Old December 21st, 2007, 05:15 AM
Michael DOUBEZ
Guest
 
Posts: n/a
Default Re: Const Varible recognisation by C++ Compiler

Sachin a écrit :
Quote:
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
  #3  
Old December 21st, 2007, 12:05 PM
James Kanze
Guest
 
Posts: n/a
Default Re: Const Varible recognisation by C++ Compiler

On Dec 21, 7:08 am, Michael DOUBEZ <michael.dou...@free.frwrote:
Quote:
Sachin a écrit :
Quote:
Quote:
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?
Quote:
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:james.kanze@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

  #4  
Old December 22nd, 2007, 09:15 PM
Howard
Guest
 
Posts: n/a
Default Re: Const Varible recognisation by C++ Compiler


"James Kanze" <james.kanze@gmail.comwrote in message
news:2fbda4d9-cf49-4993-8008-a16b367b3567@q3g2000hsg.googlegroups.com...
On Dec 21, 7:08 am, Michael DOUBEZ <michael.dou...@free.frwrote:
Quote:
Sachin a écrit :
Quote:
Quote:
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?
Quote:
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.
Quote:
Actually, the compiler *could* differentiate.
Well, not "during program execution", as originally phrased. Compilers
don't do anything at execution time. :-)

-H

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.