In a structure, is it legal to use one array field to access another one?

后端 未结 9 480
忘了有多久
忘了有多久 2020-12-24 01:43

As an example, consider the following structure:

struct S {
  int a[4];
  int b[4];
} s;

Would it be legal to write s.a[6] and

9条回答
  •  清歌不尽
    2020-12-24 02:18

    Is it legal? No. As others mentioned, it invokes Undefined Behavior.

    Will it work? That depends on your compiler. That's the thing about undefined behavior: it's undefined.

    On many C and C++ compilers, the struct will be laid out such that b will immediately follow a in memory and there will be no bounds checking. So accessing a[6] will effectively be the same as b[2] and will not cause any sort of exception.

    Given

    struct S {
      int a[4];
      int b[4];
    } s
    

    and assuming no extra padding, the structure is really just a way of looking at a block of memory containing 8 integers. You could cast it to (int*) and ((int*)s)[6] would point to the same memory as s.b[2].

    Should you rely on this sort of behavior? Absolutely not. Undefined means that the compiler doesn't have to support this. The compiler is free to pad the structure which could render the assumption that &(s.b[2]) == &(s.a[6]) incorrect. The compiler could also add bounds checking on the array access (although enabling compiler optimizations would probably disable such a check).

    I've have experienced the effects of this in the past. It's quite common to have a struct like this

    struct Bob {
        char name[16];
        char whatever[64];
    } bob;
    strcpy(bob.name, "some name longer than 16 characters");
    

    Now bob.whatever will be " than 16 characters". (which is why you should always use strncpy, BTW)

提交回复
热议问题