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 23:40:00
|
See below
/Per W
On Sat, 31 May 2003, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: Per Westermark [mailto:pw...@ia...]
> > Sent: Saturday, May 31, 2003 3:18 AM
> > To: Ioannis Vranos
> > Cc: Dev-C++ Mailing List
> > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array
> > inicializations
> >
> >
> > Give a declaration:
> >
> > const int SIZE = 7;
> >
> > If you don't take the address of it, the compiler will generate code
> > looking like:
>
>
> It is difficult for the compiler to know if I use its address in another
> file. Usually the linker notices the uses in other files.
>
If you know that you need to take the address in another file, then
declare it as:
extern const int SIZE = 7;
>
>
> >
> > pushl $7
> >
> > If you take the address of it, the compiler will notice and
> > generate code
> > like:
> >
> > pushl $_SIZE
> >
> > ...
> >
> > _SIZE:
> > .long 7
> >
> > where the second case will store _SIZE in .text and not in .data.
>
>
> I can't understand the above very much but I do not think the compiler cn
> check every file for this, only the linker. Also what if I make obj files
> and then add some code in a cpp file that uses the address, make a new obj
> annd then link all objs together?
>
It doesn't. Either make sure that the file that declares SIZE takes it's
address, or declare it as 'extern const int SIZE = 7;'
>
>
> > You will be able to take the address of SIZE from one object
> > file. If you
> > declare 'const int SIZE = 7;' in another file to and try to take the
> > address of it, you will correctly get a linker error. If you
> > in the other
> > file declares 'extern const int SIZE', that file will always
> > reference the
> > variable SIZE, since it can't do any inlining.
>
>
>
> This is a bit difficult. You know, extern const int SIZE; is a *definition*
> in the absense of another definition and takes the default value 0.
>
extern const int SIZE; will just tell the compiler that somewhere in some
other object file, there is a symbol SIZE. The compile unit that sees this
declaration will not allocate any variable, so no default value either.
>
>
>
> 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
>
|