malloc-free-malloc and strict-aliasing

前端 未结 4 1168
渐次进展
渐次进展 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条回答
  •  醉话见心
    2021-01-13 03:54

    Note: This only answers initial question, not the part about custom allocators.


    No, it's not UB, because p16 now holds different object and the former is gone after you invoked free(p32).

    Note that malloc() returns pointer that is pre-aligned for every object, thus this avoids breaking of strict aliasing in practical terms. From C11 (N1570) 7.22.3/p1 Memory management functions (emphasis mine):

    The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated). The lifetime of an allocated object extends from the allocation until the deallocation.

提交回复
热议问题