-1

In C++, compilers insert padding between object data members, and apparently, the length of padding is equivalent to the largest data type.

Now to my understanding, if the compiler is going to 'pad' each smaller datatype to the size of the largest, why aren't objects simply standardized to a single data-type? (The largest one)

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
bigcodeszzer
  • 916
  • 1
  • 8
  • 27
  • [Data structure padding](https://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding) – crashmstr Nov 17 '15 at 16:58
  • "apparently, the length of padding is equivalent to the largest data type" - where on earth did you get that idea? – davmac Nov 17 '15 at 17:11
  • Only idea, if you know the maximum size of object, try set alignment on structure where are all object stored to this size, gcc: `__attribute__ ((aligned (40)))` will align objects to every 40 bytes. – vlk Nov 17 '15 at 17:17
  • "why aren't objects simply standardized to a single data-type" - wouldn't it be annoying if `char x[20];` actually allocated 320 bytes? And you had to write `x += 16;` instead of `x++` ? – M.M Nov 17 '15 at 20:09

1 Answers1

1

Typically, compilers will insert padding to align members to boundaries that match their size. Here 16-bits of padding would be inserted between a and b to ensure b is 32-bit aligned.

int16_t a;
/* 16 bits of padding */
int32_t b;

But if the struct members would naturally fill that space, there's no need to insert padding. Here each member is already aligned: a and b are 16-bit aligned and c is 32-bit aligned.

int16_t a;
int16_t b;
int32_t c;

Standardizing each variable to 32-bits would waste space.

Similarly, you could have a mix of variable sizes. Inserting a small amount of padding is more efficient than expanding all of the variables to a large uniform size.

int8_t a;
/* 8 bits of padding */
int16_t b;
int32_t c;
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I guess what I'm wondering is, why doesn't the c++ language enforce object constraints? I mean, if there are 17 bits in an object, and then 15 bits of will be inserted, shouldn't the compiler catch that? – bigcodeszzer Nov 18 '15 at 18:51
  • I'm not sure I understand the question. What do you mean "catch that"? Do you mean throw an error if there would be padding inserted? It's not an error for a programmer to write a struct in a way that requires padding. Padding isn't a sign of a problem that needs to be fixed, if that's what you're asking. – John Kugelman Nov 18 '15 at 19:47
  • Well I mean as a design constraint. On some level, objects are the containers where all of a program's data is stored. If objects are creating 'padding' by not aligning well, shouldn't the 'remainder' data (of each object) be pushed into another object? – bigcodeszzer Nov 18 '15 at 19:52
  • I agree, normally padding wouldn't be considered an error, but if you generating hundreds or even thousands of objects and each one is padding so many bits, is that not a form of memory leak? – bigcodeszzer Nov 18 '15 at 19:53
  • It is a waste of space, yes. The compiler doesn't force you to write efficient programs. If memory usage is a concern it's the programmer's responsibility to rearrange the struct in a more efficient manner. Note that this isn't always possible. Take the first example. There's no way to avoid the 16 bits of padding. If you swap `int16_t a` and `int32_t b` the padding will be moved from the middle to the end. If you have an array of these structs, the compiler will want to align every copy of `int32_t b`. It won't create a 48-bit struct. It will add padding one way or another. – John Kugelman Nov 18 '15 at 20:01
  • Yeah. Just seems like something that would be handled compiler-side, you know? Like a garbage-collector type object that pulls in all the 'excess' bits and globs them into an aligned garbage object. Do you know if Java does bit alignment also? – bigcodeszzer Nov 18 '15 at 20:22
  • That's a good question. Why don't you research it? If you can't find the answer, I encourage you to post another question here. I can't do an answer justice in a comment. – John Kugelman Nov 18 '15 at 20:28
  • http://stackoverflow.com/questions/33790934/c-custom-data-size-for-memory-alignment?noredirect=1#comment55349450_33790934 – bigcodeszzer Nov 18 '15 at 22:02