| Can C arrays contain padding in between elements? I heard a rumor that, in C, arrays that are contained inside structs may have padding added in between elements of the array. Now obviously, the amount of padding could not vary between any pair of elements or calculating the next element in an array is not possible with simple pointer arithmetic. This rumor also stated that arrays which are not contained in structures are guaranteed to contain no padding. I know at least that part is true. So, in code, the rumor is:
I'm pretty certain that Does anyone know if this rumor is addressed in any C standard? edit: I understand that there may be padding between the end of the array | |
| c padding arrays | by wintermute on 6/30/2009 11:53:20 PM |
Consider:
Assuming shorts are 16 bits and you're on 32 bits, the size will probably be 8 bytes as each struct members tends to be aligned a word (32 bit in this case) boundary. I say "probably" because it is implementation specific behaviour that can be varied by compiler flags and the like. It's worth stressing that this is implementation behaviour not necessarily defined by the C standard. Much like the size of shorts, ints and longs (the C standard simply says shorts won't be larger than ints and longs won't be smaller than ints, which can end up as 16/32/32, 16/32/64, 32/32/64 or a number of other configurations). |
| by cletus on 6/30/2009 11:57:04 PM |