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












0















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.










share|improve this question


















  • 2





    That is actually not very good. Not only because the padding (which can only be "cleared" by using e.g. memset), but also because it's a really bad way to compare floating point values. In fact, you should not even use == to compare floating point values, as two values that might seem equal in fact might not be (due to compounding rounding errors in calculation). To compare floating point values check if the difference between the values are smaller than a specific epsilon.

    – Some programmer dude
    Nov 18 '18 at 7:07













  • That's why I replaced the memcmp with the ULP-based floating point comparison, for this particular case. The question is related to the padding bytes, in particular, how to avoid the failure in comparing two structs that otherwise are identical using memcmp or what's the best way to compare them.

    – Guille
    Nov 18 '18 at 7:11


















0















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.










share|improve this question


















  • 2





    That is actually not very good. Not only because the padding (which can only be "cleared" by using e.g. memset), but also because it's a really bad way to compare floating point values. In fact, you should not even use == to compare floating point values, as two values that might seem equal in fact might not be (due to compounding rounding errors in calculation). To compare floating point values check if the difference between the values are smaller than a specific epsilon.

    – Some programmer dude
    Nov 18 '18 at 7:07













  • That's why I replaced the memcmp with the ULP-based floating point comparison, for this particular case. The question is related to the padding bytes, in particular, how to avoid the failure in comparing two structs that otherwise are identical using memcmp or what's the best way to compare them.

    – Guille
    Nov 18 '18 at 7:11
















0












0








0








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.










share|improve this question














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.







c++ struct padding






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 '18 at 7:03









GuilleGuille

563




563








  • 2





    That is actually not very good. Not only because the padding (which can only be "cleared" by using e.g. memset), but also because it's a really bad way to compare floating point values. In fact, you should not even use == to compare floating point values, as two values that might seem equal in fact might not be (due to compounding rounding errors in calculation). To compare floating point values check if the difference between the values are smaller than a specific epsilon.

    – Some programmer dude
    Nov 18 '18 at 7:07













  • That's why I replaced the memcmp with the ULP-based floating point comparison, for this particular case. The question is related to the padding bytes, in particular, how to avoid the failure in comparing two structs that otherwise are identical using memcmp or what's the best way to compare them.

    – Guille
    Nov 18 '18 at 7:11
















  • 2





    That is actually not very good. Not only because the padding (which can only be "cleared" by using e.g. memset), but also because it's a really bad way to compare floating point values. In fact, you should not even use == to compare floating point values, as two values that might seem equal in fact might not be (due to compounding rounding errors in calculation). To compare floating point values check if the difference between the values are smaller than a specific epsilon.

    – Some programmer dude
    Nov 18 '18 at 7:07













  • That's why I replaced the memcmp with the ULP-based floating point comparison, for this particular case. The question is related to the padding bytes, in particular, how to avoid the failure in comparing two structs that otherwise are identical using memcmp or what's the best way to compare them.

    – Guille
    Nov 18 '18 at 7:11










2




2





That is actually not very good. Not only because the padding (which can only be "cleared" by using e.g. memset), but also because it's a really bad way to compare floating point values. In fact, you should not even use == to compare floating point values, as two values that might seem equal in fact might not be (due to compounding rounding errors in calculation). To compare floating point values check if the difference between the values are smaller than a specific epsilon.

– Some programmer dude
Nov 18 '18 at 7:07







That is actually not very good. Not only because the padding (which can only be "cleared" by using e.g. memset), but also because it's a really bad way to compare floating point values. In fact, you should not even use == to compare floating point values, as two values that might seem equal in fact might not be (due to compounding rounding errors in calculation). To compare floating point values check if the difference between the values are smaller than a specific epsilon.

– Some programmer dude
Nov 18 '18 at 7:07















That's why I replaced the memcmp with the ULP-based floating point comparison, for this particular case. The question is related to the padding bytes, in particular, how to avoid the failure in comparing two structs that otherwise are identical using memcmp or what's the best way to compare them.

– Guille
Nov 18 '18 at 7:11







That's why I replaced the memcmp with the ULP-based floating point comparison, for this particular case. The question is related to the padding bytes, in particular, how to avoid the failure in comparing two structs that otherwise are identical using memcmp or what's the best way to compare them.

– Guille
Nov 18 '18 at 7:11














3 Answers
3






active

oldest

votes


















1














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





