News for hackers. Stuff that matters.
Re:Once upon a time, memory allocators made sense by asveikau on Monday January 18, 2016 @09:58AM (#10924699)
This is way too hyperbolic. I don't know what specific "once upon a time" period is being described. However malloc(0), realloc(NULL, x), and realloc(y, 0) has been a portability headache for as long as I have been writing C. None of this is all that surprising.
Re:Once upon a time, memory allocators made sense by klodolph on Monday January 18, 2016 @01:24PM (#10925917)
The behavior of realloc(NULL, x) is always equivalent to malloc(x) according to the C standard. But yes, I never remember a time when malloc(0) or realloc(y, 0) played nice.
Re:Once upon a time, memory allocators made sense by asveikau on Monday January 18, 2016 @02:57PM (#10926490)
> The behavior of realloc(NULL, x) is always equivalent to malloc(x) according to the C standard.

AFAIK if you try to do this with even a fairly recent Microsoft libc it won't work. I haven't checked the standards and history on this one, but keep in mind it's been only a recent development that MS gives a crap about C99.

The idea that C89 did not mandate this makes sense to me because I remember now-obsolete Unixes not liking this either.

[Edit: The MS documentation claims that it supports realloc(NULL, x) going back quite a while. I know I've been bitten by it not doing that in the current century, however...]

Re:Once upon a time, memory allocators made sense by masklinn on Tuesday January 19, 2016 @05:59AM (#10929896)
> The idea that C89 did not mandate this makes sense to me because I remember now-obsolete Unixes not liking this either.

C89 did mandate this:

> If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size.

C89 also mandated that realloc(ptr, 0) be equivalent to free(ptr), which was removed in C99:

> If size is zero and ptr is not a null pointer, the object it points to is freed.