Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pqbGZ-00062j-L8 for pgsql-general@arkaria.postgresql.org; Sun, 23 Apr 2023 15:04:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pqbGY-0002Ph-78 for pgsql-general@arkaria.postgresql.org; Sun, 23 Apr 2023 15:04:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pqbGX-0002PX-SN for pgsql-general@lists.postgresql.org; Sun, 23 Apr 2023 15:04:37 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pqbGV-001SxS-J5 for pgsql-general@lists.postgresql.org; Sun, 23 Apr 2023 15:04:36 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 33NF4LS73839789; Sun, 23 Apr 2023 11:04:21 -0400 From: Tom Lane To: Thorsten Glaser cc: pgsql-general@lists.postgresql.org, Wen Yi Subject: Re: Why not use the calloc to replace malloc? In-reply-to: <41c9d7f-7953-d22c-12f1-e83815cb5d9e@evolvis.org> References: <3765291.1682222377@sss.pgh.pa.us> <41c9d7f-7953-d22c-12f1-e83815cb5d9e@evolvis.org> Comments: In-reply-to Thorsten Glaser message dated "Sun, 23 Apr 2023 16:21:48 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <3839787.1682262261.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Sun, 23 Apr 2023 11:04:21 -0400 Message-ID: <3839788.1682262261@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thorsten Glaser 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