4

I'm writing an Arduino library to communicate with an I2C device, and I'm wondering what the best way is to declare all the register addresses so as to save memory.

Using #defines:

#define REGISTER_MOTOR_1_MODE 0x44
#define REGISTER_MOTOR_2_MODE 0x47

Or using static const:

static const uint8_t REGISTER_MOTOR_1_MODE = 0x44;
static const uint8_t REGISTER_MOTOR_2_MODE = 0x47;

(Obviously I have more than just two registers I need to declare, but I thought two would illustrate the point just fine)

  • define's are just "find and replace"s, and can bit you, if you do anything other than numbers. For example #define LED_MASK 0x01<<2. A safe(r) way to write that would be #define LED_MASK (0x01<<2). See also stackoverflow.com/questions/6542270/… – Gerben Feb 25 at 16:24
8

You'll find no noticeable difference memory-wise between the two.

The only real difference is that the const method also imposes a type to the value, which can be useful for function overloading or mathematical operations.

  • Macros also have types. #define REGISTER_MOTOR_1_MODE 0x44 would cause REGISTER_MOTOR_1_MODE to have a type of int. The difference is more in the explicitness of the type. – Pharap Feb 26 at 5:34
  • @Pharap Almost... Macros do not have a type. However, the content of the macro can have either an implicit or explicit type. That type is nothing to do with the macro. – Majenko Feb 26 at 10:10
  • The type of the expression that the macro substitutes for is for all intents and purposes the type of the macro. Unless the macro accepts arguments then the expression will always have the same type. You can stick a macro in a decltype and get a valid type. Strictly yes, the macro is gone by the time the actual compilation phase begins, and yes, not all macros will substitute for a valid expression with a type, but the point is that if you put a macro into the code and that macro substitutes for a valid expression then the resulting value won't be 'untyped'. – Pharap Feb 27 at 6:19
5

A #define is a preprocessor macro. As Gerben says in his comment, it's just an automated find-and-replace.

If you use it to hold things like C string constants, e.g. #define ERROR_STRING "You messed up, bud!" it could actually cause your program to take more memory, since that same string literal will be duplicated every time you reference it. In that case a static const would be both type-safe AND reduce memory usage.

  • Would it be duplicated? I mean, isn't it the job of the compiler to ensure "You messed up, bud!" occurs exactly once in the string constant table? – juhist Feb 25 at 19:03
  • 2
    @juhist: C++ explicitly states that the compiler can choose to duplicate identical strings, or not. Most compilers have a flag to either do this, or not. I think, by default, most compilers do not merge strings, in case the app relies on them being unique. – Mooing Duck Feb 25 at 19:34
  • @MooingDuck I compiled a program having many std::printf("%p\n", "foo"); lines in it. All of them print the same address. – juhist Feb 25 at 20:55
  • @MooingDuck: While few compilers will guarantee that strings will always be merged, I think the normal reason is that upholding such a guarantee may impose extra build-time costs and complexity, and the savings in storage from merging strings may or may not be worthwhile. – supercat Feb 25 at 21:01
  • 1
    @juhist: Sure. In your compiler and your compiler flags. With my compiler and my flags, I get different addresses. – Mooing Duck Feb 25 at 21:39
3

Theoretically both approaches should consume the same amount of space and will probably result in the same code.


However, rather than that meaning "pick any one", what that actually means is that you should turn your attention to other factors when deciding which one to use.

As a rule of thumb you should generally avoid macros because they don't respect scoping rules due to how they work.
This can lead to some serious problems in certain cases.
See Why are preprocessor macros evil and what are the alternatives?.


However, I'd like to point out a third option: constexpr.

constexpr uint8_t motorMode1Register = 0x44;

constexpr implies const and is a better expression of the intent that the value should be capable of being evaluated at compile time.
constexpr literally means "this is a constant expression and thus can be evaluated at compile time".
As a result of this, constexpr variables can be used in situations where other variables cannot, such as template parameters.

See Constexpr vs Macros.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.