简体   繁体   English

在C ++中更有效地分配struct的内存

[英]Allocate the memory of struct more efficiently in C++

I'm trying the construct a struct in C++ as below: 我正在尝试使用C ++构造一个struct,如下所示:

struct kmer_value {
    uint32_t count : 32;
    uint32_t  path_length : 32;
    uint8_t acgt_prev : 4;
    uint8_t  acgt_next : 4;
}

The struct currently takes the memory of 12 bytes, but I want to reduce the size to 9 bytes. 结构当前占用12个字节的内存,但我想将大小减小到9个字节。 Is there any way to realize it? 有没有办法实现它?

Thank you. 谢谢。

There's no portable solution. 没有便携式解决方案。 For GCC that would be 对于海湾合作委员会而言

struct __attribute__((packed)) kmer_value {
  uint32_t count : 32;
  uint32_t path_length : 32;
  uint8_t acgt_prev : 4;
  uint8_t acgt_next : 4;
};

In MSVC #pragma pack can achieve the same effect. 在MSVC #pragma pack可以达到同样的效果。

Consult your compiler's documentation. 请参阅编译器的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM