Guille
Guille

Reputation: 343

How can I clear the padding bytes in struct for comparison?

I have a struct defined as follows:

struct s_zoneData {
    bool finep = true;
    double pzone_tcp = 1.0;
    double pzone_ori = 1.0;
    double pzone_eax = 1.0;
    double zone_ori  = 0.1;
    double zone_leax = 1.0;
    double zone_reax = 0.1;
};

I created a comparison operator:

bool operator==(struct s_zoneData i, struct s_zoneData j) {

    return (memcmp(&i, &j, sizeof(struct s_zoneData)) == 0);

}

Most of the time, the comparisons failed, even for identical variables. It took me some time (and messing with gdb) to realize that the problem is that the padding bytes for the finep structure element are uninitialized rubbish. For reference, in my machine (x64), sizeof(struct s_zoneData) is 56, which means there are 7 padding bytes for the finep element.

At first, I solved the problem replacing the memcmp with an ULP-based floating-point value comparison for each member of the struct, because I thought there might be rounding issues at play. But now I want to dig deeper in this problem and see possible alternative solutions.

The question is, is there any way to specify a value for the padding bytes, for different compilers and platforms? Or, rewriting it as a more general question because I might be too focused on my approach, what would be the correct way to compare two struct s_zoneData variables?

I know that creating a dummy variable such as char pad[7] and initializing it with zeros should solve the problem (at least for my particular case), but I've read multiple cases where people had struct alignment issues for different compilers and member order, so I'd prefer to go with a standard-defined solution, if that exists. Or at least, something that guarantees compatibility for different platforms and compilers.

Upvotes: 2

Views: 933

Answers (3)

SomeWittyUsername
SomeWittyUsername

Reputation: 18368

  1. #pragma pack can remove the extra padding.
  2. You can prevent the extra-padding addition by adding it manually so that it can be set explicitly to a predefined value (but the initialization will have to be done outside the struct):


struct s_zoneData {
    char pad[sizeof(double)-sizeof(bool)];
    bool finep;
    double pzone_tcp;
    double pzone_ori;
    double pzone_eax;
    double zone_ori;
    double zone_leax;
    double zone_reax;
};

...
s_zoneData X = {{},true, 1.0, 1.0, 0.1, 1.0, 0.1};

Edit: Per @Guille comment, the padding should be coupled with the bool member to prevent the internal padding. So either the pad should be immediately before /after finep (I changed the sample to that) or finep should be moved to the end of the structure.

Upvotes: 0

Richard Hodges
Richard Hodges

Reputation: 69902

While what you're doing would seem logical to a c or assembly programmer (and indeed many c++ programmers), what you are inadvertently doing is breaking the c++ object model and invoking undefined behaviour.

You might want to consider comparisons of value types in terms of tuples of references to their data members.

Comparing two such tuples yields the correct behaviour for ordering comparisons as well as equality.

They also optimise very well.

eg:

#include <tuple>

struct s_zoneData {
    bool finep = true;
    double pzone_tcp = 1.0;
    double pzone_ori = 1.0;
    double pzone_eax = 1.0;
    double zone_ori  = 0.1;
    double zone_leax = 1.0;
    double zone_reax = 0.1;

    friend auto as_tuple(s_zoneData const & z)
    {
        using std::tie;
        return tie(z.finep, z.pzone_tcp, z.pzone_ori, z.pzone_eax, z.zone_ori, z.zone_leax, z.zone_reax);
    }
};

auto operator ==(s_zoneData const& l, s_zoneData const& r) -> bool
{
    return as_tuple(l) == as_tuple(r);
}

example assembler output:

operator==(s_zoneData const&, s_zoneData const&):
  xor eax, eax
  movzx ecx, BYTE PTR [rsi]
  cmp BYTE PTR [rdi], cl
  je .L20
  ret
.L20:
  movsd xmm0, QWORD PTR [rdi+8]
  ucomisd xmm0, QWORD PTR [rsi+8]
  jp .L13
  jne .L13
  movsd xmm0, QWORD PTR [rdi+16]
  ucomisd xmm0, QWORD PTR [rsi+16]
  jp .L13
  jne .L13
  movsd xmm0, QWORD PTR [rdi+24]
  ucomisd xmm0, QWORD PTR [rsi+24]
  jp .L13
  jne .L13
  movsd xmm0, QWORD PTR [rdi+32]
  ucomisd xmm0, QWORD PTR [rsi+32]
  jp .L13
  jne .L13
  movsd xmm0, QWORD PTR [rdi+40]
  ucomisd xmm0, QWORD PTR [rsi+40]
  jp .L13
  jne .L13
  movsd xmm0, QWORD PTR [rdi+48]
  ucomisd xmm0, QWORD PTR [rsi+48]
  mov edx, 0
  setnp al
  cmovne eax, edx
  ret
.L13:
  xor eax, eax
  ret

Upvotes: 1

Chris Dodd
Chris Dodd

Reputation: 126408

The only way to set the padding bytes to something predicatable is to use memset to set the entire structure to something predictable -- if you always use memset to clear values of the structure before setting the fields to something else, then you can rely on the padding bytes to remain unchanged even when you copy the entire structure (as when you pass it as an argument). In addition, a variable with static storage duration will have the padding bytes initialized to 0.

Upvotes: 0

Related Questions