← Back to context

Comment by kasdh

1 year ago

What part in aliasing prevents malloc() to be written in C? All pointers can be cast to char * and back. Casting from void * is also legal.

Pointers to unallocated memory are not allowed to be read from, in any form. You have to manage memory with uintptr_t arithmetic which arguably makes pointer arithmetic in C almost completely useless for writing malloc().

  • That's not a consequence of effective type (i.e., strict aliasing) rules, that's a consequence of pointer provenance rules. And a little more generally, it's a consequence of malloc having special object system behavior that there's no way to express in pure C code.