Re: Consistently use palloc_object() and palloc_array() - Mailing list pgsql-hackers

From David Geier
Subject Re: Consistently use palloc_object() and palloc_array()
Date
Msg-id a29b0cd9-576f-486f-ba45-5139ee33adcf@gmail.com
Whole thread Raw
In response to Re: Consistently use palloc_object() and palloc_array()  (Chao Li <li.evan.chao@gmail.com>)
Responses Re: Consistently use palloc_object() and palloc_array()
List pgsql-hackers
Hi!

Thanks for taking a look.

On 27.11.2025 00:03, Chao Li wrote:
> 
> This is a large patch, I just take a quick look, and found that:
> 
> ```
> -        *phoned_word = palloc(sizeof(char) * strlen(word) + 1);
> +        *phoned_word = palloc_array(char, strlen(word) + 1);
> ```
> 
> And
> 
> ```
> -        params = (const char **) palloc(sizeof(char *));
> +        params = palloc_object(const char *);
> ```
> 
> Applying palloc_array and palloc_object to char type doesn’t seem to improve anything.
> 

You mean because sizeof(char) is always 1 and hence we could instead
simply write:

*phoned_word = palloc(strlen(word) + 1);
params = palloc(1);

I think the _array and _object variants are more expressive and for sure
don't make the code less readable.

--
David Geier



pgsql-hackers by date:

Previous
From: Hannu Krosing
Date:
Subject: Re: Revisiting {CREATE INDEX, REINDEX} CONCURRENTLY improvements
Next
From: Tom Lane
Date:
Subject: Re: Consistently use palloc_object() and palloc_array()