[Python-Dev] Problem with the memory docs

Fredrik Lundh fredrik@pythonware.com
Thu, 4 Apr 2002 19:19:46 +0200


tim wrote:

>
>     Requesting zero bytes returns a non-NULL pointer.
> 
> I can't guarantee this without insane effort, and our code has never
> guaranteed this.

umm.  I have to admit that it's rather ugly, but I'm pretty sure the
following guarantees that requesting zero bytes will never return a
NULL pointer:

static char nullbyte = MAGIC;

void* malloc(int bytes)
{
    if (bytes == 0) {
#if DEBUG_MODE
        if (*nullbyte != MAGIC)
            uh-oh
#endif
        return &nullbyte;
    }
    ...

void free(void* ptr)
{
    if (ptr == &nullbyte) {
#if DEBUG_MODE
        if (*nullbyte != MAGIC)
            uh-oh
#endif
        return; /* nothing to do */
    }
    ...

</F>