pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Thorsten Glaser <tg@evolvis.org>
Cc: pgsql-general@lists.postgresql.org, Wen Yi <chuxuec@outlook.com>
Subject: Re: Why not use the calloc to replace malloc?
Date: Sun, 23 Apr 2023 11:04:21 -0400
Message-ID: <3839788.1682262261@sss.pgh.pa.us> (raw)
In-Reply-To: <41c9d7f-7953-d22c-12f1-e83815cb5d9e@evolvis.org>
References: <TYAP286MB0636A72D4EC3E52779FDB4F0A9669@TYAP286MB0636.JPNP286.PROD.OUTLOOK.COM>
	<3765291.1682222377@sss.pgh.pa.us>
	<41c9d7f-7953-d22c-12f1-e83815cb5d9e@evolvis.org>

Thorsten Glaser <tg@evolvis.org> writes:
> On Sat, 22 Apr 2023, Tom Lane wrote:
>> The maintenance benefits are real though.

> Oh, interesting ;-) Thanks for this explanation.

> Another data point is: calloc is not correct for pointer fields,
> you have to manually assign NULL to them afterwards still, because
> NULL doesn’t have to be represented by all-zero bytes (e.g. TenDRA
> supports having 0x55555555 as NULL pointer as an option).

Yeah, according to the letter of the C standard you shouldn't assume
that NULL is physically the same as zero.  We've blithely ignored
that though --- there are lots of places that, for example, assume
that palloc0() of an array of pointers will produce pointer values
that are NULL.  I don't see any real-world benefit in insisting on
looping over such an array to set the pointers individually.

But this is a good comparison point, because it illuminates one
aspect of the maintenance argument.  Suppose you want to add a
field to struct Foo, and that field often or always needs to start
out with a nonzero value.  With our existing practice, grepping
for places that assign to the adjacent fields will generally find
all the places you need to touch.  If we relied more on calloc
or palloc0 to initialize fields, you'd have a harder time.  People
would be pushed into contorting their data representation choices
so that fields could start out as physically zero, and that would
lead to things like odd, inconsistent choices of boolean flag
polarity.

			regards, tom lane





view thread (4+ messages)

Message-ID: <3839788.1682262261@sss.pgh.pa.us>
Permalink:  ../3839788.1682262261@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3839788.1682262261@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, tg@evolvis.org, chuxuec@outlook.com
  Subject: Re: Why not use the calloc to replace malloc?
  In-Reply-To: <3839788.1682262261@sss.pgh.pa.us>
  References: <TYAP286MB0636A72D4EC3E52779FDB4F0A9669@TYAP286MB0636.JPNP286.PROD.OUTLOOK.COM> <3765291.1682222377@sss.pgh.pa.us> <41c9d7f-7953-d22c-12f1-e83815cb5d9e@evolvis.org> <3839788.1682262261@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox