Re: Consistently use palloc_object() and palloc_array()

From: David Geier <geidav(dot)pg(at)gmail(dot)com>
To: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Consistently use palloc_object() and palloc_array()
Date: 2025-11-28 21:06:34
Message-ID: a29b0cd9-576f-486f-ba45-5139ee33adcf@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2025-11-28 21:28:43 Re: Consistently use palloc_object() and palloc_array()
Previous Message Hannu Krosing 2025-11-28 21:01:04 Re: Revisiting {CREATE INDEX, REINDEX} CONCURRENTLY improvements