Does structuring alignment provide an equal complement

Let's say I have a structure with 2 fields and an implementation of C, which I also have some addition between these fields.

If I create two structure variables and assign each other, will the indentation be equal?

I know that for most compilers this would be so (because they are simply called memcpy), but I want to know what is indicated in the add-on in the standard?

The intention for this question is: can I use memcmp to check for equality of structures.

Let's say I have a compiler that emits code that just assigns all members of the structure instead of executing memcpy , will this be the correct implementation of the assignment of the struct operation?

+5
source share
3 answers

The standard says in note 51 6.2.6.1 General :

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

+6
source
6.2.6.1 General
...
6 When a value is stored in an object of a structure or type of union, including in a member of an object, the bytes of the representation of the object that correspond to any filling bytes are undefined values. 51) The value of a structure or association object is never a representation of a trap, even if the value of a member of a structure or association object can be a representation of a trap. 7 When a value is stored in a member of an object of type union, bytes of the view object that do not match this member but correspond to other members take unspecified values.
51) Thus, for example, a structure assignment should not copy any padding bits.

C 2011 Online Draft

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

+6
source

I don’t think that you can use memcmp because the memory between the members (ie the contents in the "padding" -area) is not guaranteed to have a definite meaning and assigning such a structural object to another does not need to copy the contents of the add-ons either.

+1
source

Source: https://habr.com/ru/post/1268600/


All Articles