Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the compiler allowed to modify padding bytes

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....

like image 712
DarthRubik Avatar asked Jan 24 '23 04:01

DarthRubik


1 Answers

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…

like image 197
Eric Postpischil Avatar answered Jan 29 '23 15:01

Eric Postpischil