Re:The Lost Art of Structure Packing (2018)
by saagarjha on Monday April 27, 2020 @12:54PM
(#22997572)
sizeof will include padding, but there is no need for memset to actually perform a write that cannot be consistently observed.
[ Parent
| on HN ]
Re:The Lost Art of Structure Packing (2018)
by icedchai on Monday April 27, 2020 @06:26PM
(#23000839)
Are you saying memset does not have to write the entire struct? I understand it can be optimized out, but thought it if it was actually executed it had to set all of the specified bytes.
Re:The Lost Art of Structure Packing (2018)
by saagarjha on Monday April 27, 2020 @09:32PM
(#23002357)
memset must write to the bytes that underly the members of the struct. What it does to padding bytes is not of consequence to you, as you cannot observe it as far as I understand. (That is, if you tried to read it out it would not necessarily be the value you had memset, or even be consistent across reads.)
Re:The Lost Art of Structure Packing (2018)
by icedchai on Monday April 27, 2020 @09:45PM
(#23002435)
memset definitely writes to padding bytes, as noted by several other folks here. you observe these padding bytes during serialization and deserialization (disk, network, memory mapped buffer, etc.)
Note this is important enough that compiler folks consider it a bug if it doesn't work correctly. see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92486 for a recent example involving memset and memcpy.
Re:The Lost Art of Structure Packing (2018)
by saagarjha on Tuesday April 28, 2020 @01:57AM
(#23003888)
As mentioned in the bug report, the value of padding bytes is unspecified by the C standard. Actually, according to the linked Defect Report mentioned in the bug, there is a bit of discussion on whether the functions you mentioned should be entirely undefined to call or if some exceptions should be carved out. If the functions are legal to call, it is very likely that the results will be some arbitrary reification of the unspecified value.