Eagle
Eagle

Reputation: 3474

Numerical constant in C++

I saw a lot of questions concerning what should be preferred between static const vs #define vs enum.

In none of them was discussed the aspect of resources, which is important in embedded system.

I don't like to use #define but it doesn't consume resources like static. What about enum, does in consume RAM\ROM?

In addition, I was told that if i use e.g. const int it will not consume either RAM nor ROM, assuming I do not use pointer\reference to this variable. Is this true?

If this is true, wouldn't it be a good solution to use const int inside a namespace in order to save resources?

Remark: I am using C++ 2003 standard and can't use 2011 standard. (I already saw enum class and constexpr in 2011, but i can't use them.)

Upvotes: 0

Views: 378

Answers (3)

Niall
Niall

Reputation: 30606

The definition of a type does not, by itself, consume memory (ROM); except for debug information (if present).

So, simply;

enum MyValues {
  Value1,
  Value2
};

Will not consume ROM. However, instantiations of the type (actual objects) will consume memory (loaded from ROM and then later in RAM).

MyValues val1 = Value1;

Above, val1 consume memory.

So what is the difference to the #define or const int alternatives?

Clarity.

The preprocessor (with the #define) and the compiler (with the const int) are smart enough to do pretty much exactly the same thing in most cases (outside of things such as taking the address of the const int). The enum is language construct that groups related values together, it's not the only one, but it is a very natural one.

Upvotes: 0

anatolyg
anatolyg

Reputation: 28241

... if i use e.g. const int it will not consume either RAM nor ROM, assuming I do not use pointer\reference to this variable

Exactly. If you use #define or enum, the compiler has no reason to put your constant in data memory - it will always put it in your code. If you use const int, the compiler will do the same only (mostly?) if your code has no references to it (i.e. when it's not ODR-used).

There are benign-looking constructs that silently introduce references into your code, so in practice you cannot assume const int will behave the same way as enum. For example:

const int UNIVERSE = 42;
int calc = ...;
int answer = max(calc, UNIVERSE);

If you use max from the std namespace (like you should), it will introduce a const reference to UNIVERSE to your code, which may add 42 to your data ROM.

Also:

bool condition = ...;
int answer = condition ? 0 : UNIVERSE;

The same problem here (see here for details); your code may behave differently with const int than either of #define or enum.

C++11 introduced constexpr, which will avoid these subtle problems. So if you cannot use C++11 and want maximum performance (or minimum memory wasted), you should use #define or enum. Also, if you want your constants to have type other than int, you're stuck with #define:

#define UNIVERSE (uint64_t)42

Upvotes: 1

Lundin
Lundin

Reputation: 213593

Your assumptions make no sense. All data has to be stored somewhere. You cannot store data in thin air.

The difference between #define and const is that the former tends to embed the constants with the code memory, while the latter may possibly give the variable a dedicated address in data memory. In either case, they will consume the same amount of memory.

If you don't enable any compiler optimizations, it is however possible that variables stored at dedicated address in data memory will result in a ever so slight change in memory consumption, it the case where the instruction fetching that data could as well have contained the raw value.

But these kind of micro-optimizations is not something you should even consider. We are talking about ROM memory, which you should have plenty of.

The advantage of such constants allocated at a fixed address, is that you can take their address and also view their values in a debugger. So they aren't necessarily the same thing as #defined ones.

Variables that are not used by your program will get optimized away.

Upvotes: 0

Related Questions