Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are C++ array members handled in copy control functions?

This is something I have wondered for a long time. Take the following example:

struct matrix {     float data[16]; }; 

I know what the default constructor and destructor do in this specific example (nothing), but what about the copy constructor and the copy assignment operator?

struct matrix {     float data[16];      // automatically generated copy constructor     matrix(const matrix& that) : // What happens here?     {         // (or here?)     }      // automatically generated copy assignment operator     matrix& operator=(const matrix& that)     {         // What happens here?          return *this;     } }; 

Does it involve std::copy or std::uninitialized_copy or memcpy or memmove or what?

like image 564
fredoverflow Avatar asked Nov 12 '10 11:11

fredoverflow


People also ask

Are array members deeply copied in C?

C and C++ don't have "deep copy" and "shallow copy" specifically. Instead they have member-wise copy. You could call this "shallow" if the member is a pointer or resource handle, and "deep" if the member is an array or a resource handler which has a copy-constructor that duplicates the resource.

How does array copy work in c#?

CopyTo copies all the elements of the current array to the specified destination array. This method should be called from the source array and it takes two parameters. The first being the array you want to copy to, and the second parameter tells it what index of the destination array it should start copying into.

What is an array copy?

Copy(Array, Array, Int32) Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.

Can we copy an array using the assignment operator in C?

Arrays are not assignable. You cannot use an array type variable as LHS operand of assignment operator. An assignment operator shall have a modifiable lvalue as its left operand.


2 Answers

This is what the standard says in 12.8 (Copying class objects). Copy construction:

Each subobject is copied in the manner appropriate to its type:

  • if the subobject is of class type, the copy constructor for the class is used;
  • if the subobject is an array, each element is copied, in the manner appropriate to the element type;
  • if the subobject is of scalar type, the built-in assignment operator is used.

Copy assignment:

Each subobject is assigned in the manner appropriate to its type:

  • if the subobject is of class type, the copy assignment operator for the class is used (as if by explicit qualification; that is, ignoring any possible virtual overriding functions in more derived classes);
  • if the subobject is an array, each element is assigned, in the manner appropriate to the element type;
  • if the subobject is of scalar type, the built-in assignment operator is used.
like image 77
icecrime Avatar answered Sep 16 '22 15:09

icecrime


Both copies elements in the array (instead of doing nothing or copying pointer).

struct X {     char data_[100]; };   int main ()  {     X orig, copy_assign;     orig.data_[10] = 'a';     copy_assign = orig;     X copy_constructor(orig);     printf("orginal10:%c, copy_assign10:%c, copy_constructor10:%c\n",orig.data_[10],copy_assign.data_[10],copy_constructor.data_[10]);     copy_assign.data_[10] = 'b';     printf("original10:%c, copy_assign10:%c, copy_constructor10:%c\n",orig.data_[10],copy_assign.data_[10],copy_constructor.data_[10]);     copy_constructor.data_[10] = 'c';     printf("original10:%c, copy_assign10:%c, copy_constructor10:%c\n",orig.data_[10],copy_assign.data_[10],copy_constructor.data_[10]);     return 0; } 

running results:

orginal10:a, copy_assign10:a, copy_constructor10:a original10:a, copy_assign10:b, copy_constructor10:a original10:a, copy_assign10:b, copy_constructor10:c 
  • From the first line of the result, we can see that at least something was copied (it is either the elements in the array, or the array pointer was copied).
  • From the next two lines, we can see that changing copy assigned objects and copy constructed objects' array did not change the original array. Therefore we conclude that elements were copied instead of array pointer.

Hope this example is clear.

like image 27
tartaruga_casco_mole Avatar answered Sep 18 '22 15:09

tartaruga_casco_mole