On Sat, 04 Sep 2004 22:50:25 -0400, Kai-Uwe Bux <>
wrote in comp.lang.c++:
> john smith wrote:
>
> > Can someone please explain to me what is happening when I do a malloc(0).
> >
> > This is what I did.
> >
> > int* p = (int*)malloc(0);
> >
> > Then I printed the value of p and of course it was non-null.
> >
> > But has it allocated memory or what?
> >
>
> Sure, it has allocated a block of length 0. I have no idea what you want to
> do with a block that small, but I am sure that malloc has just allocated a
> block of that size, after all that is what malloc is supposed to do
> according to its man page.
That is one of the two defined actions that malloc() may take. The
other is to return a null pointer.
> > I would think that it would return a null.
>
> Why? It returns the location in memory where the allocated block starts.
> That this block has *length* 0 does not imply that it should start at the
> *location* 0.
What do you think that a null pointer has to do with location 0? A
null pointer does not point to location 0. It does not even have an
all-bits zero representation on all platforms.
If there is a location 0 on a platform and it is valid for a C (or
C++) object to reside there, then a pointer to such an object would be
a pointer to location 0 and it would most specifically not be a null
pointer.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html