4

I'm trying the construct a struct in C++ as below:

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. Is there any way to realize it?

Thank you.

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Scarlett Huo
  • 41
  • 1
  • 2

1 Answers1

9

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.

Consult your compiler's documentation.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765