Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does struct assignment guarantee padding to also be equal

Say I have a struct with 2 fields and the implementation of C that I have also has some padding between these fields.

If I create two variables of the struct and assign one to another, will the padding be guaranteed to be equal?

I know that for most compilers it would be so (because they just call memcpy), but I want to know what is specified about the padding in the standard?

Intention for this question is, can I use memcmp to check equality of structs.

Say I have a compiler which emits code that just assigns all the members of the struct instead of doing memcpy, will it be a valid implementation of the assignment of struct operation?

like image 318
Ajay Brahmakshatriya Avatar asked Jun 06 '17 17:06

Ajay Brahmakshatriya


People also ask

Does sizeof struct include padding?

The sizeof for a struct is not always equal to the sum of sizeof of each individual member. This is because of the padding added by the compiler to avoid alignment issues. Padding is only added when a structure member is followed by a member with a larger size or at the end of the structure.

Why structure padding is required?

The answer to that lies in how a CPU accesses memory. Typically a CPU has alignment constraints, e.g. a CPU will access one word at a time, or a CPU will require data to be 16byte aligned, etc. So to make sure that data is aligned according to the constraints of the CPU, padding is required.

Can you set a struct equal to another?

Yes if the structure is of the same type. Think it as a memory copy. Keep in mind that there's no deep copy, pointed to memory isn't copied.

What is a packed struct in C?

Generally packed structures are used: To save space. To format a data structure to transmit over network without depending on each architecture alignment of each node of the network.


2 Answers

The standard says in a note 51 of 6.2.6.1 General:

Thus, for example, structure assignment need not copy any padding bits.

like image 59
Eugene Sh. Avatar answered Oct 06 '22 19:10

Eugene Sh.


6.2.6.1 General
...
6    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.51) The value of a structure or union object is never a trap representation, even though the value of a member of the structure or union object may be a trap representation.

7    When a value is stored in a member of an object of union type, the bytes of the object representation that do not correspond to that member but do correspond to other members take unspecified values.
51) Thus, for example, structure assignment need not copy any padding bits.

C 2011 Online Draft

Footnote 51 directly addresses your question - contents of padding bits may not be copied over in assignment, so memcmp may not work for comparing two structs for equality.

like image 38
John Bode Avatar answered Oct 06 '22 17:10

John Bode