share|improve this answer
























  • This looks good, thanks. I didn't know about tuple and tie. I'll read about them. In the meantime, does this approach compare struct members using the == operator of the member's type, in the case they're not primitives? Is there a way to assign a comparison function?

    – Guille
    Nov 18 '18 at 8:31











  • @Guille it uses operator== correctly for each type in the tuple. en.cppreference.com/w/cpp/utility/tuple/operator_cmp

    – Richard Hodges
    Nov 18 '18 at 8:54



















0














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.






share|improve this answer
























  • There's no such guarantee; assigning to any struct member or copying the struct makes any padding bytes become unspecified

    – M.M
    Nov 19 '18 at 4:27



















0
















  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.






share|improve this answer


























  • This actually makes it worse. sizeof(struct s_zoneData) in this case is 64, which means it added 7 padding bytes before finep and 1 before pad. However, moving the pad member to the first place in the structure keeps the structure size at 56 bytes, effectively referring to the padding bytes.

    – Guille
    Nov 18 '18 at 8:39













  • @Guille You're right, some playing around with the members layout is also needed to avoid internal padding. But other than that the idea holds. I'll change the answer to include your comment.

    – SomeWittyUsername
    Nov 19 '18 at 4:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358639%2fhow-can-i-clear-the-padding-bytes-in-struct-for-comparison%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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





share|improve this answer
























  • This looks good, thanks. I didn't know about tuple and tie. I'll read about them. In the meantime, does this approach compare struct members using the == operator of the member's type, in the case they're not primitives? Is there a way to assign a comparison function?

    – Guille
    Nov 18 '18 at 8:31











  • @Guille it uses operator== correctly for each type in the tuple. en.cppreference.com/w/cpp/utility/tuple/operator_cmp

    – Richard Hodges
    Nov 18 '18 at 8:54
















1














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





share|improve this answer
























  • This looks good, thanks. I didn't know about tuple and tie. I'll read about them. In the meantime, does this approach compare struct members using the == operator of the member's type, in the case they're not primitives? Is there a way to assign a comparison function?

    – Guille
    Nov 18 '18 at 8:31











  • @Guille it uses operator== correctly for each type in the tuple. en.cppreference.com/w/cpp/utility/tuple/operator_cmp

    – Richard Hodges
    Nov 18 '18 at 8:54














1












1








1







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '18 at 7:20









Richard HodgesRichard Hodges

55.5k657100




55.5k657100













  • This looks good, thanks. I didn't know about tuple and tie. I'll read about them. In the meantime, does this approach compare struct members using the == operator of the member's type, in the case they're not primitives? Is there a way to assign a comparison function?

    – Guille
    Nov 18 '18 at 8:31











  • @Guille it uses operator== correctly for each type in the tuple. en.cppreference.com/w/cpp/utility/tuple/operator_cmp

    – Richard Hodges
    Nov 18 '18 at 8:54



















  • This looks good, thanks. I didn't know about tuple and tie. I'll read about them. In the meantime, does this approach compare struct members using the == operator of the member's type, in the case they're not primitives? Is there a way to assign a comparison function?

    – Guille
    Nov 18 '18 at 8:31











  • @Guille it uses operator== correctly for each type in the tuple. en.cppreference.com/w/cpp/utility/tuple/operator_cmp

    – Richard Hodges
    Nov 18 '18 at 8:54

















This looks good, thanks. I didn't know about tuple and tie. I'll read about them. In the meantime, does this approach compare struct members using the == operator of the member's type, in the case they're not primitives? Is there a way to assign a comparison function?

– Guille
Nov 18 '18 at 8:31





This looks good, thanks. I didn't know about tuple and tie. I'll read about them. In the meantime, does this approach compare struct members using the == operator of the member's type, in the case they're not primitives? Is there a way to assign a comparison function?

– Guille
Nov 18 '18 at 8:31













@Guille it uses operator== correctly for each type in the tuple. en.cppreference.com/w/cpp/utility/tuple/operator_cmp

– Richard Hodges
Nov 18 '18 at 8:54





@Guille it uses operator== correctly for each type in the tuple. en.cppreference.com/w/cpp/utility/tuple/operator_cmp

– Richard Hodges
Nov 18 '18 at 8:54













0














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.






share|improve this answer
























  • There's no such guarantee; assigning to any struct member or copying the struct makes any padding bytes become unspecified

    – M.M
    Nov 19 '18 at 4:27
















