| ▲ | lifthrasiir 4 hours ago | |||||||
In the other words, free() is a flawed API. :-) | ||||||||
| ▲ | adrian_b an hour ago | parent [-] | |||||||
Actually yes, the malloc/free API (inherited from the ALLOCATE and FREE statements of PL/I) is too simple. The metadata that malloc always attaches to the allocated memory would normally be useful for the user (i.e. having access to values like the currently allocated size and the total size of the allocation). Very frequently, the user must duplicate inside the allocated memory the same information that already exists in the metadata, wasting memory. Also time may be wasted with requests for reallocation, instead of just adjusting the currently allocated size, when this is sufficient. With a better API, the metadata would have been visible for the user. Being hidden from the user is not a protection in a language like C, where using pointer arithmetic can trash any memory location. Being exposed as non-mutable would have been a better protection. Moreover, a better metadata structure for malloc should have always included a reference count, to be handled automatically by the compiler, and the malloc/free functions should have been invoked only implicitly, never explicitly. | ||||||||
| ||||||||