Re: Should we use MemSet or {0} for struct initialization? - Mailing list pgsql-hackers

From Jelte Fennema
Subject Re: Should we use MemSet or {0} for struct initialization?
Date
Msg-id CAGECzQR9LBC0emeHUj7H5DZHVN8wsOSuOSMJ50gHtxtwZk9YrQ@mail.gmail.com
Whole thread Raw
In response to Re: Should we use MemSet or {0} for struct initialization?  (Junwang Zhao <zhjwpku@gmail.com>)
Responses Re: Should we use MemSet or {0} for struct initialization?
List 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 by date:

Previous
From: Tom Lane
Date:
Subject: Re: Move bki file pre-processing from initdb to bootstrap
Next
From: Peter Eisentraut
Date:
Subject: Re: Unlogged relations and WAL-logging