News for hackers. Stuff that matters.
Re:Once upon a time, memory allocators made sense by kazinator on Monday January 18, 2016 @01:21PM (#10925887)
The fix is to wrap malloc and friends with your own routines.

You can have it so that "my_malloc" always returns NULL on a zero length object. Or, if you prefer, so that it always returns a unique pointer:

   void *my_malloc(size_t size)
   {
      return size ? malloc(size) : 0;  /* option A */
   }


   void *my_malloc(size_t size)
   {
      return malloc(size ? size : 1);  /* option B */
   }
And you can make my_realloc have the nice freeing behavior:

   void *my_realloc(void *ptr, size_t size)
   {
     return (ptr && size)
            ? realloc(ptr, size)
            : !ptr
              ? my_malloc(size) 
              : (free(ptr), 0);
   }
Note how we still carefully implement a parallel requirement to the one in ISO C. Namely that my_realloc(NULL, size) calls my_malloc(size), regardless of size, just like realloc(NULL, size) is required to be equivalent to malloc(size). We want all the routines in this allocator to be drop-in replacements such that any code which is written to the ISO C allocator spec can be blindly retargetted to use them.

Pretty much any serious C program, if indeed it doesn't have an entire allocator of its own, at least wraps the standard one, for the sake of more uniform behaviors, as well as easy retargettability to embedded scenarios. (Not only embedded machines, but say, embedding in a larger application, where you are told "you must use this table of funtion pointers as your allocator" and it doesn't quite look like malloc: zero allocations are not allowed, realloc is missing, ...)

In this particular program, realloc is wrapped under a function called resize; resize had to be patched not to rely on realloc(nonnull, 0), but users of resize don't have to change. The programmer doesn't have to look for fifty uses of resize to fix them. However, it's worth it to wrap the functions at a lower level anyway, with an identical API.

The C library is far from perfect. Do not expect consistency, completeness, beauty, symmetry and so on.

There are worse things in it than realloc(ptr, 0) not behaving in the neat way that you would like. Oh such as:

   isalpha(str[42]);   // str is char array
being undefined behavior if char happens to be signed, and str[42] is negative, because isalpha takes an int argument which is expected to hold a positive byte value [0, UCHAR_MAX), and not char value. Whoooops!
Re:Once upon a time, memory allocators made sense by klodolph on Monday January 18, 2016 @01:27PM (#10925939)
You can make my_realloc() simpler:

    void *my_realloc(void *ptr, size_t size)
    {
        return size ? realloc(ptr, size) : (free(ptr), 0);
    }
According to the C standard, realloc(NULL, size) is guaranteed to be equivalent to malloc(size). From n1548 7.22.3.5 para 3:

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

Also, free(0) is well-defined. From n1548 7.22.3.3 para 2:

> If ptr is a null pointer, no action occurs.