操作原始内存的正确数据类型

Correct data type for manipulating raw memory

我有一个 class 可以促进 encoding/decoding 原始内存。我最终存储了一个 void 指针指向内存和被引用的字节数。我担心别名问题以及使编码正确的位移位操作。本质上,对于 WHAT_TYPE 我应该使用 charunsigned charint8_tuint8_tint_fast8_tuint_fast8_tint_least8_t,还是uint_least8_t?规范中是否有明确的答案?

class sample_buffer {
    size_t index; // For illustrative purposes
    void *memory;
    size_t num_bytes;
public:
    sample_buffer(size_t n) :
        index(0),
        memory(malloc(n)),
        num_bytes(memory == nullptr ? 0 : n) {
    }
    ~sample_buffer() {
        if (memory != nullptr) free(memory);
    }
    void put(uint32_t const value) {
        WHAT_TYPE *bytes = static_cast<WHAT_TYPE *>(memory);
        bytes[index] = value >> 24;
        bytes[index + 1] = (value >> 16) & 0xFF;
        bytes[index + 2] = (value >> 8) & 0xFF;
        bytes[index + 3] = value & 0xFF;
        index += 4;
    }
    void read(uint32_t &value) {
        WHAT_TYPE const *bytes = static_cast<WHAT_TYPE const *>(memory);
        value = (static_cast<uint32_t>(bytes[index]) << 24) |
                (static_cast<uint32_t>(bytes[index + 1]) << 16) |
                (static_cast<uint32_t>(bytes[index + 2]) << 8) |
                (static_cast<uint32_t>(bytes[index + 3]);
        index += 4;
    }
};

在 C++17 中:std::byte。这种类型正是出于这个原因而专门创建的,以传达所有正确的语义。此外,它具有您需要在原始数据上使用的所有运算符(例如您示例中的 <<),但是 none 您不需要的运算符。


C++17 之前:unsigned char。该标准将对象表示定义为 unsigned char 的序列,因此它是一个很好用的类型。此外,由于 ,使用 unsigned char* 可以防止错误使用 char* 导致的许多错误,该 char* 引用原始字节,就好像它是一个字符串并将其传递给像 [=16 这样的函数=].

如果你真的不能使用unsigned char,那么你应该使用charunsigned charchar 都是您可以作为别名的类型,因此它们都优于任何其他整数类型。