I recently learned that empty class have size 1 instead of zero.Why it has no byte alignment, in which it's size should be 4 in 32bit environment? What's the address of the next object?
Asked
Active
Viewed 136 times
1
-
1why do you think it should have alignment 4? – bolov May 17 '20 at 15:28
-
The "next object" doesn't mean anything unless the object in question is part of an array or inside a `struct`. – Pete Becker May 17 '20 at 15:33
-
Does this answer your questoin: [What is the size of an object of an empty class?](https://stackoverflow.com/questions/621616/c-what-is-the-size-of-an-object-of-an-empty-class). – sanitizedUser May 17 '20 at 15:34
-
1@bolov -- because in a 32-bit environment, pretty much everything does. – Pete Becker May 17 '20 at 15:34
-
1@PeteBecker no it doesn't https://godbolt.org/z/CkQmMc – bolov May 17 '20 at 15:38
-
Wow, fundamental logic, folks: "pretty much everything does" is **not** asserting that **everything** does. – Pete Becker May 17 '20 at 15:41
-
2@PeteBecker I don't understand your point. In x86-32 usually objects 4 bytes or larger have 4 byte alignment and objects smaller have alignment equal to their size. So that's why I asked why does the OP thinks an object with size 1 should have an alignment of 4. – bolov May 17 '20 at 15:46
-
@bolov: An object with size 1 **cannot** have alignment 4. Consider an array of them. – MSalters May 17 '20 at 15:50
-
@bolov unless you #pragma pack(1) your structs will almost certainly align every member at a 4 byte boundary regardless of its size – Big Temp May 17 '20 at 15:50
-
@bolov -- the point is that you asked "why do you think...", and I took a stab at answering **that question** -- why would a beginner think that things are all aligned at 4 bytes. I'll bet that at one point in your life you, too, were confused about things that you now take for granted. Demanding explanations for things like that is simply hostile. Granted, it's common here, but it's still hostile. – Pete Becker May 17 '20 at 15:51
-
1@PeteBecker I wasn't hostile. The point of my question was to understand what assumptions/misconceptions the OP has so that we can address them. – bolov May 17 '20 at 15:56
-
@BitTemp Sorry but it's totally untrue. Try to inspect addresses of members of an object of type `struct X { char a, b, c, d; };`. All the members will be very likely 1-byte distant. – Daniel Langr May 17 '20 at 17:35
-
What is _no byte alignment_? – Daniel Langr May 17 '20 at 17:36
-
@BigTemp : Daniel is right, that's not the case. Maybe if you allocate memory for it, individually, on the heap. – einpoklum May 17 '20 at 18:21
-
@DanielLangr Oops. I worded that sentence wrong. Yeah 4 consecutive chars will most likely be treated as a char array[4] but the moment you try introducing a larger type like `struct Y { char a; long long b; bool c; double d; };` you will get some sort of padding. On Intel C++ in x64 mode the struct and its members will all be aligned at an 8 byte boundary. – Big Temp May 17 '20 at 22:44
1 Answers
3
Because C++ simply does not guarantee 4-byte alignment, or word-alignment, of variables. If this is important to you, you can specify an alignment requirement using alignas
:
struct alignas(4) my_empty_struct {};
and now, the address of a my_empty_struct
variable would be a multiple of 4 - and so will its size, apparently.
Alternatively, you could pad your struct with a dummy field for alignment, yourself. The alignas
is a bit like padding with an inaccessible field.

einpoklum
- 118,144
- 57
- 340
- 684