Do fill bytes of type POD remove?

Suppose I have a POD type:

struct A { char a; int b; }; 

On my system, sizeof(A) == 8 , although sizeof(char) == 1 and sizeof(b) == 4 . This means that the data structure contains 3 unused bytes.

Now suppose that

 A x = ...; A y =x; 

Question:

Is it guaranteed that all 8 bytes x and y will be identical, even these 3 unused ones?

Equivalently, if I transfer the base bytes of some A objects to another program that does not understand their meaning or structure and treat them as an array of 8 bytes, can this other program safely compare two A for equality?

Note: In the experiment with gcc 7, it seems that these bytes are being copied. I would like to know if this is guaranteed.

+45
c ++ copy-constructor
Oct 22 '17 at 14:10
source share
4 answers

An implicit copy / move constructor for a non-unit class X performs a phased copy / move of its bases and members.

12.8 / 15 [class.copy] in N4141

Thus, the bit-bit in padding bytes may differ.

+40
Oct 22 '17 at 14:28
source share
β€” -

This is not an authority, but the cppreference entry for std::memcmp suggests that padding bytes may differ:

memcmp() between two objects of type struct{char c; int n;} struct{char c; int n;} will compare padding bytes whose values ​​may differ when c and n are the same

+9
Oct 22 '17 at 14:29
source share

given that you asked about the type of POD (hence including alliances), it is worth mentioning that according to [class.copy]

The implicit copy / move constructor for combining X copies the representation of the object (6.9) from X

which for trivially copied types should also include padding bits. So this could just be replacing A with

 union A{ struct { char a; int b; }; }; 

(actually a non-standard anonymous structure is used above, but you get the point ...)

+7
Oct 22 '17 at 15:16
source share

Answering your second question:

Equivalently, if I transfer the base bytes of some A objects to another program that does not understand their meaning or structure, and treat them as an array of 8 bytes, can this other program safely compare the two for equality?

Since an object of your type may contain padding bytes, another program usually cannot compare two such objects for equality:

Knowing which bits of bytes that make up an object semantically are the key to determining its presentation representation . However, in this case, the target program only knows the object representation, that is, the bytes sequence representing such an object in memory, including padding bytes. A function like memcmp can only compare objects whose representation of values ​​is identical to representing the object in a meaningful way. If you use it to compare objects by value, even if they have indentation, it may not give the correct results, since it cannot determine which bits in the object's representation are irrelevant so that the representations of the two objects are equal.

See http://en.cppreference.com/w/cpp/language/object

+4
Oct 22 '17 at 14:45
source share



All Articles