Balog Pal wrote in a message to Wim Veldhuis:
BM> #define WHEEL_MAX 13
BM> #define STARTING_CASH 300
TM> const unsigned int WHEEL_MAX = 13;
TM> const unsigned int STARTING_CASH = 300;
WV> This is not entirely correct. Compilers usually allocate memory for
WV> something declared as const ...
BP> What compilers do that? I tried a plenty (MS, Borland, Sym,
BP> Watcom, SUN, HP) and didn't notice that. They allocate
BP> memory if you comile in C (!) mode but not in C++. Unless
BP> you happen to reference the address of the const that is not
BP> very likely for an int constant as above.
If you do it with ints, there is not a problem, if you do it with double or
another more complex type the problem does arise.
If you place a const double inch 2.54; in a header file and include the
header file in a module which does not use the const, you might get a warning
like I said. I had this problem when using Borland C++ 4.5x quite some time
ago. The "reasoning" of the compiler probably is to allocate it just once in
memory as a read-only variable, since it is difficult to load it into
processor registers directly and the code for transfering a double into
registers is already present when it is located somewhere in memory.
The compiler however generates a warning about an identifier not being used
if you include the headerfile in a module which does not use the identifier.
WV> This can result in warnings if you do
WV> this inside a header file and you compile another module which uses
WV> the headerfile but doesn't use the constant.
BP> I have a plenty of such const int's in headers and have no
BP> warnings. Hm?
I omitted to say that this is usually a problem with other data types than
int. Int is mostly used for consts, so this is indeed usually not a problem.
WV> In segmented programs there is also a gotscha's if you put initialised
WV> data in a non default segment for some reason. At least with the
WV> borland compiler this results in errors as it ALWAYS assumes const
WV> variables to be in the default data segment !
BP> Which version and path level? Probably a serious bug.
If I recall correctly it was with Borland C++ v4.52. Target model is large,
and windows.
I agree it is a bug, but Borland will probably call it a feature :-(
I haven't tested this with v5.x since I only used it for 32-bit applications
so far, which do not have the 64K barrier.
PS. My excuse for my late response, I am very busy at the moment and do not
regularly check my fido mail. You can reach me also at the internet at
veldhuis@xs4all.nl
mvg/wr
--- timEd/2 1.01.g3+
---------------
* Origin: LightHouse BBS ==> I am a H.U.G.O. Member ! (2:285/324.3)
|