开发者

lvalue doesn't designate an object after evaluation?

C99 [Section 6.3.2.1/1] sa开发者_运维技巧ys

An lvalue is an expression with an object type or an incomplete type other than void; if an lvalue does not designate an object when it is evaluated, the behavior is undefined.

What does the part in bold mean? Can someone please explain it with an example?


Null pointers, pointers to deallocated objects and pointers to objects with automatic storage duration whose lifetime has already ended come to mind. Dereferencing these results in invalid lvalues; the undefined behaviour you will encounter most often are segfaults if you're lucky, and arbitrary heap or stack corruption if not.


#include <stdio.h>

int* ptr;

void f(void) {
    int n = 1;
    ptr = &n;
}

int main(void) {
    f();
    // UB: *ptr is an lvalue that is not an object:
    printf("%d\n", *ptr);
    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