Why use array size 1 instead of pointer?

后端 未结 6 800
滥情空心
滥情空心 2020-12-02 09:04

In one C++ open source project, I see this.

struct SomeClass {
  ...
  size_t data_length;
  char data[1];
  ...
}

What are the advantages

6条回答
  •  抹茶落季
    2020-12-02 09:33

    1. The structure can be simply allocated as a single block of memory instead of multiple allocations that must be freed.

    2. It actually uses less memory because it doesn't need to store the pointer itself.

    3. There may also be performance advantages with caching due to the memory being contiguous.

提交回复
热议问题