You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Alexei Kosut <ak...@organic.com> on 1997/07/19 03:20:12 UTC

Use of pcalloc()

Hmm.

I recently noticed something: The Apache code uses pcalloc() a lot to
allocate memory and initialize it, since pcalloc() (like its
similar-named calloc()) fills with zeros what it initializes. However,
isn't it illegal to assume that this will correctly make a structure's
members which are pointers null, since the null pointer is not
neccessarily represented by an all-bits zero pointer internally?

Now certainly we've never had any bug reports on this, so I imagine it
works on nearly every OS out there. But isn't it still a bad practice?

-- Alexei Kosut <ak...@organic.com>


Re: Use of pcalloc()

Posted by Ben Laurie <be...@algroup.co.uk>.
Alexei Kosut wrote:

> Hmm.
>
> I recently noticed something: The Apache code uses pcalloc() a lot to
> allocate memory and initialize it, since pcalloc() (like its
> similar-named calloc()) fills with zeros what it initializes. However,
>
> isn't it illegal to assume that this will correctly make a structure's
>
> members which are pointers null, since the null pointer is not
> neccessarily represented by an all-bits zero pointer internally?
>
> Now certainly we've never had any bug reports on this, so I imagine it
>
> works on nearly every OS out there. But isn't it still a bad practice?

Although you are, strictly, correct, the equivalence between all zeroes
and NULL is so widely assumed, and the difficulty of implementing a
non-zero NULL so large, that I really don't believe it will ever prove
to be a problem (though there is the interesting case of 16-bit
segmented architectures - but they are so appallingly broken anyway they
are hardly worth considering).

There is a comment to the effect that we make this assumption in a
header somewhere.

Cheers,

Ben.

--
Ben Laurie                Phone: +44 (181) 994 6435  Email:
ben@algroup.co.uk
Freelance Consultant and  Fax:   +44 (181) 994 6472
Technical Director        URL: http://www.algroup.co.uk/Apache-SSL
A.L. Digital Ltd,         Apache Group member (http://www.apache.org)
London, England.          Apache-SSL author



Re: Use of pcalloc()

Posted by Dean Gaudet <dg...@arctic.org>.
On Fri, 18 Jul 1997, Alexei Kosut wrote:

> But isn't it still a bad practice?

Pretty much every program out there makes this assumption.

FWIW, ANSI defines the constant 0 to be NULL when used in a pointer
context.  It also specifies that uninitialized globals will have all
pointers initialized to NULL and all scalars to 0.  I don't remember
if it goes as far as to require that NULL actually is 0.

Dean