Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does having constant variables as defines make your program smaller?

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 ints in debug mode (so you can easily see the values while debugging) but make them #defines in release mode so it would make the program smaller.

like image 692
Edward Avatar asked Dec 25 '14 01:12

Edward


People also ask

What happens when a variable is constant?

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.

What is the advantage of #define over constant?

#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.

Do constants make a program faster?

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.

Why should you not use #define to declare a constant?

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.


2 Answers

Using macros to “make the program smaller” is ungood for several reasons:

  • Use of macros may instead make the program larger, or have no effect.
  • Macros don't follow the scoping rules in C++. You risk inadvertent text replacement.
  • Depending on the quality of the tools you may lose debug information.
  • The advantageous effect, if it occurs, is marginal.
  • The common convention for avoiding macro name clashes, namely ALL UPPERCASE, is an eyesore.

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.

like image 51
Cheers and hth. - Alf Avatar answered Nov 24 '22 05:11

Cheers and hth. - Alf


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.

like image 45
Ryan Haining Avatar answered Nov 24 '22 05:11

Ryan Haining