Sign in to follow this  

Global variables in comparison to #define

This topic is 2014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Unless you need a non-integer type constant.

Edit: And we can agree that what originally prompted me to my response is true too, meaning
[quote name='Krypt0n' timestamp='1341918763' post='4957577']
one important difference is that global variables are really variables, the compiler has to assume that you could change them, even if they are const (e.g. modifying the binary or unlocking that segment via system calls, changing a value and locking that segment again). That means that you have a load, possibly a cache miss and in cases where that value is just 0 or 1, the compiler could optimize code, but with variables it's not fully legal.[/quote]
is pretty much untrue. The compiler does not have to create 'real variables'. The compiler does not have to assume you change them, changing them is undefined behavior and may or may work depending on the compiler. The compiler can optimize the variable completely out, it is completely legal.

I'm leaving you with "enums might be better if you are sticking to integer types anyway and might work on some embedded platforms". I do not have a problem with the end result in this case (though I still prefer the more idiomatic constants which work fine on my target platforms) but I highly object to the plainly wrong information we encountered on the way. Edited by BitMaster

Share this post


Link to post
Share on other sites
The ultimate answer is:
Whenever there is a way to get the same result without defines, that solution is always preferred.
If not, defines are there to cover your back.

End of discussion.


L. Spiro

Share this post


Link to post
Share on other sites
[quote name='BitMaster' timestamp='1341923613' post='4957599']
is pretty much untrue. The compiler does not have to create 'real variables'. The compiler does not have to assume you change them, changing them is undefined behavior and may or may work depending on the compiler. The compiler can optimize the variable completely out, it is completely legal.[/quote]

Yep. Writing to a variable that was declared const then has its const-ness removed with const_cast or a more basic C style cast is undefined behaviour.

If, however, you take the address of a const T, then the compiler can no longer inline it I believe. But you couldn't take the address of a #define literal anyway so this is apples and orangutans.

Share this post


Link to post
Share on other sites
Braindump inc:[list]
[*]'C' programs or C++ written 'like C' should prefer using enums for constants.
[*]Global variables are almost always re-read from memory. If they are pointers, the pointers are re-loaded and then dereferenced on almost every access. This can be a perf problem, even when the pointer is just some virtual base class. Compilers treat globals as volatile for the most part.
[*]global const variables should be externed and placed in a single file. This also means the compiler can't inline their value in the code like it can with an enum or a define. On the plus side they are visible in mapfiles and debuggers and easy to change even in optimized and release builds.
[*]If you make the variable static in some attempt to make the compiler inline its value where it is used, you will generally fail, and the linker map won't show the symbol (it is static), and it will take up space in your executable for every .cpp file that includes your header (which can be pretty bad if your static is a big array of bytes or something large).
[*]class member static const variables are ok to use, they work more like enums (but can't be anything but integer types until C++11 is supported on your environment)
[/list] Edited by Zoner

Share this post


Link to post
Share on other sites

This topic is 2014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this