There are two cases of discrimination.
If your structure is POD , the copy is optimized and will run as fast as memcpy (with the right level of optimization).
If your structure is not a POD , C ++ should call the copy constructor for your object. The copy constructor may call other functions, new operators, etc., so it will be slower than memcpy. But memcpy will not copy struct correcty, using memcpy for the non-POD type as a result of undefined behavior!
Note that, for example, in g++ the memcpy call will be nested and optimized. Since the intention between the struct copy and memcpy call is the same (copy X bytes from location Y to Z), I don't think the generated assembly code will be different.
In any case, be sure to find out by analyzing your code assembly.
Edit: just read the end of the question about function parameters. Note that passing parameters to a function is usually (especially in x64) done in registers and much faster than memcpy .
I checked the build code and they are different . The exact code will depend on the calling convention your current compiler uses. For me, the structure is not passed to the registers, but passed to the stack, and the actual copy is created. Three int are passed in %ecx , %edx and %r8d . I tried this on windows gcc. It seems to be using the prompt for Windows x64.
For details on how parameters are passed, see the specifications of your calling convention. All parts and corner cabinets are designed. For instance. for x64 GCC see System V AMD64 ABI Chapter 3.2.3 Passing Parameters. For Visual Studio, look here .
source share