[C / C + +] details of enum enumeration in C and C + + 11

Time:2021-8-29

Author: Li Chungang
source: https://www.cnblogs.com/lcgbk/p/14101271.html

catalogue
  • 1、 Foreword
  • 2、 Enumeration in C (enum)
    • 2.1 size of enumeration in C
    • 2.2 value range of enumeration in C
  • 3、 Enumeration in C + + 11 (enum)
    • 3.1 new enumeration size control in C + + 11
    • 3.2 C + + 11 adds scope enumeration types

1、 Foreword

Because there are relatively many enums in C + + projects, normally, enum variables occupy the size of an integer type, but enum in the project only needs to use the size of one byte. Because it is code executed on embedded devices and has relatively few resources, if enums are used according to the size of int type, This is undoubtedly a waste of resources.

So I wonder if there is a way to control the memory occupied by enum. Fortunately, by looking up the data, it is found that the new feature of C + + 11 just adds the mechanism to control the enumeration size. Then let’s take a look at the changes of enum in C + + 11 standard? And what is the difference between enum in C and C + + 11?

2、 Enumeration in C (enum)

2.1 size of enumeration in C

As mentioned earlier, by default, the enum size is the size of an integer type, but there are many integer types: int, long int, short int, etc., so the enum size is still uncertain. In C, the size of enum can not be explicitly controlled by the programmer. This size is selected by the compiler according to the size we assign to enum.

Here is an example:

/*****************************************************************************
** Copyright © 2020 lcg. All rights reserved.
** File name: enum.c
**Description: test the characteristics of enum
** Author: lcg
** Version: 1.0
** Date: 2020.12.08
*****************************************************************************/

#include 

enum color1
{
    RED = 0,
    GREEN,
    BLUE
};

enum color2
{
    GRAY = 0x1122334455,
    YELLOW,
    PURPLE
};

int main(int argc, char *argv[])
{
    printf("enum color1: %d\n", sizeof(enum color1));
    printf("enum color2: %d\n", sizeof(enum color2));
    return 0;
}

Output results:

enum color1: 4
enum color2: 8

We found that the sizes occupied by the two enumeration variables are different. Enum color2 has exceeded 4 bytes, so the compiler will adjust the size of the enumeration variables according to the actual value.

This has a disadvantage. When we define a structure with enumerated variables, when the value of the enumerated variables changes, the size of the structure may change. When the enumeration is not assigned a value, the heap space is applied for the structure. Later, if the enumeration is assigned a value of more than 4 bytes, the actual size of the structure has changed at this time, so there will be hidden dangers when our later programs operate on the structure.

2.2 value range of enumeration in C

We continue to use the above routine to illustrate the value range:

/*****************************************************************************
** Copyright © 2020 lcg. All rights reserved.
** File name: enum.c
**Description: test the characteristics of enum
** Author: lcg
** Version: 1.1
** Date: 2020.12.08
*****************************************************************************/

#include 

enum color1
{
    RED = 0,
    GREEN,
    BLUE
};

enum color2
{
    GRAY = 1,
    YELLOW,
    PURPLE
};

int main(int argc, char *argv[])
{
    enum color1 myClolor1;
    
    myClolor1 = RED;
    printf("myClolor1: %d\n", myClolor1);
    
    myClolor1 = GRAY;
    printf("myClolor1: %d\n", myClolor1);
    
    myClolor1 = 10;
    printf("myClolor1: %d\n", myClolor1);
    
    return 0;
}

Output results:

myClolor1: 0
myClolor1: 1
myClolor1: 10

It can be seen that variables defined by enum under C can be assigned values outside the specified range, but this is not allowed under c + + 11. The compilation fails in C + + 11, which is the optimization of enum in C + + 11.

3、 Enumeration in C + + 11 (enum)

3.1 new enumeration size control in C + + 11

In C + + 11, programmers can explicitly control the memory occupied by enums, as shown in the following example:

/*****************************************************************************
** Copyright © 2020 lcg. All rights reserved.
** File name: enum.cpp
**Description: test the characteristics of enum
** Author: lcg
** Version: 1.2
** Date: 2020.12.08
*****************************************************************************/

#include 

enum color1 : char {
    RED = 0,
    GREEN,
    BLUE
};

enum color2 : int {
    GRAY = 1,
    YELLOW,
    PURPLE
};

int main(int argc, char *argv[])
{
    printf("enum color1: %d\n", sizeof(enum color1));
    printf("enum color2: %d\n", sizeof(enum color2));
    
    return 0;
}

Output results:

enum color1: 1
enum color2: 4

Features like C + + 11 can well solve the situations I mentioned in the preface and save resources.

3.2 C + + 11 adds scope enumeration types

For example, if the program in Section 2.2 above is compiled in the C + + 11 standard, the following errors will appear:

1.cpp:33:15: error: cannot convert ‘color2’ to ‘color1’ in assignment
     myClolor1 = GRAY;
               ^
1.cpp:36:15: error: invalid conversion from ‘int’ to ‘color1’ [-fpermissive]
     myClolor1 = 10;

In the C + + 11 standard, variables defined by enum cannot be assigned values outside the specified range. This brings a lot of convenience for later debugging.