[Python-Dev] Re: [Python-checkins] python/dist/src/Objects listobject.c, 2.176, 2.177

Jeff Epler jepler at unpythonic.net
Sun Jan 18 16:09:13 EST 2004


On Mon, Jan 19, 2004 at 05:48:38AM +0900, Hye-Shik Chang wrote:
> Is there a particular reason for allocating zero-sized memory for
> this?  On my test, assigning NULL on self->ob_item instead is worked
> either.

Yes.  I think that the explanation goes something like this:

Only values returned by malloc/calloc/realloc are suitable as an argument
to realloc/free.  So, if you want to start with 0 bytes but not special
case the deallocation or reallocation case, you write
	f = malloc(0);
instead of 
	f = NULL;
later, you can use
	f = realloc(f, newsize); /* Ignoring error checking */
and
	f = free(f)
instead of
	if (f) f = realloc(f, newsize);
	else f = malloc(newsize);
and
	if(f) free(f);

now, some systems have malloc(0) return NULL, and accept free(NULL) as a
no-op, and realloc(NULL, newsize) as malloc(newsize), but behavior other
than that is allowed by the C standard.

Jeff



More information about the Python-Dev mailing list