18

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?

Puppy
  • 144,682
  • 38
  • 256
  • 465
user396672
  • 3,106
  • 1
  • 21
  • 31

3 Answers3

14

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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 3
    _which isn't a concern if you only have a single element_ Actually handling a single element in a struct may still be padded for alignment / performance reasons. Typically example is a `char array[6]` on a 32-bit system, may pad with 2 additional bytes to be a whole multiple of 4-bytes (32-bits). – mctylr Aug 27 '10 at 14:48
  • 1
    @mctylr: I'd be a bit surprised if that behavior was typical. There can be no padding between elements of an array, so a `char[6]` will have a size of six bytes and a `char[6][2]` will have a size of 12 bytes. I don't see the benefit of aligning a type `struct S { char c[6]; };` differently than the element of type `char[6]` that it contains. – James McNellis Aug 27 '10 at 15:04
  • 1
    I meant treat it as `struct S = {char array[6]; char pad[2];}` Since the a 32-bit CPU is going to access 8 bytes anyhow, if `struct S` is itself within a second array `struct S aS[]` the elements of the array are then aligned on alignment boundaries. I wouldn't expect it to be common, but possible. I've seen people assume no such padding when directly writing structs via fwrite / write to a binary file or network socket, and it can and does break. – mctylr Sep 03 '10 at 15:01
  • Additional case: Consider a word-addressed machine where sizeof(char*) > sizeof(int*). (The char pointer has to have additional bits to select with a word.). The standard guarantees that all pointers to struct have the same size, so a struct will typically always be word-aligned (you could choose to say all struct pointers will be unaligned, but that was typically not the choice on such machines). If all structs *are* word aligned, sizeof(struct S { char a;}) will be greater than sizeof(char). – Martin Bonner supports Monica Oct 10 '17 at 09:28
13

It does not have to be equal, due to structure padding.

section 6.7.2.1 in the C99 standard states that "There may be unnamed padding within a structure object, but not at its beginning".

This is refered to as structure padding. Paddings may be added to make sure that the structure is properly aligned in memory. The exakt size of a structure can change if you change the order of its members.

Silfverstrom
  • 28,292
  • 6
  • 45
  • 57
  • 1
    Or, two paragraphs later, "There may be unnamed padding at the end of a structure or union." – James McNellis Aug 27 '10 at 14:27
  • That's not particularly useful, since the OP tagged both C++ and C. – Puppy Aug 27 '10 at 14:31
  • 3
    But does padding come into play if there's only one member of the struct? That's the OP's question, and so far I haven't found anything definitive in n1256. For example, per gcc, the size of a struct containing a single 3-element array of char is 3; no padding. Add an int following the array, and the size of the array is 8, so there's one byte of padding between the elements. – John Bode Aug 27 '10 at 19:12
1

It depends on the packing of your compiler. Usually the size of a structure is divisible by the word-length of your system (e.g. 4 byte == 32 bit).

So you will often have sizeof(struct S) > sizeof(SomeType)

For most compilers you can modify the packing size using compiler pragmas. If you set #pragma pack(1) then the sizes should be equal.

SebastianK
  • 3,582
  • 3
  • 30
  • 48
  • 1
    usually it doesn't depend on packing. Alignment of a structure usually is calculated as a strongest alignment of structure members. If all members are chars then alignment in most cases will be 1 (regardless to default packing). – user396672 Aug 27 '10 at 14:41