Will an array have any padding bytes?

H

hpsMouse

This problem has confused me for a long time. Think about the following simple declaration:

char arr[3][4][5];

Will arr[j][k] always be equivalent to *(&arr[0][0][0] + i * 4 * 5 + j *5 + k) ?

All the books I read says it's true. But when I turn to ISO C++98/03 I don't find enough clue about it.

The only paragraph I found about this topic is 8.3.4/1, in which it says:

An object of array type contains a contiguously allocated non-empty set of N sub-objects of type T.

It means there shall be no padding between two elements in the same array. However it didn't say whether there can be any padding bytes at the end. Ifa tailing padding exists, sizeof(char[5]) may be greater than 5, and the method to locate arr[j][k] may be incorrect.

Did I miss anything, or the standard just said nothing about such a padding?
 
V

Victor Bazarov

This problem has confused me for a long time. Think about the
following simple declaration:

char arr[3][4][5];

Will arr[j][k] always be equivalent to *(&arr[0][0][0] + i * 4 * 5
+ j * 5 + k) ?
Yes.

All the books I read says it's true. But when I turn to ISO C++98/03
I don't find enough clue about it.

The only paragraph I found about this topic is 8.3.4/1, in which it
says:

An object of array type contains a contiguously allocated non-empty
set of N sub-objects of type T.

It means there shall be no padding between two elements in the same
array. However it didn't say whether there can be any padding bytes
at the end.


It didn't say that because it would be superfluous. An array of 5 char
contains 5 [sub]objects of type 'char'. Nothing more, nothing less.
Why a simple definition like that is so difficult to accept? Why the
suspicion that something might have been left unsaid?
If a tailing padding exists, sizeof(char[5]) may be
greater than 5, and the method to locate arr[j][k] may be
incorrect.

Did I miss anything, or the standard just said nothing about such a
padding?


Yes, it "just said nothing" but not because it wants you to wonder.
It's because there *is* (or *shall be*) no padding.

V
 
A

Alf P. Steinbach /Usenet

* hpsMouse, on 21.06.2011 15:20, posted with Quoted Printable just in order to
make things more interesting:
This problem has confused me for a long time. Think about the following simple declaration:

char arr[3][4][5];

Will arr[j][k] always be equivalent to *(&arr[0][0][0] + i * 4 * 5 + j * 5 + k) ?

All the books I read says it's true. But when I turn to ISO C++98/03 I don't find enough clue about it.

The only paragraph I found about this topic is 8.3.4/1, in which it says:

An object of array type contains a contiguously allocated non-empty set of N sub-objects of type T.

It means there shall be no padding between two elements in the same array. However it didn't say whether there can be any padding bytes at the end. If a tailing padding exists, sizeof(char[5]) may be greater than 5, and the method to locate arr[j][k] may be incorrect.

Did I miss anything, or the standard just said nothing about such a padding?


You missed the definition of `sizeof`.

The size of an array of N elements is N times the size of an element.

When an element is itself an array, that does not leave room for any padding.


Cheers & hth.,

- Alf
 
H

hpsMouse

* hpsMouse, on 21.06.2011 15:20, posted with Quoted Printable just in order to
make things more interesting:
This problem has confused me for a long time. Think about the followingsimple declaration:
char arr[3][4][5];
Will arr[j][k] always be equivalent to *(&arr[0][0][0] + i * 4 * 5 +j * 5 + k) ?

All the books I read says it's true. But when I turn to ISO C++98/03 I don't find enough clue about it.
The only paragraph I found about this topic is 8.3.4/1, in which it says:
An object of array type contains a contiguously allocated non-empty setof N sub-objects of type T.
It means there shall be no padding between two elements in the same array. However it didn't say whether there can be any padding bytes at the end.. If a tailing padding exists, sizeof(char[5]) may be greater than 5, and the method to locate arr[j][k] may be incorrect.

Did I miss anything, or the standard just said nothing about such a padding?

You missed the definition of `sizeof`.

The size of an array of N elements is N times the size of an element.

When an element is itself an array, that does not leave room for any padding.

Cheers & hth.,

- Alf


You are right. I missed the definition of 'sizeof'.

Thanks for the help.
 
H

hanukas

This problem has confused me for a long time. Think about the following simple declaration:

char arr[3][4][5];

Will arr[j][k] always be equivalent to *(&arr[0][0][0] + i * 4 * 5 + j* 5 + k) ?

All the books I read says it's true. But when I turn to ISO C++98/03 I don't find enough clue about it.

The only paragraph I found about this topic is 8.3.4/1, in which it says:

An object of array type contains a contiguously allocated non-empty set of N sub-objects of type T.

It means there shall be no padding between two elements in the same array.. However it didn't say whether there can be any padding bytes at the end. If a tailing padding exists, sizeof(char[5]) may be greater than 5, and themethod to locate arr[j][k] may be incorrect.

Did I miss anything, or the standard just said nothing about such a padding?


Memory allocation offset and size often do indeed have alignment which
varies between implementations. A good rule-of-thumb: you shouldn't
care.

When you do something super duper special for some known platform,
knowing some of the internal details is helpful: cache associativity,
cache line size, memory (allocation) alignment, smallest allocation
block and such things can help but these topics are not really C++, in
context like that the C++ is just a tool not the primary topic. Just
forget it unless you really need to.
 
A

Andrey Tarasevich

You missed the definition of `sizeof`.

The size of an array of N elements is N times the size of an element.

When an element is itself an array, that does not leave room for any padding.

Or, in other words, any padding that might be present in the array
always belongs to the element itself (i.e. it is included into the
`sizeof` of the element). No additional array-specific padding can ever
be introduced by the array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top