-6

Multiple sources across the internet, including this question at Stackoverflow, suggest that, there will never be any padding between elements of an array in C.

However, according to the 2nd Edition of Compilers: Principles, Techniques, and Tools, page 428 (logical) or 453 (physical):

On many machines, instructions to add integers may expect integers to be aligned, that is, placed at an address divisible by 4. Although a character array (as in C) of length 10 needs only enoughbytes to hold ten characters, a compiler may allocate 12 bytes to get the proper alignment, leaving 2 bytes unused.

To verifiy this, I wrote a small C++ program to print the addresses of the char array elements, and there is no padding.

EDIT: My question was, whether or not, padding will exist between array elements. The answers have explained that the padding mentioned in the book, will be at the end of the array. Thanks!

  • 8
    There's no contradiction between your two quotes. Even if a `char[10]` gets `12` bytes assigned to it, it'll have the padding after the end of the array, not between the elements. – Nathan Pierson Jun 08 '23 at 14:38
  • Clarify **what**? You need to be specific. – Andrew Henle Jun 08 '23 at 14:38
  • 2
    Do you have a question? – Toby Speight Jun 08 '23 at 14:43
  • 2
    [Array declaration](https://en.cppreference.com/w/cpp/language/array) "_...A declaration of the form T a[N];, declares a as an array object that consists of N __contiguously allocated__ objects of type T. ..."_ . – Richard Critten Jun 08 '23 at 14:44
  • *To verifiy this, I wrote a small C++ program to print the addresses of the char array elements* -- At the very least, in lieu of an actual question, you should post your verification ode. – PaulMcKenzie Jun 08 '23 at 14:45
  • Sorry for not being clear enough. Yet the answers have clarified my doubt. Thanks! – Kishore Suresh Jul 03 '23 at 04:52

3 Answers3

3

Although a character array (as in C) of length 10 needs only enoughbytes to hold ten characters, a compiler may allocate 12 bytes to get the proper alignment, leaving 2 bytes unused.

That is a misleading statement. If at file scope you declare

int before;
char my_array[10];
int after;

then the size of the array is 10. Period. The compiler might leave unassigned space before or after the storage for my_array, but that's at the compiler's discretion, and such extra space is not part of the array (and I don't usually refer to it as "padding" in such cases, but YMMV).

If you declare a structure containing an array:

struct my_struct {
    int before;
    char my_array[10];
    int after;
}

then the compiler may lay out the structure with padding between my_array and after, but the size of the array is still 10. The padding belongs to the structure, not to the array.

Similar applies for arrays whose element type itself has an alignment requirement larger than 1 byte. The beginning of the array may be assigned such that there is unused space before, but that space is not part of the array.

To verifiy this, I wrote a small C++ program to print the addresses of the char array elements, and there is no padding.

There will definitely be no padding between elements of the same array. Both C and C++ are very clear on this, and the source you quote does not say otherwise.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
2

The issue you are referring to is related to structure padding, rather than padding between elements of an array in C or C++. Padding in this context refers to the insertion of unused bytes between structure members to align them properly in memory.

In the case of arrays, elements are stored consecutively in memory without any padding between them. Each element occupies the exact amount of space required for its data type.

The passage you mentioned from the book "Compilers: Principles, Techniques, and Tools" is discussing structure padding, which is not directly related to arrays. When dealing with structures, compilers may insert padding bytes between members to align them according to the alignment requirements of the target machine architecture. This alignment can improve performance, as some processors may have alignment restrictions for certain data types.

1

The text you quote talks about (a) alignment needed for some integer type and (b) padding after a character array. This mention of two different types suggests the padding is not for the character array but is for some integer type. A clue for this is in the paragraph prior to your quote, which ends:

Storage for an aggregate type, such as an array or structure, must be large enough to hold all its components.

So the quote may discussing padding within aggregates generally. A character array does not need any padding by itself, but if it is in a structure which also has an integer type, padding may be needed after the character array so the next member of the structure has the alignment it needs (or so the whole structure has the size needed to make its size a multiple of the alignment requirements of its members). The quote may also be discussing placing multiple objects generally, as in arranging several objects that the compiler will place in stack space or other storage.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312