Can I use memcpy in C++ to copy classes that have no pointers or virtual functions

前端 未结 11 1144
灰色年华
灰色年华 2020-12-04 13:16

Say I have a class, something like the following;

class MyClass
{
public:
  MyClass();
  int a,b,c;
  double x,y,z;
};

#define  PageSize 1000000

MyClass Ar         


        
相关标签:
11条回答
  • 2020-12-04 13:50

    According to the Standard, if no copy constructor is provided by the programmer for a class, the compiler will synthesize a constructor which exhibits default memberwise initialization. (12.8.8) However, in 12.8.1, the Standard also says,

    A class object can be copied in two ways, by initialization (12.1, 8.5), including for function argument passing (5.2.2) and for function value return (6.6.3), and by assignment (5.17). Conceptually, these two operations are implemented by a copy constructor (12.1) and copy assignment operator (13.5.3).

    The operative word here is "conceptually," which, according to Lippman gives compiler designers an 'out' to actually doing memberwise initialization in "trivial" (12.8.6) implicitly defined copy constructors.

    In practice, then, compilers have to synthesize copy constructors for these classes that exhibit behavior as if they were doing memberwise initialization. But if the class exhibits "Bitwise Copy Semantics" (Lippman, p. 43) then the compiler does not have to synthesize a copy constructor (which would result in a function call, possibly inlined) and do bitwise copy instead. This claim is apparently backed up in the ARM, but I haven't looked this up yet.

    Using a compiler to validate that something is Standard-compliant is always a bad idea, but compiling your code and viewing the resulting assembly seems to verify that the compiler is not doing memberwise initialization in a synthesized copy constructor, but doing a memcpy instead:

    #include <cstdlib>
    
    class MyClass
    {
    public:
        MyClass(){};
      int a,b,c;
      double x,y,z;
    };
    
    int main()
    {
        MyClass c;
        MyClass d = c;
    
        return 0;
    }
    

    The assembly generated for MyClass d = c; is:

    000000013F441048  lea         rdi,[d] 
    000000013F44104D  lea         rsi,[c] 
    000000013F441052  mov         ecx,28h 
    000000013F441057  rep movs    byte ptr [rdi],byte ptr [rsi] 
    

    ...where 28h is the sizeof(MyClass).

    This was compiled under MSVC9 in Debug mode.

    EDIT:

    The long and the short of this post is that:

    1) So long as doing a bitwise copy will exhibit the same side effects as memberwise copy would, the Standard allows trivial implicit copy constructors to do a memcpy instead of memberwise copies.

    2) Some compilers actually do memcpys instead of synthesizing a trivial copy constructor which does memberwise copies.

    0 讨论(0)
  • 2020-12-04 14:00

    It will work, because a (POD-) class is the same as a struct (not completely, default access ...), in C++. And you may copy a POD struct with memcpy.

    The definition of POD was no virtual functions, no constructor, deconstructor no virtual inheritance ... etc.

    0 讨论(0)
  • 2020-12-04 14:04

    Your class has a constructor, and so is not POD in the sense that a C struct is. It is therefore not safe to copy it with memcpy(). If you want POD data, remove the constructor. If you want non-POD data, where controlled construction is essential, don't use memcpy() - you can't have both.

    0 讨论(0)
  • 2020-12-04 14:04

    [...] but are there any other hidden nasties I should be aware of?

    Yes: your code makes certain assumptions that are neither suggested nor documented (unless you specifically document them). This is a nightmare for maintenance.

    Also, your implementation is basically hacking (if it's necessary it's not a bad thing) and it may depend (not sure on this) on how your current compiler implements things.

    This means that if you upgrade compiler / toolchain one year (or five) from now (or just change optimization settings in your current compiler) nobody will remember this hack (unless you make a big effort to keep it visible) and you may end up with undefined behavior on your hands, and developers cursing "whoever did this" a few years down the road.

    It's not that the decision is unsound, it's that it is (or will be) unexpected to maintainers.

    To minimize this (unexpectedness?) I would move the class into a structure within a namespace based on the current name of the class, with no internal functions in the structure at all. Then you are making it clear you're looking at a memory block and treating it as a memory block.

    Instead of:

    class MyClass
    {
    public:
        MyClass();
        int a,b,c;
        double x,y,z;
    };
    
    #define  PageSize 1000000
    
    MyClass Array1[PageSize],Array2[PageSize];
    
    memcpy(Array1,Array2,PageSize*sizeof(MyClass));
    

    You should have:

    namespace MyClass // obviously not a class, 
                      // name should be changed to something meaningfull
    {
        struct Data
        {
            int a,b,c;
            double x,y,z;
        };
    
        static const size_t PageSize = 1000000; // use static const instead of #define
    
    
        void Copy(Data* a1, Data* a2, const size_t count)
        {
            memcpy( a1, a2, count * sizeof(Data) );
        }
    
        // any other operations that you'd have declared within 
        // MyClass should be put here
    }
    
    MyClass::Data Array1[MyClass::PageSize],Array2[MyClass::PageSize];
    MyClass::Copy( Array1, Array2, MyClass::PageSize );
    

    This way you:

    • make it clear that MyClass::Data is a POD structure, not a class (binary they will be the same or very close - the same if I remember correctly) but this way it is also visible to programmers reading the code.

    • centralize the usage of memcpy (if you have to change to a std::copy or something else) in two years you do it in a single point.

    • keep the usage of memcpy near the implementation of the POD structure.

    0 讨论(0)
  • 2020-12-04 14:11

    As pointed out by John Dibling, you should not use memcpy manually. Instead, use std::copy. If your class is memcpy-able, std::copy will automatically do a memcpy. It may be even faster than a manual memcpy.

    If you use std::copy, your code is readable and it always uses the fastest way to copy. And if you change the layout of your class at a later point so that it is not memcpy-able any more, the code that uses std::copy will not break, while your manual calls to memcpy will.

    Now, how do you know whether your class is memcpy-able? In the same way, std::copy detects that. It uses: std::is_trivially_copyable. You can use a static_assert to make sure that this property is upheld.

    Note that std::is_trivially_copyable can only check the type information. It does not understand the semantics. The following class is trivially copyable type, but a bitwise copy would be a bug:

    #include <type_traits>
    
    struct A {
      int* p = new int[32];
    };
    
    static_assert(std::is_trivially_copyable<A>::value, "");
    

    After a bitwise copy, the ptr of the copy will still point to the original memory. Also see the Rule of Three.

    0 讨论(0)
提交回复
热议问题