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

3

19

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?

Electrode answered 27/8, 2010 at 14:17 Comment(0)
P
13

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.

Pillar answered 27/8, 2010 at 14:20 Comment(4)
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).Susuable
@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.Pillar
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.Susuable
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).Salesin
L
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.

Lighterage answered 27/8, 2010 at 14:22 Comment(3)
Or, two paragraphs later, "There may be unnamed padding at the end of a structure or union."Pillar
That's not particularly useful, since the OP tagged both C++ and C.Solemn
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.Accentor
P
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.

Pung answered 27/8, 2010 at 14:26 Comment(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).Electrode

© 2022 - 2024 — McMap. All rights reserved.