This example :
#include <iostream>
#include <cstring>
struct A
{
int a;
bool b;
};
bool foo( const A a1, const A a2 )
{
return ( 0 == std::memcmp( &a1, &a2, sizeof( A ) ) );
}
int main()
{
A a1 = A();
a1.a = 5;a1.b = true;
A a2 = A();
a2.a = 5;a2.b = true;
std::cout<<std::boolalpha << foo( a1, a2 ) << std::endl;
}
is going to produce false
, because of padding.
I do not have access to the foo
function, and I can not change the way the comparison is done.
Assuming a bool
occupies 1 byte (that is true on my system), if I change the struct A
to this :
struct A
{
int a;
bool b;
char dummy[3];
};
then it works fine on my system (the output is true
).
Is there anything else I could do to fix the above problem (get the true
output)?