Re: Should we use MemSet or {0} for struct initialization?

Поиск
Список
Период
Сортировка
От Jelte Fennema
Тема Re: Should we use MemSet or {0} for struct initialization?
Дата
Msg-id CAGECzQR9LBC0emeHUj7H5DZHVN8wsOSuOSMJ50gHtxtwZk9YrQ@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Should we use MemSet or {0} for struct initialization?  (Junwang Zhao <zhjwpku@gmail.com>)
Ответы Re: Should we use MemSet or {0} for struct initialization?  (John Naylor <john.naylor@enterprisedb.com>)
Список pgsql-hackers
On Thu, 31 Aug 2023 at 13:35, Junwang Zhao <zhjwpku@gmail.com> wrote:
> > > If the struct has padding or aligned, {0} only guarantee the struct
> > > members initialized to 0, while memset sets the alignment/padding
> > > to 0 as well, but since we will not access the alignment/padding, so
> > > they give the same effect.
> >
> > See above -- if it's used as a hash key, for example, you must clear everything.
>
> Yeah, if memcmp was used as the key comparison function, there is a problem.

The C standard says:
> When a value is stored in an object of structure or union type, including in a member object, the bytes of the object
representationthat correspond to any padding bytes take unspecified values.
 

So if you set any of the fields after a MemSet, the values of the
padding bytes that were set to 0 are now unspecified. It seems much
safer to actually spell out the padding fields of a hash key.



В списке pgsql-hackers по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Move bki file pre-processing from initdb to bootstrap
Следующее
От: Peter Eisentraut
Дата:
Сообщение: Re: Unlogged relations and WAL-logging