vsz
vsz

Reputation: 4909

What are the major advantages of const versus #define for global constants?

In embedded programming, for example, #define GLOBAL_CONSTANT 42 is preferred to const int GLOBAL_CONSTANT = 42; for the following reasons:

Against all these advantages of using #define, what are the major advantages of using const? In a non-µC environment memory is usually not such a big issue, and const is useful because it can be used locally, but what about global constants? Or is the answer just "we should never ever ever use global constants"?

Edit:

The examples might have caused some misunderstanding, so I have to state that they are in C. If the C compiler generated the exact same code for the two, I think that would be an error, not an optimization.

I just extended the question to C++ without thinking much about it, in the hopes of getting new insights, but it was clear to me, that in an object-oriented environment there is very little space for global constants, regardless whether they are macros or consts.

Upvotes: 3

Views: 5366

Answers (7)

Alok Save
Alok Save

Reputation: 206518

The answer to your question varies for C and C++.

In C, const int GLOBAL_CONSTANT is not a constant in C, So the primary way to define a true constant in C is by using #define.

In C++, One of the major advantage of using const over #define is that #defines don't respect scopes so there is no way to create a class scoped namespace. While const variables can be scoped in classes.

Apart from that there are other subtle advantages like:

Avoiding Weird magical numbers during compilation errors:

If you are using #define those are replaced by the pre-processor at time of precompilation So if you receive an error during compilation, it will be confusing because the error message wont refer the macro name but the value and it will appear a sudden value, and one would waste lot of time tracking it down in code.

Ease of Debugging:

Also for same reasons mentioned in #2, while debugging #define would provide no help really.

Upvotes: 3

Adam Liss
Adam Liss

Reputation: 48290

Another reason that hasn't been mentioned yet is that const variables allow the compiler to perform explicit type-checking, but macros do not. Using const can help prevent subtle data-dependent errors that are often difficult to debug.

Upvotes: 3

SoapBox
SoapBox

Reputation: 20609

There's no reason to use #define instead of a const int in C++. Any decent C++ compiler will substitute the constant value from a const int in the same way it does for a #define where it is possible to do so. Both take approximately the same amount of flash when used the same way.

Using a const does allow you to take the address of the value (where a macro does not). At that point, the behavior obviously diverges from the behavior of a Macro. The const now needs a space in the program in both flash and in RAM to live so that it can have an address. But this is really what you want.

The overhead here is typically going to be an extra 8 bytes, which is tiny compared to the size of most programs. Before you get to this level of optimization, make sure you have exhausted all other options like compiler flags. Using the compiler to carefully optimize for size and not using things like templates in C++ will save you a lot more than 8 bytes.

Upvotes: 1

Puppy
Puppy

Reputation: 146910

The only problems you list with const sum up as "I've got the most incompetent compiler I can possibly imagine". The problems with #define, however, are universal- for example, no scoping.

Upvotes: 1

ouah
ouah

Reputation: 145829

In C the const qualifier does not define a constant but instead a read-only object:

#define A  42       // A is a constant
const int a = 42;  // a is not constant

A const object cannot be used where a real constant is required, for example:

static int bla1 = A;  // OK, A is a constant
static int bla2 = a;  // compile error, a is not a constant

Note that this is different in C++ where the const really qualifies an object as a constant.

Upvotes: 1

Tadeusz Kopec for Ukraine
Tadeusz Kopec for Ukraine

Reputation: 12403

Are you sure your compiler is too dumb to optimize your constant by inserting its value where it is needed instead of putting it into memory? Compilers usually are good in optimizations.

And the main advantage of constants versus macros is that constants have scope. Macros are substituted everywhere with no respect for scope or context. And it leads to really hard to understand compiler error messages.
Also debuggers are not aware of macros.
More can be found here

Upvotes: 8

Luchian Grigore
Luchian Grigore

Reputation: 258568

I think the main advantage is that you can change the constant without having to recompile everything that uses it.

Since a macro change will effectively modify the contents of the file that use the macro, recompilation is necessary.

Upvotes: 1

Related Questions