malloc-free-malloc and strict-aliasing

前端 未结 4 1154
渐次进展
渐次进展 2021-01-13 03:06

I\'ve been trying to understand a particular aspect of strict aliasing recently, and I think I have made the smallest possible interesting piece of code. (Interesting for me

4条回答
  •  旧时难觅i
    2021-01-13 03:48

    The actual rules regarding aliasing are laid out in standard section 6.5, paragraph 7. Note the wording:

    An object shall have its stored value accessed only by an lvalue expression that has one of the following types:

    (emphasis mine)

    Aliasing includes the notion of objects, not just general memory. For malloc to have returned the same address on a second use requires the original object to have been deallocated. Even if it has the same address, it is not considered the same object. Any attempts to access the first object through dangling pointers leftover after free are UB for completely different reasons, so there's no aliasing because any continued use of the first pointer p32 is invalid anyway.

提交回复
热议问题