Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> The act of writing a value of a different type tells the compiler that the lifetime of the previous object has ended. There's no special magic required.

I think strictly speaking in C this is only true for anonymous memory, i.e. memory you got from some allocator-like function. So if your custom allocator gets its memory from malloc (or sbrk, or mmap), everything is fine, but, for example, you are allocating from a static char array, it is formally UB.

In C++ you can use placement new to change the type of anything.



Yes, type-changing stores are guaranteed by the C standard to work only for allocated storage. In practice no compiler makes a difference between declared and allocated storage. A bigger problem is that Clang does not implement the C rules as specified..




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: