Re: Fix and improve allocation formulas - Mailing list pgsql-hackers

From Michael Paquier
Subject Re: Fix and improve allocation formulas
Date
Msg-id aTvmHFsAXkW0ab5F@paquier.xyz
Whole thread Raw
In response to Re: Fix and improve allocation formulas  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
On Thu, Dec 11, 2025 at 11:43:27AM -0500, Tom Lane wrote:
> And before that, you'd have to get consensus that sizeof(*var) *is*
> the preferred style.  I for one don't like it a bit.  IMO what it
> mostly accomplishes is to remove a cue as to what we are allocating.
> I don't agree that it removes a chance for error, either.  Sure,
> if you write
>
>     foo = palloc(sizeof(typeA))
>
> when foo is of type typeB*, you made a mistake --- but we know how
> to get the compiler to warn about such mistakes, and indeed the
> main point of the palloc_object() changes was to catch those.
> However, suppose you write
>
>     foo = palloc(sizeof(*bar))
>
> I claim that's about an equally credible typo, and there is
> nothing that will detect it.

Yeah, I'd prefer something where we keep track of the type, with the
extra layer that enforces a cast to the type of the variable like
palloc_object/array macros.  The latter style of specifying a variable
pointer within the sizeof is more error-prone long-term, so it's not
something I think we should encourage.
--
Michael

Attachment

pgsql-hackers by date:

Previous
From: Chengpeng Yan
Date:
Subject: Re: Add a greedy join search algorithm to handle large join problems
Next
From: shveta malik
Date:
Subject: Re: Proposal: Conflict log history table for Logical Replication