RE: [Dev-C++] #DEFINE versus cont variables in array inicializations
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-05-31 12:17:59
|
I don't know why gcc doesn't optimize a 'const int &var = <value>' when
referenced. The only thing I could guess is that they never thought
it useful to optimize the use of the above declaration, since the
most common use for a const reference is something like:
int var = 12;
void func(const int& ref) {
// do something
}
int main() {
func(var);
}
In this case, the compiler doesn't know that func() is only called with
var as parameter, i.e. with a constant value, so it can't optimize.
If you think about it, what extra results are you expecting from the
following declaration? It is basically an unchangeable pointer to an
unchangeable value. But what improvements should it give to your program?
const int &var = 12;
/Per W
On Sat, 31 May 2003, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On Behalf
> > Of Ioannis Vranos
> > Sent: Saturday, May 31, 2003 3:22 AM
> > To: Dev-C++ Mailing List
> > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array
> > inicializations
> >
> >
> > I just checked it and it seems you are right. Don't know why yet.
> >
> >
> > However does the above style make the compiler to give it an
> > address? That
> > would be very strange. I shall check the standard now.
>
>
> TC++PL on pages 199,200 says that const has by default internal linkage. So
> the definition must be accomanied by the keyword extern too.
>
>
> So my code becomes:
>
>
> temp2.cpp:
>
> extern const int x=17;
>
>
> temp.cpp:
>
> #include <iostream>
>
>
> extern const int x;
>
>
> int main()
> {
> const unsigned char *p=reinterpret_cast<const unsigned char *>(&x);
>
>
> // Shows x representation in memory in byte values
> // including padding bits
> for(unsigned long i=0; i<sizeof(x); ++i)
> std::cout<<static_cast<short>(p[i])<<" ";
>
>
> }
>
>
> C:\c>g++ temp.cpp temp2.cpp -o temp
>
> C:\c>temp
> 17 0 0 0
> C:\c>
>
>
> So in this case the compiler can know if the const object or *reference* is
> accessed in another translation unit.
>
>
>
> However why a compiler would "inline" const int x=7; and would not be able
> to "inline" const int &x=7; isn't clear and would be nice if you explained
> it to us. And please do not involve L1/L2 caches, we are talking about
> inline substitution here. And my CPU may have no cache.
>
>
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL 1: http://run.to/noicys
> * Alternative URL 2: http://www.noicys.cjb.net
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: eBay
> Get office equipment for less on eBay!
> http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|