6

I came across the initial sequence concept. Serching through the Standard for initial sequence phrase gives only 3 results and they don't give a definition.

Section N3797::9.5/1 [class.union]:

If a standard-layout union contains several standard-layout structs that share a common initial sequence (9.2), and if an object of this standard-layout union type contains one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of standard-layout struct members;

I wish to look at an example which is demostrated that quote.

timrau
  • 22,578
  • 4
  • 51
  • 64
  • What prevents you from just writing up an example? key phrase "standard-layout union type contains one of the standard-layout structs" – Cheers and hth. - Alf Oct 25 '14 at 07:42
  • @Cheersandhth.-Alf What's the initial sequnce? –  Oct 25 '14 at 07:43
  • ["initial"](https://www.google.no/search?q=initial) = comes first. the data at the start of the struct. – Cheers and hth. - Alf Oct 25 '14 at 07:43
  • @Cheersandhth.-Alf that's they appear in the initializer `struct A { int a = 5; }`, do they? –  Oct 25 '14 at 07:45
  • Oh. No. We're talking *memory layout*, placement in memory. With no intervening access specifiers this order is the same as declaration order. E.g. in `struct S { int a; int b; int c; };` the `a`, `b` and `c` in any given `S` instance are at increasing memory addresses. – Cheers and hth. - Alf Oct 25 '14 at 07:46
  • @Cheersandhth.-Alf I can't get the point what's the struct that _share_ common initial sequence? Are there structs that don't share it? –  Oct 25 '14 at 07:58
  • 1
    I think it is saying that for as long as the data members are of the same type, the padding between them will be the same and the members will properly alias one another. That means putting a number in using one struct's identifier can be accurately gotten out (from the same position) using a different struct's identifier *until* one of the structs contains a different type (in declaration order) to the other. (Is that explanation less complicated than the docs?)! – Galik Oct 25 '14 at 08:08
  • @Galik Sure. I'd like to clarify one more thing. I've written an example: http://coliru.stacked-crooked.com/a/fa1f5f440732d901 If I understand you correclty, there is no UB in that example, right? –  Oct 25 '14 at 08:13
  • 2
    the key-phrase here is `common initial sequence`, the common part is important, if all struct's start with `int kind;` then thats the common initial sequence. – sp2danny Oct 25 '14 at 08:13
  • 1
    @DmitryFucintv According to my understading your example causes **no** UB. But that guarantee ends as soon as the types differ in declaration order. – Galik Oct 25 '14 at 08:16
  • @sp2danny So if both structs start with different kind of declration (e.g. int and float) then the common initial sequence is empty, is it? –  Oct 25 '14 at 09:33

1 Answers1

7

I believe it's talking about this kind of thing:

union U {
    struct S {
        int a;
        int b;
        int c;
    }
    struct T {
        int x;
        int y;
        float f;
    }
};

It's saying that it's OK to access either U.S.a or U.T.x and that they will be equivalent. Ditto for U.S.b and U.T.y of course. But accessing U.T.f after setting U.S.c would be undefined behaviour.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Is it necessary that `S.a, S.b` have tha same names as `T.a, T.b`? –  Oct 25 '14 at 08:04
  • Is the implication therefore that accessing `U.S.b` and `U.T.b` or `U.S.c` and `U.T.f` on the same struct undefined behaviour because I have always taken that to be the case. – sjdowling Oct 25 '14 at 08:05
  • 2
    accessing T.f is UB, if S.c was set. but a & b are part of the 'common initial sequence' and ok to access – sp2danny Oct 25 '14 at 08:08
  • @DmitryFucintv: I suspect the names of the fields don't matter, only the types, but I am not a "language lawyer". Answer updated to reflect this now. – Paul R Oct 25 '14 at 08:15
  • 1
    @PaulR: The names don't influence the memory layout. I.e. the names don't matter. – Cheers and hth. - Alf Oct 25 '14 at 08:28
  • 2
    @sp2danny: nope. c++11 9.2/19 "Two standard-layout structs share a common initial sequence if corresponding members have layout-compatible types and either neither member is a bit-field or both are bit-fields with the same width for a sequence of one or more initial members". – Cheers and hth. - Alf Oct 25 '14 at 08:30
  • @Cheersandhth.-Alf Let me ask you one more question. In the quote I cited in the initial post it's talking about _data-member_ of type struct, not a nested type `struct`, is it? –  Oct 25 '14 at 09:55
  • 2
    @DmitryFucintv: Yes, data members, because we're talking about memory layout. The types can be locally defined or not, doesn't matter. – Cheers and hth. - Alf Oct 25 '14 at 10:03