How does C / C ++ copy structures?

When passing a structure to C / C ++ by value, the contents of the structure must be copied. How do compilers achieve this? Ie, what assembly instructions are usually emitted for this copy?

How quickly does this happen, for example, when compared to calling memcpy?

Now consider this code:

struct X { int i, j, k; }; void foo(X x); void foo( int i, int j, int k); 

Is there a difference between calls to foo (X) and foo (int, int, int), or can the generated assembler code be the same (given the passage of parameters)?

+4
source share
4 answers

Obviously, if a constructor exists for a struct or class , then the constructor is called.

If the constructor does not exist, it depends entirely on the compiler, but most likely there will be three separate mov commands for three objects with an integer size. For large structures, this is either a memcpy call or an embedded version similar to memcpy .

It is also very likely that if the structure is VERY large (several megabytes), then true memcpy faster than the built-in version, and the compiler may not understand this and use the attached version in any case. But most of us do not use large megabytes of large structures, so I do not think that in general, something is too much to worry about. Copying structures onto the stack as arguments, if the structure has a large megabyte, is probably not a great idea in the first place, given the limited size of a typical stack.

+4
source

In c ++

How do compilers achieve this?

They call the copy constructor for this class / structure. Implicitly generated unless you provide one or the one you provide.

How quickly does this happen, for example, when compared to calling memcpy ?

Depends on the class and its members. Profiling should give you a sharper image.
However, you should avoid using memcpy to copy class instances.

In C

How do compilers achieve this?

They make a shallow copy for this structure. For all practical purposes, you can consider this the same as memcpy .

+5
source

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 .

+3
source

See another Alok Save answer for . In c , it can be memcpy (or the equivalent) or its built-in version (up to one mov for structures with good size).

0
source

All Articles