0














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.






share|improve this answer
























  • There's no such guarantee; assigning to any struct member or copying the struct makes any padding bytes become unspecified

    – M.M
    Nov 19 '18 at 4:27














0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '18 at 7:17









Chris DoddChris Dodd

80.8k680160




80.8k680160













  • There's no such guarantee; assigning to any struct member or copying the struct makes any padding bytes become unspecified

    – M.M
    Nov 19 '18 at 4:27



















  • There's no such guarantee; assigning to any struct member or copying the struct makes any padding bytes become unspecified

    – M.M
    Nov 19 '18 at 4:27

















There's no such guarantee; assigning to any struct member or copying the struct makes any padding bytes become unspecified

– M.M
Nov 19 '18 at 4:27





There's no such guarantee; assigning to any struct member or copying the struct makes any padding bytes become unspecified

– M.M
Nov 19 '18 at 4:27











0
















  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.






share|improve this answer


























  • This actually makes it worse. sizeof(struct s_zoneData) in this case is 64, which means it added 7 padding bytes before finep and 1 before pad. However, moving the pad member to the first place in the structure keeps the structure size at 56 bytes, effectively referring to the padding bytes.

    – Guille
    Nov 18 '18 at 8:39













  • @Guille You're right, some playing around with the members layout is also needed to avoid internal padding. But other than that the idea holds. I'll change the answer to include your comment.

    – SomeWittyUsername
    Nov 19 '18 at 4:11
















0
















  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.






share|improve this answer


























  • This actually makes it worse. sizeof(struct s_zoneData) in this case is 64, which means it added 7 padding bytes before finep and 1 before pad. However, moving the pad member to the first place in the structure keeps the structure size at 56 bytes, effectively referring to the padding bytes.

    – Guille
    Nov 18 '18 at 8:39













  • @Guille You're right, some playing around with the members layout is also needed to avoid internal padding. But other than that the idea holds. I'll change the answer to include your comment.

    – SomeWittyUsername
    Nov 19 '18 at 4:11














0












0








0









  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.






share|improve this answer

















  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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 4:14

























answered Nov 18 '18 at 7:33









SomeWittyUsernameSomeWittyUsername

14.9k22967




14.9k22967













  • This actually makes it worse. sizeof(struct s_zoneData) in this case is 64, which means it added 7 padding bytes before finep and 1 before pad. However, moving the pad member to the first place in the structure keeps the structure size at 56 bytes, effectively referring to the padding bytes.

    – Guille
    Nov 18 '18 at 8:39













  • @Guille You're right, some playing around with the members layout is also needed to avoid internal padding. But other than that the idea holds. I'll change the answer to include your comment.

    – SomeWittyUsername
    Nov 19 '18 at 4:11



















  • This actually makes it worse. sizeof(struct s_zoneData) in this case is 64, which means it added 7 padding bytes before finep and 1 before pad. However, moving the pad member to the first place in the structure keeps the structure size at 56 bytes, effectively referring to the padding bytes.

    – Guille
    Nov 18 '18 at 8:39













  • @Guille You're right, some playing around with the members layout is also needed to avoid internal padding. But other than that the idea holds. I'll change the answer to include your comment.

    – SomeWittyUsername
    Nov 19 '18 at 4:11

















This actually makes it worse. sizeof(struct s_zoneData) in this case is 64, which means it added 7 padding bytes before finep and 1 before pad. However, moving the pad member to the first place in the structure keeps the structure size at 56 bytes, effectively referring to the padding bytes.

– Guille
Nov 18 '18 at 8:39







This actually makes it worse. sizeof(struct s_zoneData) in this case is 64, which means it added 7 padding bytes before finep and 1 before pad. However, moving the pad member to the first place in the structure keeps the structure size at 56 bytes, effectively referring to the padding bytes.

– Guille
Nov 18 '18 at 8:39















@Guille You're right, some playing around with the members layout is also needed to avoid internal padding. But other than that the idea holds. I'll change the answer to include your comment.

– SomeWittyUsername
Nov 19 '18 at 4:11





@Guille You're right, some playing around with the members layout is also needed to avoid internal padding. But other than that the idea holds. I'll change the answer to include your comment.

– SomeWittyUsername
Nov 19 '18 at 4:11


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358639%2fhow-can-i-clear-the-padding-bytes-in-struct-for-comparison%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?