Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

enum or const int: which is better? 1

Status
Not open for further replies.

dds82

Programmer
Jun 5, 2002
251
US
If I have a number of variables that are not going to change, is it better to declare them like this

Code:
enum EnumName {
    var1=1,
    var2,
    var3
};

or like this?

Code:
const int var1=1;
const int var2=2;
const int var3=3;

Are there any differences/optimizations that would make one preferable to the other?

Thanks.
 
Enumerations are used for when you want to declare a variable that will have different states. They are used such that you can limit a certain variable to those limited values. So if you were using a variable that could ONLY have the values 1, 2, or 3 and you wanted to check which it had, then use the enum. If you are using the const ints for other purposes than the the state of a variable, const int is the correct choice.

As for optimizations, I do not think there really is any difference at all between them. The keyword enum simply creates a set of interger constants which is exactly what many const int calls does. The only difference is enum might take up a SMALL amount of more space (prolly just 4 bytes of an address to the set) to have the set itself. But that is neglibile, so don't worry about that. As for speed of access, it is possible that the const ints could be placed in registers and stay there, and the enum is in memory. I am not sure about that, but if you want optimizations, usually the compiler can handle where to put the variables itself.

Hope some of that made sense and helped...
-Skatanic
 
use #define instead.
enum is meant to be something other then declaring constants.
const you don't need. the only time I use const is when
passing a pointer to a function, thus avoiding that the
data pointed to can be changed within the function.
and I havn't found other purpose for const.
 
Actually #define macros and constants are considered obsolete in C++. It allows for a lot of implicit casting that you might not know is happening. It is better to use the const constants because you specify an exact type and that is actually stored in memory. All #define does is cut and paste whatever it is you set the #define to into the code when it is compiled.

But... To each their own...

-Skatanic
 
fheyn,

I do not completely agree with you for the following reason:

#define is processed by the preprocessor, and the compiler has no knowledge of it at all.
Look at the following (very stupid) code:

Code:
#include <windows.h>
#include <stdio.h>

#define blabla &quot;Some text to be displayed&quot;
#define perc_s &quot;%s\n&quot;

void main ( )
{ printf ( perc_s, blabla );
  printf ( perc_s, blabla );
  printf ( perc_s, blabla );
  printf ( perc_s, blabla );
  printf ( perc_s, blabla );
  printf ( perc_s, blabla );
  printf ( perc_s, blabla );
  printf ( perc_s, blabla );
  printf ( perc_s, blabla );
  printf ( perc_s, blabla );
  printf ( perc_s, blabla ); }

The compiler is seeing eleven times printf ( &quot;%s\n&quot;, Some text to be displayed&quot;);
The result is that the same string occurs eleven times in the object file, the compiler has no knowledge of the string already being familiar.

If you replace the #define lines with:
const char blabla[] = &quot;Some text to be displayed&quot;;
const char perc_s[] = &quot;%s\n&quot;;
both of the literals occur only once in the object file.

Marcel
 
Both Skatanic and MKuiper are correct, especially if you view the preprocessor and the compiler as one.

Here is a summary.

1) Use enum if you are limiting a variable to a set of values.

2) const is better than #define because

2.1) #define has not type, it simply get expanded by the preprocessor before the source code is submitted to the compiler.

2.2) Because of code expansion, #define wastes memory space.

Some intelligent compiler maybe able to determine the use of #define and could convert it to a const for you, hopefully.

Cheng
 
The main difference between enum and const is that enum is only compiler definition without allocation of the memory like #define is preprocessor definition, while
const int var1=1;
is real memory allocation. var1 is ordinary integer variable, keyword &quot;const&quot; means only, that it is readonly. const wastes the memory by definition of integer data and saves memory by character strings (CYeh).

More delicate is difference between enum and #define. #define is preprocessor definition and is totally hidden from compiler (MKuiper). enum is compiler definition, it can be used for limitting of integer assignments (Skatanic), also enum definitions are visible in debugger, where #define's are not. But enum can define only integer data. Also main purpose of #define is conditional compilation, so I don't agree with Skatanic, that they are obsolete.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top