Use memcmp to compare objects?

Source: Internet
Author: User
I Came authentication ss a question regarding C ++, is it more efficient to use memcmp to determine equality of two objects of the same type.
This is not a question regarding efficiency at all, it's about correctness. Using memcmp to compare two objects may be correct sometimes, but it really depends on several factors:
    1. Class alignment
    2. Compiler implementation
    3. Compiler Configuration

You may get different results when work with different types, with different compilers. even worse, you may get different results between two invocations in the same environment. though it seems to be efficient, it's not reliable.

We know compute compiler will align a class's members to word size for better performance, because it's harder to read or write memories at arbitrary location. so possibly, there are gaps (unused memories) between fields.

Those gaps are occupied by objects of the class, but are note directly managed through objects. the contents of these gaps are undefined. they may be what's left over since their last usage. or they might be cleared/filled by a diligent compiler.
When you use memcmp to compare two objects, these gaps which has random bits are also taken into considertion. But this is undesired behavior and leads to uncertainty.

So, never do this unless you're 100% sure about the memory layout, compiler behavior, and you really don't care portability, and you really want to gain the efficiency.

The demo below shall show using memcmp doesn't work correctly with Microsoft's c ++ compiler v15.00.30729.01 and GCC v4.4.1.

 
# include "string. H "
// ====================================== =
// class: foo
// description:
// ===========================================< BR> class Foo
{< br> Public:
Foo (): A (0), B (0), C (0) {
}; // constructor
int;
char B;
int C;

}; // ----- end of class Foo -----
void shuffle_stack ()
{< br> Foo F1;
Foo F2;
* (int *) (& f1. B) = 0x87654321;
* (int *) (& f2. B) = 0x12345678;
}< br>
int compare ()
{< br> Foo F1;
Foo F2;
return memcmp (void *) (& F1), (void *) (& F2), sizeof (FOO);
}< br>
int main (INT argc, char * argv [])
{< br> int rc = 0;
shuffle_stack ();
rc = compare ();
return 0;
}// ---------- end of function main ----------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.