tinycc-devel
[Top][All Lists]
Advanced

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

[Tinycc-devel] Re: malloc(0)


From: Charlie Gordon
Subject: [Tinycc-devel] Re: malloc(0)
Date: Mon, 23 Sep 2002 08:11:04 +0000

Hello folks!

Here comes one of my favorites: libc behaviour at the limits!

- ANSI C 1990 does not specify precisely what the semantics of malloc(0)
it only says that free(NULL) is legitimate, and realloc(ptr,0) should free ptr. - a quick note here on free(NULL): whenever you implement a wrapper on malloc/free make it adhere to this convention, it is very useful and becomes quite natural; I only test for NULL ptr before calling free() in cases where the function call would be too costly, a rare occasion. As a matter of fact, very few C++ addicts
 know that its perfectly fine to 'delete' a NULL pointer.
- ISO/IEC 9899:1999 is more specific on the malloc(0) issue: section 7.20.3 pg 324 read: "if the size of the space request is zero, the behaviour is implementation- defined: either a null pointer is returned, or the behaviour is as the size were some nonzero value, except that the returned pointer shall not be used
 to access an object.
- tcc returns non NULL for malloc(0) because it allocates 1 more byte than
requested to ensure that malloc'd areas are never contiguous. This is needed for bound checking. ISO reads in section 7.20.3 that each successful allocation shall yield a pointer to an object disjoint from any other object, yet that
 order and contiguity of storage is unspecified.

Conclusion: do not rely on implementation defined behaviour. Both glibc and ulibc are OK on this issue, TCC internal libc can implement one or the other, preferably
glibc's, but not necessarily.

There are a lot of small details like these to polish in the C language,
keep contributing to tcc by testing it to the limits!

Chqrlie.

___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx





reply via email to

[Prev in Thread] Current Thread [Next in Thread]