lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


In message <3D9EED95.E914E7EB@gmx.de>, Edgar Toernig writes:
> > I do not know what the ANSI C standard says about using
> > realloc(x,0).
> 
> ANSI C99 about memory management functions in general:
> 
>        If the size of the space requested  is
>        zero,  the behavior is implementation-defined: either a null
>        pointer is returned, or the behavior is as if the size  were
>        some  nonzero  value, except that the returned pointer shall
>        not be used to access an object.
> 
> and about realloc itself [the part where free is mentioned]:
> 
>        If the realloc function returns a  null  pointer
>        when  size is zero and ptr is not a null pointer, the object
>        it pointed to has been freed.
> 
> So it seems one cannot depend on realloc(x,0) to free x and my
> libc is not buggy (puh *g*).  Perfectly legal behaviour: truncate
> to some small size and return a valid and unique pointer.

*ugh*.  The wording in the ISO C Standard has changed between the 1989
version and the 1999 version.  The 1989 version simply stated:
"If size is zero and ptr is not a null pointer, the object it pointed to
has been freed." (or something essentially equivalent, I haven't got the
standard here in front of me).  The 1999 version added the qualification
"If the realloc function returns a null pointer when ... ".

In other words, lua was fine under the old, 1989, version of the C
standard, but now opens itself to a hosing by new implementations of the
1999 version.

lua should change.

Cheers,
 drj