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-30 18:46:13
|
If you check gcc, it will allocate a SIZE symbol in the text area, if
someone tries to take the address of it. By storing it in the text area,
you will get a GP if you try to write to it, since the text area is
write-protected for most operating systems.
Since you are not allowed to change it, you can't use it as a target for a
memory copy. If you use it as the source for a memory copy, it doesn't
matter where it is - or if there is multiple copies of the value - since
the copy would still copy the value of the const symbol.
If you try to compare the address of SIZE from two different modules, you
will - I haven't tested for different compilers - probably get a linker
error, since both object files will contain a _SIZE symbol. But then again
- 'a const &int = <value>' is a complicated declaration just to get a
"magic" address to compare against.
/Per W
On Fri, 30 May 2003, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: Per Westermark [mailto:pw...@ia...]
> > Sent: Saturday, May 31, 2003 12:19 AM
> > To: Ioannis Vranos
> > Cc: Dev-C++ Mailing List
> > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array
> > inicializations
> >
> >
> > At least for scalar data types, it doesn't matter if a global const xx
> > variable is used in other modules.
>
>
> Why? Imagine in another unit:
>
>
> extern const int SIZE;
>
> void f()
> {
> cout<<&SIZE;
> }
>
>
>
> > Since the compiler inlines the value of the const variable
> > everytime it is
> > used, there is no need to keep a global reference for use by
> > other source
> > files.
>
>
> What if we use its address in another unit? What about this:
>
> #include <cstring>
>
> extern const int SIZE;
>
> // in a function ...
>
> using std::memcpy;
>
> // ...
>
> int *p=new int;
>
> memcpy(p, &SIZE, sizeof(*p));
>
>
>
>
> 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
>
|