It is understood that arrays in C cannot insert padding between their elements . However, is there a rule saying that they cannot add a final complement at the end of the array as a whole?
i.e. Is this program guaranteed to give the same results everywhere?
As far as I can tell, adding an end byte or five to size a might not violate the array access rules in an attempt to erroneously optimize ( b[1][1] still exactly matches *(&b + sizeof(a) * 1 + 1) regardless of the size of the objects a contained in it, and access beyond the length of the contained a is UB in any case).
I cannot find anywhere else in the C standard where it actually says directly that the size of the array is the size of the element type times the number of elements. 6.5.3.4 only says that sizeof returns the "number of bytes" in the array (it gives sizeof array / sizeof array[0] as an example code, but it's just an example - it doesn't say that it should work, and it doesn't work give any details).
An implicit guarantee is useful for writing portable code that depends on exact data layouts, for example. transfer of packed RGB values:
typedef uint8_t RGB[3]; RGB * data = ...; glColorPointer(3, GL_UNSIGNED_BYTE, 0, data);
(OK, so OpenGL can take step values, so this is a bad example, but you get the point)
In this regard, I assume from the widespread notion (even for an example in the standard) that you can get the number of elements in an array with sizeof , that this is likely to be true everywhere, anyway, there are any known situations where this is not the case ?
source share