0

I have to store the multiples values of same type.

I opted for array.

But that array values will be used in the multiple parts of the program.

So i decided to access the array values using Macro.

arr[VALUE_1]

Here VALUE_1 is the macro

Another workaround for macro is enums

enums{
VALUE_1,
VALUE_2
}

I wonder that enums will consume some memory.

Which is better for efficient programming?

VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38
  • 1
    What language are we talking about? – venerik Jun 18 '14 at 09:01
  • @venerik Added the C++ tag – VINOTH ENERGETIC Jun 18 '14 at 09:02
  • Even if the enum were to consume memory, we'd be talking about something like 8 bytes total. Unless you're on an incredibly memory-constrained system, it's not even worth thinking about. – user2357112 Jun 18 '14 at 09:04
  • May be a duplicate of http://stackoverflow.com/questions/17125505/what-makes-a-better-constant-in-c-a-macro-or-an-enum or http://stackoverflow.com/questions/4767667/c-enum-vs-const-vs-define – dragosht Jun 18 '14 at 09:06

2 Answers2

4

I wonder that enums will consume some memory.

Enumerators don't exist at runtime. They are not objects, but rather constants. And their names are prvalues.

They are substituted by their values right at compile-time. So there is no performance difference by any means.

And follow the general guideline: Prefer enum to macros.

Columbo
  • 60,038
  • 8
  • 155
  • 203
2

In C++, enums are virtually identical to constants at runtime, so there won't be a difference with macros when the programme is running. However, from a source stand point, enums are understood by the compiler as a set of related values bundled into a single type. This can be very useful for type checking your code. Besides, it will force you into properly handle your constants, if you weren't already doing it, for a better overall code base.

Just use enums.

didierc
  • 14,572
  • 3
  • 32
  • 52