Is it undefined behavior to take the address of an uninitialized pointer?

前端 未结 5 1191
猫巷女王i
猫巷女王i 2020-12-21 00:46

N1570 states that this is undefined behavior:

§J.2/1 The value of an object with automatic storage duration is used while it is indeterminate (6.2.4

5条回答
  •  渐次进展
    2020-12-21 01:40

    The pointer's value and its address are not the same.

    void *foo;
    

    That pointer has an undefined value, but the address of foo, i.e. the value of &foo, must be well-determined (since otherwise we can't access it).

    At least that's my intuitive understanding, I didn't dig up the standard now, I just think you're mis-reading it.

    When talking about code, the two are sometimes confused ("what's the address of that pointer?" can mean "what's the value of that pointer, what address is it pointing to?") but they are really distinct.

提交回复
热议问题