> Freeing a non-null value returned from malloc is always safe,
Robert Gamble <(E-Mail Removed)> wrote:
> On Mon, 20 Jun 2005 07:54:55 -0700, Charles Mills wrote:
> > Richard Bos wrote:
> >> "Mehta Shailendrakumar" <(E-Mail Removed)> wrote:
> >> > What is the significance of "malloc(0)"?
> >> > Where one can use such a "malloced" pointer?
> >> Nowhere. Well, one can pass it to free() without worrying that this will
> >> cause problems.
> > On some implementations won't it cause problems if the pointer returned
> > by malloc(0) is not free'd? (Probably those implementation where
> > malloc(0) does not return NULL.)
Theoretically any unfree()d allocated block could cause problems, but
there's no reason why malloc(0) should cause more problems than
malloc(sizeof int). On modern implementations, it shouldn't cause any
problems at all, except the obvious ones.
> If a non-null value is returned from malloc, it should eventually be
> free'd. It is possible (likely) that an implementation that returns a
> non-null value from a malloc(0) will utilize a small amount of memory
> which would not be available again until free was called with the return
True, but no more so than for malloc(anything_positive).
> Freeing a non-null value returned from malloc is always safe, if
> malloc(0) return NULL, this does not, of course, need to be free'd.
But is still safe to be free()d, as much as a non-null value.
Jonathan Bartlett wrote:
> Mehta Shailendrakumar wrote:
>> Hi all,
>> What is the significance of "malloc(0)"?
>> It doesn't return a NULL pointer, then what does it return?
>> Where one can use such a "malloced" pointer?
> You may be able to use it as a tag of some sort, if you are generally
> doing comparisons of pointers. Let's say you had a list of pointers to
> data, but you wanted a few "special" values such as EMPTY and UNKNOWN.
> Let's say your list node looks like this:
> struct list_node
> void *data;
> struct list_node *next;
> You could do the following:
> void *EMPTY, *UNKNOWN;
> int main()
> EMPTY = malloc(0);
> UNKNOWN = malloc(0);
Of course, if malloc(0) returns NULL this does not help you, and it
could return NULL.
> Anyway, any time you had a pointer to a certain type of data (maybe char
> *), but needed some special values that aren't necessarily of that type
> (like UNKNOWN or EMPTY above), you could malloc(0) to make sure that you
> have a valid, unique pointer, but doesn't take up much memory.
malloc(0) could return a null pointer because the implementer decided
the easiest thing to do with malloc(0) was return a null pointer.
malloc(1) is only *likely* to fail if you are out of memory, although
the implementation *could* make it fail for other reasons.
So if you do a malloc(0) to get a unique pointer you have to test it if
you got a null pointer try malloc(1). Much simpler to just do a
malloc(1) and report a failure to the user and abort if you need unique
pointers, especially if they are being created on program start up as in
Living in interesting times.
Although my email address says spam, it is real and I read it.
On Mon, 20 Jun 2005 22:01:26 +0000, Keith Thompson wrote:
> "Malcolm" <(E-Mail Removed)> writes:
>> "Mehta Shailendrakumar" <(E-Mail Removed)> wrote
>>> What is the significance of "malloc(0)"?
>>> It doesn't return a NULL pointer, then what does it return?
>>> Where one can use such a "malloced" pointer?
>> There's a philosophical difference between nothing and emptiness. An example
>> in C is the null string (char *str = 0) and the empty string (char * str =
> That's a misnomer; a null char pointer (char *str = 0) is not a string
> at all. But yes, that's a good illustration of the difference between
> not-a-string and an empty string.
More generally a string is a sequence of characters terminated by a null
character. A pointer is not a string but you can have a "pointer to a
string" which is terminology defined in the standard.