Thread: 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...@te...> - 2003-05-29 16:12:20
|
> -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of Andr? Macario Barros > Sent: Thursday, May 29, 2003 4:09 PM > To: Dev-Cpp-Users > Subject: [Dev-C++] #DEFINE versus cont variables in array=20 > inicializations >=20 >=20 > Dear Users, >=20 > For the sake of flexibility, I think we only have two ways=20 > to iniciatize > a > size of an array: > 1) via #define: > #define MAX 10 > ... > int vect[MAX]; > ... >=20 > 2) via cont variable: > const int size=3D10; > ... > int vetc[size]; > ... The second in C++ only. So please all, explain what programming language = you are referring to, so as to avoid confusions. >=20 > So, in both ways, everytime if I need to change the size of=20 > the arrays, it=B4s The *initial* size of built in arrays... > only necessary to change 10 to the other value. >=20 > -->My questions: > 1) Is there more ways to to this? For built in arrays, no (except of int vetc[10]; of course). > 2) For theese two ways presented here, when use one and when=20 > use the other? Use the second. The first is used in C because the second is not = availlable. Also keep in mind that C and C++ are very different languages now. > Explaining a little more what I need to know: in a "good=20 > programming > technique" > point of view, what of theese two is the preferred of the=20 > programmers The second. But standard library containers should be preferred more. Ioannis Vranos =20 * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Ioannis V. <iv...@te...> - 2003-05-29 16:16:48
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of OROSZI Balazs > Sent: Thursday, May 29, 2003 4:32 PM > To: dev...@li... > Subject: Re: [Dev-C++] #DEFINE versus cont variables in array > inicializations > > > > 2) For theese two ways presented here, when use one and > when use the other? > > Well, C++ encourages people to use const variables instead of #define > values. But I think there are circumstances, when #define is > better. In > particular, when you HIGHLY optimize for speed in an > algorithm, and you > don't want to use magic values, then it is better to use > #define values, > as the preprocessor replaces them (as if you had typed in the exact > value) and the compiler can make various optimizations. A good compiler optimises out the const variable when possible (e.g. when not accessed later in a strange way). > For example, when you need to shift with a known value: > > i = 1<<SHIFT_VAL; > > This case it is better to #define SHIFT_VAL 3 for example, > rather than > const int SHIFT_VAL=3, because in the first case the compiler will > automatically calculate the value in compile time: > > i = 1<<3 --> i = 8; > > While this is not true for using const variables (I think, unless the > compiler is smart), and it takes more time to shift with a variable, > rather than with a #define. So, another example: > int v; > v<<SHIFT_VAL; > Here v is a variable, and it is faster to shift it if SHIFT_VAL is a > #define, rather than if SHIFT_VAL is a variable. (I have tested this > extensively!) Both above should be equivalent. > > In your case, I don't think you need this type of low-level > optimisation, so I think you can safely use a const variable. :) At the end we agree. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Ioannis V. <iv...@te...> - 2003-05-29 18:17:14
|
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Avik Ghose
Sent: Thursday, May 29, 2003 8:30 PM
To: Andr?_Macario_Barros; Dev-Cpp-Users
Subject: Re: [Dev-C++] #DEFINE versus cont variables in array
inicializations
Hi Andre,
you are right, there's probably no third way out. Now I prefer to use the
first one due to two reasons.
1> Being textual subsitution, the #define macro does not use up memory size
whereas an integer uses up 32 bytes, though that is negligent for most
purposes.
There is still a 3rd way that guarantees that no size is occupied but i
shall post it in a separate email after this.
2> The more important being that apart from compiler systems that support
C++ along with C, the interpretation of const by the compilers around is a
bit non-standard.
It is standard C++.
Like in the Cygwin gcc complier assigment to const variables result only
in a warning at compile time and program runs fine, and prints out 20!!!.
#include <stdio.h>
int main()
{
const int i = 10;
i=20;
printf("%d\n",i);
return 0;
}/*main*/
So well although I cannot remember of a third way to do it right now,
obviously the first approach is better.
I am bored to check the C standard, bu in C++ that is not allowed (g++, not
gcc).
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
<http://www.noicys.freeurl.com/>
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net <http://www.noicys.cjb.net/>
|
|
From: Ioannis V. <iv...@em...> - 2003-05-29 18:18:11
|
A third way that guarantees no space occupation by a decent compiler is: const int &SIZE = 7; Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Per W. <pw...@ia...> - 2003-05-29 19:01:38
|
I guess you don't think of gcc as a decent compiler then. try gcc -O3 -S on the code below and look at the output. 4 byte for the value 7 4 byte for the SIZE reference. access loads SIZE and pushes [SIZE] (4 byte addr info + extra instr) with const int SIZE = 7 zero byte for value every access pushes 7 directly. (4 byte value) Most compilers treat 'const int v = <value>' identical to #define XX <value>, i.e. optimal code is generated. /Per W On Thu, 29 May 2003, Ioannis Vranos wrote: > A third way that guarantees no space occupation by a decent compiler is: > > > const int &SIZE = 7; > > > > > > > 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 > |
|
From: Ioannis V. <iv...@em...> - 2003-05-29 20:12:39
|
At first the const reference can't be used for the size of the array, my
mistake.
For the rest, I do not get your point:
int main()
{
int array[17];
}
g++ -pedantic-errors -Wall -fexpensive-optimizations -O3 -ffloat-store
-mcpu=pentiumpro temp.cpp -o temp
29/05/2003 11:09 ?? 23.036 temp.exe
For the code:
int main()
{
const int SIZE=17;
int array[SIZE];
}
29/05/2003 11:10 ?? 23.036 temp.exe
So the compiler optimises out the const int thing.
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]
> Sent: Friday, May 30, 2003 2:00 AM
> To: Ioannis Vranos
> Cc: Dev-C++ Mailing List
> Subject: RE: [Dev-C++] #DEFINE versus cont variables in array
> inicializations
>
>
> I guess you don't think of gcc as a decent compiler then.
>
> try gcc -O3 -S on the code below and look at the output.
> 4 byte for the value 7
> 4 byte for the SIZE reference.
> access loads SIZE and pushes [SIZE] (4 byte addr info + extra instr)
>
> with const int SIZE = 7
> zero byte for value
> every access pushes 7 directly. (4 byte value)
>
> Most compilers treat 'const int v = <value>' identical to #define XX
> <value>, i.e. optimal code is generated.
>
> /Per W
>
> On Thu, 29 May 2003, Ioannis Vranos wrote:
>
> > A third way that guarantees no space occupation by a decent
> compiler is:
> >
> >
> > const int &SIZE = 7;
> >
> >
> >
> >
> >
> >
> > 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
> >
>
>
|
|
From: <or...@vp...> - 2003-05-29 21:02:58
|
Ioannis Vranos wrote: > 29/05/2003 11:09 ?? 23.036 temp.exe > ... > 29/05/2003 11:10 ?? 23.036 temp.exe > > So the compiler optimises out the const int thing. Are you sure if size is the same, exe is same? Look at the contents of it with a comparison program. gcc works in blocks or what, so 2 totally different programs might have the same size. -- Greetings, Balázs |
|
From: Per W. <pw...@ia...> - 2003-05-29 21:30:50
|
For comparing size of code - either go for the map file, or look at the assembler output. The exe file size isn't interesting. A look at the assembler output also shows the difference between fast, slow, big, small, ... code. Extra memory references are almost always bad. Extra bytes of code might not be expensive, as long as the processor have bandwidth enough - for example in a loop small enough to fit in the L1 cache, the processor doesn't consume any code bandwidth after the first iteration. In such a case, a larger code block might actually be faster by removing an extra access to a memory location. /Per W On Thu, 29 May 2003, OROSZI Bal=E1zs wrote: > Ioannis Vranos wrote: > > 29/05/2003 11:09 ?? 23.036 temp.exe > > ... > > 29/05/2003 11:10 ?? 23.036 temp.exe > > > > So the compiler optimises out the const int thing. > > Are you sure if size is the same, exe is same? Look at the contents of > it with a comparison program. gcc works in blocks or what, so 2 totally > different programs might have the same size. > > -- > Greetings, > Bal=E1zs > > > > ------------------------------------------------------- > 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 > |
|
From: Ioannis V. <iv...@em...> - 2003-05-29 20:15:48
|
> -----Original Message----- > From: Per Westermark [mailto:pw...@ia...] > Sent: Friday, May 30, 2003 2:00 AM > To: Ioannis Vranos > Cc: Dev-C++ Mailing List > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array > inicializations > > > I guess you don't think of gcc as a decent compiler then. > > try gcc -O3 -S on the code below and look at the output. > 4 byte for the value 7 > 4 byte for the SIZE reference. > access loads SIZE and pushes [SIZE] (4 byte addr info + extra instr) > > with const int SIZE = 7 > zero byte for value > every access pushes 7 directly. (4 byte value) > > Most compilers treat 'const int v = <value>' identical to #define XX > <value>, i.e. optimal code is generated. ... and we are talking about C++ here, so g++ is the compiler and not gcc. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Per W. <pw...@ia...> - 2003-05-29 22:03:00
|
I was also talking about c++, so obviously the extension of the source file (I normally use *.cc) tells gcc to use g++ for c++ compilation. /Per W On Thu, 29 May 2003, Ioannis Vranos wrote: > > -----Original Message----- > > From: Per Westermark [mailto:pw...@ia...] > > Sent: Friday, May 30, 2003 2:00 AM > > To: Ioannis Vranos > > Cc: Dev-C++ Mailing List > > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array > > inicializations > > > > > > I guess you don't think of gcc as a decent compiler then. > > > > try gcc -O3 -S on the code below and look at the output. > > 4 byte for the value 7 > > 4 byte for the SIZE reference. > > access loads SIZE and pushes [SIZE] (4 byte addr info + extra instr) > > > > with const int SIZE = 7 > > zero byte for value > > every access pushes 7 directly. (4 byte value) > > > > Most compilers treat 'const int v = <value>' identical to #define XX > > <value>, i.e. optimal code is generated. > > > > ... and we are talking about C++ here, so g++ is the compiler and not gcc. > > > > 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 > |
|
From: Ioannis V. <iv...@em...> - 2003-05-29 20:19:30
|
> -----Original Message----- > From: Per Westermark [mailto:pw...@ia...] > Sent: Friday, May 30, 2003 2:00 AM > To: Ioannis Vranos > Cc: Dev-C++ Mailing List > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array > inicializations > > Most compilers treat 'const int v = <value>' identical to #define XX > <value>, i.e. optimal code is generated. ... and what do you mean by that? It seems we agree? Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Per W. <pw...@ia...> - 2003-05-29 21:24:28
|
Yes, we agree that 'const int x = <value>' is efficient. We don't agree that 'const int &x = <value>' is efficient. /Per W On Thu, 29 May 2003, Ioannis Vranos wrote: > > -----Original Message----- > > From: Per Westermark [mailto:pw...@ia...] > > Sent: Friday, May 30, 2003 2:00 AM > > To: Ioannis Vranos > > Cc: Dev-C++ Mailing List > > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array > > inicializations > > > > Most compilers treat 'const int v = <value>' identical to #define XX > > <value>, i.e. optimal code is generated. > > > ... and what do you mean by that? It seems we agree? > > > > 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 > |
|
From: Ioannis V. <iv...@te...> - 2003-05-30 03:36:08
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Per Westermark > Sent: Friday, May 30, 2003 4:23 AM > To: Ioannis Vranos > Cc: Dev-C++ Mailing List > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array > inicializations > > > Yes, we agree that 'const int x = <value>' is efficient. > We don't agree that 'const int &x = <value>' is efficient. The reference can't be used to provide the size of a built in array, so my mistake for that. On the other hand regarding reference, a compiler would be able to optimise out and make it occupy no more space. const int &x=7; Can you tell me why that wouldn't be able to be equivalent to #define x 7 when used just as a value and not accessed in any other way? As I can think of it, a const reference should be more eaiser to optimise out than a const variable. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Per W. <pw...@ia...> - 2003-05-30 08:30:27
|
If the reference is declared auto, the compiler can optimize away the reference, but if it's declared as a global, the compiler won't optimize it away, since by definition, a reference - just like a pointer - is an address to a value, not the value itself. If the global was declared static, then file-level global optimization would be able to notice if no part of the the file used it as an address and might then convert it back to just a 'const int', but then again: why not declare it as 'const int' from the beginning, since that is the optimal form if we don't want to play with the address of it. /Per W On Fri, 30 May 2003, Ioannis Vranos wrote: > > -----Original Message----- > > From: dev...@li... > > [mailto:dev...@li...] On Behalf > > Of Per Westermark > > Sent: Friday, May 30, 2003 4:23 AM > > To: Ioannis Vranos > > Cc: Dev-C++ Mailing List > > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array > > inicializations > > > > > > Yes, we agree that 'const int x = <value>' is efficient. > > We don't agree that 'const int &x = <value>' is efficient. > > > The reference can't be used to provide the size of a built in array, so my > mistake for that. > > > On the other hand regarding reference, a compiler would be able to optimise > out and make it occupy no more space. > > > const int &x=7; > > > Can you tell me why that wouldn't be able to be equivalent to #define x 7 > when used just as a value and not accessed in any other way? > > As I can think of it, a const reference should be more eaiser to optimise > out than a const variable. > > > > 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 > |
|
From: Ioannis V. <iv...@te...> - 2003-05-30 16:38:51
|
> -----Original Message----- > From: Per Westermark [mailto:pw...@ia...] > Sent: Friday, May 30, 2003 2:39 PM > To: Ioannis Vranos > Cc: Dev-C++ Mailing List > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array > inicializations > > > If the reference is declared auto, the compiler can optimize away the > reference, but if it's declared as a global, the compiler > won't optimize > it away, since by definition, a reference - just like a > pointer - is an > address to a value, not the value itself. Yes I agree. I was talking about local auto const references. Global const variables are difficult to optimise out too, because the compiler doesn't know if it is used in another compilation unit (file). Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Per W. <pw...@ia...> - 2003-05-30 17:47:35
|
At least for scalar data types, it doesn't matter if a global const xx variable is used in other modules. 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. Since an address is 4 bytes for normal 32-bit code, any const variable <= 4 byte will consume same space for the inlined value as the address needed to reference a real variable. However, the inlined value will automatically follow in the instruction stream, thereby using the L1/L2 caches optimally, while an address reference to a separate storage would have to use a different cache line in the processor. The net result is that a global 'const int xx = <value>' is just as efficient as if the declararation was written locally within a function. Both alternatives will be inlined, without any global or auto variable whatsoever. /Per W On Fri, 30 May 2003, Ioannis Vranos wrote: > > -----Original Message----- > > From: Per Westermark [mailto:pw...@ia...] > > Sent: Friday, May 30, 2003 2:39 PM > > To: Ioannis Vranos > > Cc: Dev-C++ Mailing List > > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array > > inicializations > > > > > > If the reference is declared auto, the compiler can optimize away the > > reference, but if it's declared as a global, the compiler > > won't optimize > > it away, since by definition, a reference - just like a > > pointer - is an > > address to a value, not the value itself. > > > > Yes I agree. I was talking about local auto const references. Global const > variables are difficult to optimise out too, because the compiler doesn't > know if it is used in another compilation unit (file). > > > > > 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 > |
|
From: Ioannis V. <iv...@te...> - 2003-05-30 18:33:13
|
> -----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
|
|
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
>
|
|
From: Per W. <pw...@ia...> - 2003-05-30 19:05:11
|
Oops, goofed a bit. See below.
/Per W
On Sat, 31 May 2003, Per Westermark wrote:
> 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
Should of course be 'const int SIZE = <value>'
> "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
> >
>
>
>
> -------------------------------------------------------
> 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
>
|
|
From: Ioannis V. <iv...@te...> - 2003-05-30 19:27:04
|
> -----Original Message----- > From: Per Westermark [mailto:pw...@ia...]=20 > Sent: Saturday, May 31, 2003 2:04 AM > To: Ioannis Vranos > Cc: Dev-C++ Mailing List > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array=20 > inicializations >=20 >=20 > Oops, goofed a bit. See below. > > But then again > > - 'a const &int =3D <value>' is a complicated declaration=20 > just to get a >=20 > Should of course be 'const int SIZE =3D <value>' >=20 > > "magic" address to compare against. I still do not understand what you mean by the above. The language rules = are language rules. A global const variable has an address (at least it = behaves as it has, when we want to use this address).=20 Only references do not have their own address. Ioannis Vranos =20 * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Per W. <pw...@ia...> - 2003-05-30 20:29:59
|
Give a declaration:
const int SIZE = 7;
If you don't take the address of it, the compiler will generate code
looking like:
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.
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.
/Per W
On Fri, 30 May 2003, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: Per Westermark [mailto:pw...@ia...]
> > Sent: Saturday, May 31, 2003 2:04 AM
> > To: Ioannis Vranos
> > Cc: Dev-C++ Mailing List
> > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array
> > inicializations
> >
> >
> > Oops, goofed a bit. See below.
> >
> > But then again
> > > - 'a const &int = <value>' is a complicated declaration
> > just to get a
> >
> > Should of course be 'const int SIZE = <value>'
> >
> > > "magic" address to compare against.
>
>
> I still do not understand what you mean by the above. The language rules are
> language rules. A global const variable has an address (at least it behaves
> as it has, when we want to use this address).
>
> Only references do not have their own address.
>
>
>
>
> 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
>
|
|
From: Ioannis V. <iv...@te...> - 2003-05-30 23:24:25
|
> -----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. > > 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? > 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. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
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
>
|
|
From: Ioannis V. <iv...@em...> - 2003-05-31 00:21:40
|
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]
> Sent: Saturday, May 31, 2003 6:38 AM
> To: Ioannis Vranos
> Cc: Dev-C++ Mailing List
> Subject: RE: [Dev-C++] #DEFINE versus cont variables in array
> inicializations
>
>
> See below
>
>
> If you know that you need to take the address in another file, then
> declare it as:
> extern const int SIZE = 7;
I am sorry to tell you this, but this is *nonsense*. The C++ standard
doesn't require to define it like this.
I can define a const object in the global namespace:
const int x=7;
and in another file I can do this:
#include <iostream>
extern const int x;
// ...
void f()
{
const unsigned char *p=reinterpret_cast<const unsigned char *>(&x);
// Shows SIZE 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])<<" ";
}
> 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;'
Please direct me in the C++ standard where it mentions such a requirement.
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|
|
From: Per W. <pw...@ia...> - 2003-05-31 00:50:49
|
Please.
I'm not talking about C++ standard.
I'm talking about a real gcc 3.2 behaviour.
This isn't what I think but what my compiler does:
--- def.cc ---
const int x = 7;
--- ref.cc ---
#include <stdio.h>
extern const int x;
int main() {
printf("value x = %d\n",x);
printf("addr x = %p\n",&x);
return 0;
}
--- build ---
>g++ -c def.c
>g++ -c ref.c
>g++ def.o ref.o -lstdc++ -o test.exe
--- result ---
ref.o(.text+0x3f):ref.cc: undefined reference to `x'
ref.o(.text+0x54):ref.cc: undefined reference to `x'
--- def.s ---
.file "def.cc"
.text
.align 4
_x:
.long 7
--- ref.s ---
.file "ref.cc"
.def ___main; .scl 2; .type 32; .endef
.text
LC0:
.ascii "value x = %d\12\0"
LC1:
.ascii "addr x = %p\12\0"
.align 2
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
LFB1:
pushl %ebp
LCFI0:
movl %esp, %ebp
LCFI1:
subl $8, %esp
LCFI2:
andl $-16, %esp
movl $0, %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
call __alloca
call ___main
subl $8, %esp
pushl _x
pushl $LC0
LCFI3:
call _printf
addl $16, %esp
subl $8, %esp
pushl $_x
pushl $LC1
call _printf
addl $16, %esp
movl $0, %eax
leave
ret
LFE1:
.def _printf; .scl 2; .type 32; .endef
--- end ---
As you can see, def.cc doesn't export the const int x.
If I instead make the following change, everything will work ok, since
now, x is exported from def.o.
--- def.cc ---
extern int x = 7;
--- def.s ---
.file "def.cc"
.globl _x
.text
.align 4
_x:
.long 7
--- end ---
/Per W
On Sat, 31 May 2003, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: Per Westermark [mailto:pw...@ia...]
> > Sent: Saturday, May 31, 2003 6:38 AM
> > To: Ioannis Vranos
> > Cc: Dev-C++ Mailing List
> > Subject: RE: [Dev-C++] #DEFINE versus cont variables in array
> > inicializations
> >
> >
> > See below
> >
> >
> > If you know that you need to take the address in another file, then
> > declare it as:
> > extern const int SIZE = 7;
>
>
> I am sorry to tell you this, but this is *nonsense*. The C++ standard
> doesn't require to define it like this.
>
>
> I can define a const object in the global namespace:
>
> const int x=7;
>
>
>
> and in another file I can do this:
>
>
> #include <iostream>
>
>
> extern const int x;
>
> // ...
>
>
> void f()
> {
> const unsigned char *p=reinterpret_cast<const unsigned char *>(&x);
>
>
> // Shows SIZE 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])<<" ";
> }
>
>
>
>
>
>
>
> > 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;'
>
>
>
> Please direct me in the C++ standard where it mentions such a requirement.
>
>
>
>
> 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
>
|