当前位置:网站首页>Details of enum enumeration in C and C + + 11

Details of enum enumeration in C and C + + 11

2020-12-08 10:37:12 Li Chungang

author : Li Chungang
Source :https://www.cnblogs.com/lcgbk/p/14101271.html

One 、 Preface

because C++ More enumerations are used in the project (enum), Under normal circumstances , Enumeration variables take up the size of an integer type , But the project enumerates (enum) Only need to use a byte size , Because it's code executed on an embedded device , Less resources , So if the enumeration is based on int If the size of the model is used , This is undoubtedly a waste of resources .

So I wonder if there is a way to control enumeration (enum) The way to take up memory . Fortunately , By looking up information , Find out C++11 The new feature of the new feature just adds a mechanism to control the enumeration size . So let's take a look at , enumeration (enum) stay C++11 What are the changes in standards ? as well as C and C++11 Enumeration in (enum) What's the difference? ?

Two 、C Enumeration in (enum)

2.1 C The size of the enumeration in

I said before , By default , enumeration enum The size of a shaping type is the size of , But there are many types of plastic surgery :int、long int、short int etc. , therefore enum We can't determine the size of . stay C in enum The size of is not explicitly controlled by the programmer , This size is the compiler's enumeration based on what we gave (enum) Assign the size to select the appropriate integer type of .

Here's an example :

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

#include <stdio.h>

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 find that the size of these two enumeration variables is different ,enum color2 Already exceeded 4 Bytes , So the compiler will adjust the size of the enumeration variable according to the actual value .

There is a drawback , When we define a structure with enumeration variables , When the value of an enumeration variable changes , Then the size of the structure may change with it . Heap space is requested for a struct without assigning a value to the enumeration , If the enumeration is assigned a value greater than 4 The value of bytes , Then the actual size of the structure has changed , In this way, there will be hidden dangers when we operate the structure in the later program .

2.2 C The value range of enumeration in

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

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

#include <stdio.h>

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

You can see , stay C Next enum Defined variables can be assigned values outside the specified range , But in C++11 This is not allowed . stay C++11 The compiler will not pass , yes C++11 Yes C in enum The optimization of the .

3、 ... and 、C++11 Enumeration in (enum)

3.1 c++11 Added enumeration size control in

stay C++11 The programmer can enumerate (enum) To explicitly control the size of the memory it occupies , The following example :

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

#include <stdio.h>

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

C++11 This feature can solve the problems I mentioned in the preface , You can save resources .

3.2 c++11 New enumeration type with scope

Like above 2.2 Section of the program if in C++11 The following errors will appear in the compilation of the standard :

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;

stay C++11 In the standard ,enum A defined variable cannot be assigned a value outside the specified range . This brings a lot of convenience for later debugging .

版权声明
本文为[Li Chungang]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012081037030090.html