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

前端 未结 7 1126
北恋
北恋 2021-01-14 10:28

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

相关标签:
7条回答
  • 2021-01-14 10:57

    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.

    0 讨论(0)
  • 2021-01-14 10:58

    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.

    0 讨论(0)
  • 2021-01-14 10:59

    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.

    0 讨论(0)
  • 2021-01-14 11:03

    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

    0 讨论(0)
  • 2021-01-14 11:09

    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.

    0 讨论(0)
  • 2021-01-14 11:13

    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.

    0 讨论(0)
提交回复
热议问题