Suppose I have a snippet of code like this (and assume that the padding is inserted as I say in the comment):
#include "stdint.h"
struct A
{
uint8_t x;
// 3 bytes of padding
uint32_t y;
};
void foo(struct A* a)
{
a->x = 0;
}
Is the compiler allowed to clear the padding bytes or do anything to the padding bytes in the function foo?
https://en.cppreference.com/w/c/language/object is silent on this issue, which leads me to believe that yes the compiler is allowed to do this....
The reason that I want to know whether or not the compiler is allowed to do this is I want to memcmp
two structs, and I want to know what I am allowed to assume....
Yes. C 2018 6.2.6.1 6 says:
When a value is stored in an object of structure or union type, including in a member object, the bytes of the object representation that correspond to any padding bytes take unspecified values…
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With