Post

Enum

Enum

🌱 How is enum data stored in memory?

Khái niệm

Enum thường được lưu trữ trong bộ nhớ dưới dạng giá trị số nguyên (thường là int trong C). Mỗi giá trị trong enum được gán một số nguyên, mặc định bắt đầu từ 0 và tăng dần, hoặc do lập trình viên chỉ định. Khi chương trình chạy, các giá trị số nguyên này được sử dụng để biểu diễn, lưu trữ và so sánh các giá trị enum trong bộ nhớ, giúp chương trình xử lý các giá trị này hiệu quả hơn.

Ví dụ:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

enum level {
  LOW,    // = 0
  MEDIUM, // = 1
  HIGH    // = 2
};

int main() {
  // Create an enum variable and assign a value to it
  enum Level myVar = MEDIUM;

  // Print the enum variable
  printf("%d", myVar); 

  return 0;
}

// OUTPUT: 1

Theo mặc định, phần tử đầu tiên của enum được gán giá trị 0, các phần tử tiếp theo sẽ tự động tăng dần (mỗi phần tử có giá trị lớn hơn phần tử trước đó 1 đơn vị). Tuy nhiên, ta có thể chỉ định bất kỳ giá trị số nguyên nào cho từng phần tử trong enum theo ý muốn.

1
2
3
4
5
6
7
enum Level {
  LOW = 25,
  MEDIUM = 50,
  HIGH = 75
};

printf("%d", myVar); // Now outputs 50

💡 Note that if you assign a value to one specific item in an enum, the following items will automatically increment from that assigned value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

enum Level {
  LOW = 5,
  MEDIUM,
  HIGH,
  RED = 20,
  BLUE,
  YELLOW
};
  
int main() {
  enum Level myVar = MEDIUM;
  printf("myVar = MEDIUM: %d\n", myVar);
  
  myVar = YELLOW;
  printf("myVar = YELLOW: %d\n", myVar);
  
  return 0;
}

Output:

1
2
myVar = MEDIUM: 6
myVar = YELLOW: 22

Trả lời cho câu hỏi: “How is enum data stored in memory❓”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include <cstdint>

enum foo
{
    typeA = 0,
    typeB,
    typeC
};

enum two : int8_t
{
    typeD = 0,
    typeE,
    typeF
};

enum bar : uint64_t
{
    typeG = 0,
    typeH,
    typeK
};
int main()
{
    std::cout << "Size of <foo> type variable: " << sizeof(foo) << " bytes" << std::endl;
    std::cout << "Size of <bar> type variable: " << sizeof(two) << " bytes" << std::endl;
    std::cout << "Size of <bar> type variable: " << sizeof(bar) << " bytes" << std::endl;
    return 0;
}

Output:

1
2
3
Size of <foo> type variable: 4 bytes
Size of <two> type variable: 1 bytes
Size of <bar> type variable: 8 bytes

🌱 Allocating enum memory in C

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include <cstdint> // dùng để sử dụng các kiểu dữ liệu init8_t, uint8_t, ....

enum foo // default data type: int
{
    typeA = 0,
    typeB,
    typeC
};

enum fun : int8_t
{
    typeD = 0,
    typeE,
    typeF
};

enum bar : uint16_t
{
    typeG = 0,
    typeH,
    typeK
};

int main()
{
    foo myFoo_1 = typeA; // Khai báo một biến thuộc kiểu enum foo
    foo myFoo_2 = typeB;
    fun myFun__ = typeD;
    bar myBar_1 = typeK;
    bar myBar_2 = typeG;

    std::cout << "Address of <myFoo_1>: " << &myFoo_1 << std::endl;
    std::cout << "Address of <myFoo_2>: " << &myFoo_2 << std::endl;
    std::cout << "Address of <myFun__>: " << &myFun__ << std::endl;
    std::cout << "Address of <myBar_1>: " << &myBar_1 << std::endl;
    std::cout << "Address of <myBar_2>: " << &myBar_2 << std::endl;
    
    return 0;
}

Output:

1
2
3
4
5
Address of <myFoo_1>: 0x7ffff01f7220
Address of <myFoo_2>: 0x7ffff01f7224
Address of <myFun__>: 0x7ffff01f721b
Address of <myBar_1>: 0x7ffff01f721c
Address of <myBar_2>: 0x7ffff01f721e

This post is licensed under CC BY 4.0 by the author.