RE: [Dev-C++] #DEFINE versus cont variables in array inicializations
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <iv...@em...> - 2003-05-31 01:19:11
|
> -----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
|