-1

As it was made know to me, adjacent members of the same type can have padding between them.

Even a structure like this could have it.

struct S{
    int x;
    int y;
};

First, why would this structure need padding anyway?

Second, why would padding be between the members of the same type?

Hrisip
  • 900
  • 4
  • 13
  • I think the crucial word is "*could*". It can have a padding, but I doubt any compiler would add one there (unless for example target platform only has 64-bit registers and `int` is 32-bit for some reason). – Yksisarvinen Nov 26 '20 at 16:49
  • @Yksisarvinen, what's the point of allowing it? – Hrisip Nov 26 '20 at 17:14
  • I don't get why the question got closed. https://stackoverflow.com/questions/4306186/structure-padding-and-packing does not answer my question – Hrisip Nov 26 '20 at 17:14
  • Standard allows it to allow compilers to optimize for speed. If you have for example 32-bit ints and `struct A{char x; int y;};`, on a machine with alignment to 4 bytes, it would be highly inefficient to store `y` right after `x` - each access to `y` would require reading two words and bitwise manipulation to restore that value. And for the case of two `int`s in a struct, what would be the point of disallowing it? Perhaps it's better, perhaps not. Sandard leaves that for compiler to decide. – Yksisarvinen Nov 26 '20 at 17:19
  • I think it answers your question, just not directly. Padding is a possibility. Compiler can add padding to your struct, or it may decide that it's perfectly fine to leave it as it is. What's important is that you should not rely on the memory layout of the struct. – Yksisarvinen Nov 26 '20 at 17:26
  • It does not answer still. Ok, so the real question is why not disallow padding between members of the same type? It is clearly better to disallow it. – Hrisip Nov 26 '20 at 18:02
  • Depending on your platform, disallowing padding when padding is necessary for aligned memory access would put the burden on the developer. I know developers that make padding be a compiler error, and manually add the padding themselves. – Eljay Nov 26 '20 at 18:17
  • @Eljay, now tell me, how disallowing padding **between members of the same type** would affect anything? – Hrisip Nov 26 '20 at 18:24
  • If the type requires aligned access, and having two of those types in memory without padding means that one of them does not have the proper alignment, then accessing the misaligned type would either cause a CPU fault, or a memory inefficiency, or require extra code to piecemeal load and shift the type. – Eljay Nov 26 '20 at 18:27
  • @Eljay, that's what the alignment of a structure is for. And padding has nothing to do with it. – Hrisip Nov 26 '20 at 18:31
  • The *padding* is how it becomes aligned when you have two of them side-by-side in memory. – Eljay Nov 26 '20 at 18:51
  • @Eljay, I don't know how to explain to you where you're wrong... – Hrisip Nov 26 '20 at 19:02
  • Consider a 10-byte `long double` that must be 16-byte aligned. `struct S { long double x; long double y; };` without padding between x and y would have y misaligned, even if an instance of S itself is properly 16-byte aligned. – Eljay Nov 26 '20 at 19:26
  • @Eljay, do you know of any platform where that would be the case(10-byte `long double` that must be 16-byte aligned)? And if there existed something like this, what would be the situation with arrays of that type? – Hrisip Nov 26 '20 at 19:35
  • Yes, I do of such a platform: Intel x86. It's a quite popular platform. Arrays of `long double` also have padding of the 10-byte long double to 16-byte alignment. – Eljay Nov 26 '20 at 20:23
  • @Eljay, and what happens is the size of this type becomes 16 bytes, so it's no longer 10 bytes. Thus there would be no padding between the members, since the members themselves include the padding you're talking about. So `struct S { long double x; long double y; };` would have the same layout as `long double a[2]`. ~~Therefore you're wrong. Q.E.D.~~ – Hrisip Nov 26 '20 at 20:36
  • Padding changes the size of the type. – Eljay Nov 26 '20 at 20:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225181/discussion-between-eljay-and-hrisip). – Eljay Nov 26 '20 at 21:47

1 Answers1

1

The C++ standard allows padding between sub objects (members and bases) of classes. As such, there "can" be padding because the standard allows it. The standard allows the padding because there are cases where that is necessary in order to satisfy the alignment requirement of the members.

The standard doesn't enumerate cases where padding isn't needed. It is entirely up to the language implementation to choose when to pad and when not to as long as the alignment requirements are satisfied.

why would this structure need padding anyway?

There is no practical reason (that I know of) why that class would need padding. However, that is not the only class definition that exists and there are other classes which may need padding.

eerorika
  • 232,697
  • 12
  • 197
  • 326