#define vs enum in the embedded environment (how do they compile?)

This question was brought to death, and I agree that transfers are the way to go. However, I am curious how the enums are listed in the final code - #defines are just string replacements, but do the enumerations list anything in a compiled binary? Or they are both equivalent at this stage. When writing firmware and memory it is very limited, is there any advantage, no matter how small, to use #defines?

Thanks!

EDIT: according to the comment below, built-in, I mean a digital camera.

Thanks for answers! I'm all for transfers!

+6
c enums embedded firmware
source share
3 answers

Both are constant expressions in the terminology of the standard, so they should be fully evaluated at compile time by any compiler. To generate other code, you need a malicious pathological compiler.

+10
source share

An enum is just an integer, ultimately. The compiler propagates values ​​in the same way as for const .

+5
source share

It is impossible to say without profiling or measuring in any other way.

BUT, any worthy compiler will not show significant differences. In addition, you should always give preference to readable, typed code over efficient, unreadable, encoded code. Do not start optimizing for efficiency until you have proven two things:

  • you really need to increase efficiency.
  • the part of the program that you are changing was shown by the profiler bottleneck.
+4
source share

All Articles