views:

253

answers:

3

Following an hot comment thread in another question, I came to debate of what is and what is not defined in C99 standard about C arrays.

Basically when I define a 2D array like int a[5][5], does the standard C99 garantee or not that it will be a contiguous block of ints, can I cast it to (int *)a and be sure I will have a valid 1D array of 25 ints.

As I understand the standard the above property is implicit in the sizeof definition and in pointer arithmetic, but others seems to disagree and says casting to (int*) the above structure give an undefined behavior (even if they agree that all existing implementations actually allocate contiguous values).

More specifically, if we think an implementation that would instrument arrays to check array boundaries for all dimensions and return some kind of error when accessing 1D array, or does not give correct access to elements above 1st row. Could such implementation be standard compilant ? And in this case what parts of the C99 standard are relevant.

A: 

If the array is static, like your int a[5][5] array, it's guaranteed to be contiguous.

ThiefMaster
Might be a good idea to investigate the meaning of the word "static" in C.
anon
+3  A: 

I've added some more comments to our original discussion.

sizeof semantics imply that int a[5][5] is contiguous, but visiting all 25 integers via incrementing a pointer like int *p = *a is undefined behaviour: pointer arithmetics is only defined as long as all pointers invoved lie within (or one element past the last element of) the same array, as eg &a[2][1] and &a[3][1] do not (see C99 section 6.5.6).

In principle, you can work around this by casting &a - which has type int (*)[5][5] - to int (*)[25]. This is legal according to 6.3.2.3 §7, as it doesn't violate any alignment requirements. The problem is that accessing the integers through this new pointer is illegal as it violates the aliasing rules in 6.5 §7. You can work around this by using a union for type punning (see footnote 82 in TC3):

int *p = ((union { int multi[5][5]; int flat[25]; } *)&a)->flat;

This is, as far as I can tell, standards compliant C99.

Christoph
He could pass the int(*)[25] to another function legally, right? (as long as he doesn't dereference it within the same scope as the original array).
Daniel Stutzbach
@Daniel: that would indeed be the typical use (and would be coherent with right to call memset or memcpy). But from reading C99, I do not really succeed making my mind on the subject. For now I will probably accept @Secure answer, because I understand the contiguous part exactly as he explained it.
kriss
Use of a union for this is undefined behavior. With unions, you can only read from the most-recently-written member.
R..
+3  A: 

We should begin with inspecting what int a[5][5] really is. The types involved are:

  • int
  • array[5] of ints
  • array[5] of arrays

There is no array[25] of ints involved.

It is correct that the sizeof semantics imply that the array as a whole is contiguous. The array[5] of ints must have 5*sizeof(int), and recursively applied, a[5][5] must have 5*5*sizeof(int). There is no room for additional padding.

Additionally, the array as a whole must be working when given to memset, memmove or memcpy with the sizeof. It must also be possible to iterate over the whole array with a (char *). So a valid iteration is:

int  a[5][5], i, *pi;
char *pc;

pc = (char *)(&a[0][0]);
for (i = 0; i < 25; i++)
{
    pi = (int *)pc;
    DoSomething(pi);
    pc += sizeof(int);
}

Doing the same with an (int *) would be undefined behaviour, because, as said, there is no array[25] of int involved. Using a union as in Christoph's answer should be valid, too. But there is another point complicating this further, the equality operator:

6.5.9.6 Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space. 91)

91) Two objects may be adjacent in memory because they are adjacent elements of a larger array or adjacent members of a structure with no padding between them, or because the implementation chose to place them so, even though they are unrelated. If prior invalid pointer operations (such as accesses outside array bounds) produced undefined behavior, subsequent comparisons also produce undefined behavior.

This means for this:

int a[5][5], *i1, *i2;

i1 = &a[0][0] + 5;
i2 = &a[1][0];

i1 compares as equal to i2. But when iterating over the array with an (int *), it is still undefined behaviour, because it is originally derived from the first subarray. It doesn't magically convert to a pointer into the second subarray.

Even when doing this

char *c = (char *)(&a[0][0]) + 5*sizeof(int);
int  *i3 = (int *)c;

won't help. It compares equal to i1 and i2, but it isn't derived from any of the subarrays; it is a pointer to a single int or an array[1] of int at best.

I don't consider this a bug in the standard. It is the other way around: Allowing this would introduce a special case that violates either the type system for arrays or the rules for pointer arithmetic or both. It may be considered a missing definition, but not a bug.

So even if the memory layout for a[5][5] is identical to the layout of a[25], and the very same loop using a (char *) can be used to iterate over both, an implementation is allowed to blow up if one is used as the other. I don't know why it should or know any implementation that would, and maybe there is a single fact in the Standard not mentioned till now that makes it well defined behaviour. Until then, I would consider it to be undefined and stay on the safe side.

Secure
@Secure: I believe the rationale behind this definition is related to http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html. After reading this I believe that the standard chose a larger than necessary undefined behavior and that stating that `concurrent accesses both through original pointer and casted one has undefined behavior` would be enough, but OK they are on the safe side.
kriss
@Secure: so would you agree that, had the original integer type used in the array been `char` (or `unsigned char`?) instead of `int`, things like `a[0][6]` would be valid and well-defined?
R..
@R..: No, this is explicitely listed as undefined behaviour. J.2: "An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7] given the declaration int a[4][5])(6.5.6)."
Secure
@Secure: But any object can be accessed as an overlaid array of `unsigned char` of size equal to the size of the object. J.2 uses the example of `int`, and to me it seems clear from this other requirement that J.2 would not be an example of UB if the type were `unsigned char`, but I thought I'd ask you since you seem to have put a lot of thought in these issues too.
R..
@R..: But it is not an overlaid array of chars, you still access it as an array[5][5]. This is a different issue. The array subscript out of range UB doesn't make an exception for any type, like this from J.2: "A trap representation is read by an lvalue expression that does not have character type (6.2.6.1)." Thus it is always undefined behaviour.
Secure
R..
@R..: As far as I see it you're right here, but the subscript out of range is a completely unrelated issue. Your original question was for `a[0][6]`, with the declaration `char a[5][5]`. This is UB, no matter what. It is valid to use `char *p =
Secure