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

前端 未结 5 1176
猫巷女王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:16

    I only have access to N1256 but I will be surprised if there is any material change.

    The most relevant section is "6.5.3.2 Address and indirection operators"

    And in particular paragraph 3 (my emphasis):

    Semantics

    3 The unary & operator yields the address of its operand. If the operand has type ''type'', the result has type ''pointer to type''. If the operand is the result of a unary * operator, neither that operator nor the & operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue. Similarly, if the operand is the result of a [] operator, neither the & operator nor the unary * that is implied by the [] is evaluated and the result is as if the & operator were removed and the [] operator were changed to a + operator. Otherwise, the result is a pointer to the object or function designated by its operand.

    None of the cited paragraphs in the OP apply because as many have pointed out. The value of something and its address are very different.

    I would argue that the absence of any restriction on & regarding uninitialized values taking their address is permitted (by the absence of prohibition).

    NOTE: We all know it's fine but it's very rare to find statements in these standards that explicitly say "Yes you can do so & so."

提交回复
热议问题