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

I didn't know the part where the standard lets you malloc a buffer, write it as int, read it as int, then write it as float, and read it as float, all without freeing, and how the compiler thus has to treat heap memory differently from other memory. The part where compilers are too aggressive to follow this rule actually makes sense (as it seems to greatly increase the chance of valid optimizations based on type-based alias analysis to actually happen; it's hard to prove that a given pointer wasn't malloced.)

I wonder if the story is different with C++'s operator new, which returns a typed result.



Yes, the C rules are weird as that. I believe that GCC semi official stance is that stores always change the effective type of memory, not just for anonymous memory (this is more relaxed than the C rules, but handles correctly cases which are ambiguous under the standard rules).

C++ has the same rules as C for trivially constructible objects, but for objects with non trivial constructors, things are more complicated (and keep being revised at every standard update).




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

Search: