Lifetime of temporary objects in C11 vs C99

前端 未结 1 844
遥遥无期
遥遥无期 2021-02-12 20:01

I am trying to decipher a note that led to a change between C99 and C11. The change proposed in that note ended up in C11\'s 6.2.4:8, namely:

A non-lvalue

相关标签:
1条回答
  • 2021-02-12 20:22

    My understanding is that in C99, the finest grain of lifetime for an object is the block. Thus, while 6.5.2.2 (and some other § mentioned in the note you refer to) specifically says that you can't access the returned value after the next sequence point, technically its address is not indeterminate until after you have left the enclosing block (the reason why you should have some storage reserved for an inaccessible object is left as an exercise for the reader, though). Thus, something like

    struct X { int a[5]; } f();
    int *p;
    { p = f().a; }
    printf("%p\n", p);
    

    is undefined in C99 as well as in C11. In C11, the notion of "temporary lifetime", that does not exist in C99, allows to consider that the pointer becomes indeterminate as soon as the full expression ends.

    0 讨论(0)
提交回复
热议问题