Activate structure memory more efficiently in C ++

I am trying to build a C ++ construct as shown below:

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

The structure currently occupies 12 bytes, but I want to reduce the size to 9 bytes. Is there any way to figure this out?

Thank.

+4
source share
1 answer

There is no portable solution. For GCC, which will 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 packyou can achieve the same effect.

Refer to your compiler documentation.

+9
source

Source: https://habr.com/ru/post/1680534/


All Articles