H Bellamy
H Bellamy

Reputation: 22715

Clang and MSVC report different `sizeof` for the same struct

Clang: 3.8.0 MSVC: 19.00.24215.1 for x64

What could be causing this strange difference between the compilers? MSVC reports 12, but Clang reports 8! What's the correct/expected behaviour here? Does the standard have anything to say about this?

enum class CodeCompletionDeclKind {};

struct SwiftSemanticToken {
  unsigned ByteOffset;
  unsigned Length : 24;
  CodeCompletionDeclKind Kind : 6;
  bool IsRef : 1;
  bool IsSystem : 1;
};
static_assert(sizeof(SwiftSemanticToken) == 8, "Too big");

int main()
{
    std::cout << "Hello, world!\n";
}

Upvotes: 3

Views: 298

Answers (1)

WhiZTiM
WhiZTiM

Reputation: 21576

The size of your class object containing bit fields will be implementation defined.

class.bit/1:

...Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined. Bit-fields are packed into some addressable allocation unit. [ Note: Bit-fields straddle allocation units on some machines and not on others. Bit-fields are assigned right-to-left on some machines, left-to-right on others.  — end note ]

Upvotes: 2

Related Questions