[Python-Dev] Re: [Python-checkins] python/dist/src/Objects
listobject.c, 2.176, 2.177
Hye-Shik Chang
perky at i18n.org
Sun Jan 18 16:20:29 EST 2004
On Sun, Jan 18, 2004 at 03:09:13PM -0600, Jeff Epler wrote:
> 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.
>
Thanks for the explanation. But listobject isn't using realloc and
ob_item == NULL case is concerned in the code already.
In PyList_New:
73 if (size <= 0) {
74 op->ob_item = NULL;
75 }
Excuse me but am I missed something?
Hye-Shik
More information about the Python-Dev
mailing list