c++csizeofdatamember

Is the size of a struct with one member the same as the size of the member?


Given

struct S {
  SomeType single_element_in_the_struct;
};

Is it always true that

sizeof(struct S) == sizeof(SomeType)

Or it may be implementation dependent?


Solution

  • This will usually be the case, but it's not guaranteed.

    Any struct may have unnamed padding bytes at the end of the struct, but these are usually used for alignment purposes, which isn't a concern if you only have a single element.