开发者

Is this valid C? No warning about p being uninitialized

Compiling this code snippet with gcc (4.5) and as many -Wall, -Wextra, -Wuninitialized type flags turned on as possible gives me no warnings:

int main() {
    int *p = p;
    printf("p = %p\n", (void *)p);
    return 0;
}

But running it multip开发者_开发问答le times gives this output:

p = 0xbe9ff4
p = 0x550ff4
p = 0xeb1ff4
p = 0x4caff4

... and so on.

What's going on here?

EDIT: Compiling with "g++ -Wall" instead gives me warning as I'd expect:

In function ‘int main()’: warning: ‘p’ is used uninitialized in this function


int *p = p;

p is defined as soon as int *p is parsed, but the RHS is only evaluated afterwards. This statement is equivalent to

int * p;
p = p;

This is different in C++ with implicit constructors, but in plain ol' C, this is what you have. Undefined initial value.

As far as the compiler warning goes, it's a Quality Of Implementation issue. gcc isn't being "tricked", it's just being permissive.


Valgrind gives warnings about p being uninitialized. I guess that gcc is tricked and a bug report should be filled in.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