I need to compare about 25k bytes of data.
size_t const len = 25600;
char unsigned *const a = new char unsigned[len];
*const b = new char unsigned[len];
/* Alter memory chunk a */
/* Alter memory chunk b */
memcmp(a,b,len);
I'd like to be able to do a simple comparison of memory between 2
structures containing intrinsic data and find the position where the
first difference occurs.
Too unspecific.
Is there an easy way to do this?
Yes, probably, but I don't know your requirements.
I'd like to use a call like memcmp but that only provides <, or =
responses. I thought maybe there was some sort of "memdiff" call that
would give me the position where the 2 structures are different.
Needless to say, I don't want to write overloaded operator== methods
that will handle the comparisons - my 25k of data is a series of structs
of structs that will be a pain to write a method for every one.
These struct objects might contain padding. It's possible for two POD
objects to be identical, but yet have padding which is different. Do you
want these to still compare equal? If so, your ownly choice is to perform
an equality object-by-object.
You might start off with an algorithm to tell you if a particular byte is a
padding byte or not:
bool IsPaddingByte(MyPOD const &obj,char unsigned const *const p)
{
struct NoPaddingRange {
char unsigned const *p;
size_t len;
};
/* Let's say it has six members */
char unsigned const *const member_start_addresses[6] = {
p+offsetof(MyPOD,a),p+offsetof(MyPOD,b),p+offsetof (MyPOD,c),
p+offsetof(MyPOD,d),p+offsetof(MyPOD,e),p+offsetof (MyPOD,f) };
char unsigned const *const member_over_addresses[6] = {
member_start_addresses[0] + sizeof obj.a,
member_start_addresses[1] + sizeof obj.b,
member_start_addresses[2] + sizeof obj.c,
member_start_addresses[3] + sizeof obj.d,
member_start_addresses[4] + sizeof obj.e,
member_start_addresses[5] + sizeof obj.f };
for(unsigned i=0;i!=sizeof ranges/sizeof*ranges;++i)
{
/* This is a tad complicated... */
}
return false;
}
Then you could do as follows:
size_t OffsetWhereDiff(T const &a, T const &b)
{
for (...)
{
if (*p != *q && !IsPaddingByte(p)) return offset;
}
}
--
Frederick Gotham