Example:
#define Var1 35
static const int Var1( 35);
So while #define
replaces everywhere that I've used Var1
with 35
at compile time (which I presume makes the compile time slightly longer, if you have a lot of them, as it parses the code), using a static const int
makes the compiler consider it a variable.
Does this mean that when using static const int
it'll increase the memory imprint of my program because it has to use memory for all those constants, or is this overhead pretty much optimised out by the compiler anyway?
The reason I ask is because I'm wondering if it'd be better, for situations like this, to have them as static const int
s in debug mode (so you can easily see the values while debugging) but make them #define
s in release mode so it would make the program smaller.
A constant variable is one whose value cannot be updated or altered anywhere in your program. A constant variable must be initialized at its declaration.
#define is a preprocessor directive. Data defined by the #define macro definition are preprocessed, so that your entire code can use it. This can free up space and increase compilation times. const variables are considered variables, and not macro definitions.
Yes, const can (not guaranteed) help the compiler produce faster/more correct code. More so than not, they're just a modifier on data that you express to both the compiler and to other people that read your code that some data is not supposed to change. This helps the type system help you write more correct software.
The disadvantage of #define is that is replaces every occurence of the name, while const variables get normal lookup, so you have less risk of naming conflicts and it's not typesafe.
Using macros to “make the program smaller” is ungood for several reasons:
In short this is an example of a premature optimization.
And as Donald Knuth observed, premature optimizations are Evil™.
In passing, note that the static
in
static const int Var1( 35);
… is redundant if this at namespace scope. A namespace scope constant has internal linkage by default. Just write
const int Var1 = 35;
… for the same effect, but IMHO more clear.
If it is static
then the compiler can see that it's only used inside of that translation unit and not have to wonder how it's used externally, which is an advantage. If you don't do anything making it have to be an actual variable (such as creating a pointer to it) then the compiler will often optimize it out.
A friendlier approach could be using enums
enum { Var1 = 35 };
or in C++11, constexpr
constexpr int Var1 = 35;
These also have the advantage of not messing with a variable of the same name in another scope, if you later had
void f() {
int Var1;
}
The #define would turn it into int 35;
But the difference in memory used will be very small, likely so insignificant it will never have any measurable impact on performance unless you're in an extremely limited environment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With