Why c++ vector inner elements/array are copied when passed by valueCopy array by valueCan I list-initialize a vector of move-only type?C++ Vector Arrays in Copy ConstructorsC++ vector compile error, when using vector::push_back() methodPass vector of pointers by reference and replace a vector of void* pointersWhy can I not call reserve on a vector of const elements?Passing array of vectors as a function parameter to change the original valuesCapturing array of vectors in lambda makes elements consterror: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(int&)’Copy constructor error when initilizing a vector of custormer typed objects

What is the difference between `command a[bc]d` and `command `ab,cd`

What is the strongest case that can be made in favour of the UK regaining some control over fishing policy after Brexit?

Examples of subgroups where it's nontrivial to show closure under multiplication?

how to find the equation of a circle given points of the circle

How to have a sharp product image?

Size of electromagnet needed to replicate Earth's magnetic field

How to solve constants out of the internal energy equation?

How to get a plain text file version of a CP/M .BAS (M-BASIC) program?

Is there a way to get a compiler for the original B programming language?

Why does academia still use scientific journals and not peer-reviewed government funded alternatives?

How can the Zone of Truth spell be defeated without the caster knowing?

Do I have an "anti-research" personality?

How come there are so many candidates for the 2020 Democratic party presidential nomination?

Phrase for the opposite of "foolproof"

Apply MapThread to all but one variable

How to stop co-workers from teasing me because I know Russian?

Is there an official tutorial for installing Ubuntu 18.04+ on a device with an SSD and an additional internal hard drive?

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

Reducing vertical space in stackrel

Does Gita support doctrine of eternal cycle of birth and death for evil people?

How to verbalise code in Mathematica?

Does a semiconductor follow Ohm's law?

Will a top journal at least read my introduction?

Don’t seats that recline flat defeat the purpose of having seatbelts?



Why c++ vector inner elements/array are copied when passed by value


Copy array by valueCan I list-initialize a vector of move-only type?C++ Vector Arrays in Copy ConstructorsC++ vector compile error, when using vector::push_back() methodPass vector of pointers by reference and replace a vector of void* pointersWhy can I not call reserve on a vector of const elements?Passing array of vectors as a function parameter to change the original valuesCapturing array of vectors in lambda makes elements consterror: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(int&)’Copy constructor error when initilizing a vector of custormer typed objects






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















Why does the inner elements of the vector are copied when the vector is passed by value?



#include<vector>
using namespace std;

// this func won't modify v[2].
// Meaning v[2] (and the whole inner array) was copied
// when v is passed to the func?
void modify(vector<int> v)
v[2] = 100;


// this func modify v[2]
void modify(vector<int>& v)
v[2] = 100;


int main()
vector<int> v = 1,2,3,4,5,6;

// still same
modify(v);

// modified
modified2(v);



I find that it's strange that the actual content of the vector is copied when the vector is passed by value. I picture that the std::vector implementation must have a pointer field which maps to an address on heap, where the actual array is located. So when the vector is passed, even by value, the address should stay the same, pointing to the same content. Something like this:



#include<iostream>

using namespace std;

// a dummy wrapper of an array
// trying to mock vector<int>
class vector_int
public:
int* inner_array; // the actual array
vector_int(int *a)
inner_array = a;

int* at(int pos)
return inner_array+pos;

;

// this passes the "mocked vector" by value
// but 'inner_array' is not copied
void modify(vector_int v)
*(v.at(2)) = 10;


int main()
int* a = new int[3] 1,2,3;
vector_int v = vector_int(a);
modify(v); // v[2] is modified



Is this assumption about the std::vector implementation correct? What makes the vector content being copied when passed by value?




EDIT



Thanks to alter igel's answer and UnholySheep's comment, I figured out the reason why std::vector has value sementics (or why the inner array got copied).



If the copy constructor class is defined explicitly in the class definition, the copy constructor will determine how the struct/class instance is copied when the variable is passed in a function call. So I can define a copy constructor to my vector_int, in which I copy the whole inner_array, like



#include<iostream>

using namespace std;

class vector_int
public:
int* inner_array;
int len;
vector_int(int *a, int len)
inner_array = a;
this->len = len;

int* at(int pos)
return inner_array+pos;

// this is the copy constructor
vector_int(const vector_int &v2)
inner_array = new int;
for (int i =0; i < v2.len; i++)
*(inner_array+i) = *(v2.inner_array+i);


;

// Yay, the vector_int's inner_array is copied
// when this function is called
// and no modification of the original vector is done
void modify(vector_int v)
*(v.at(2)) = 10;


int main()
int* a = new int[3] 1,2,3;
vector_int v = vector_int(a,3);
//
modify(v);



I checked the source code of the stdlib implementation on my local computer (g++ Apple LLVM version 10.0.0). The std::vector defines a copy constructor which looks like this



template <class _Tp, class _Allocator>
vector<_Tp, _Allocator>::vector(const vector& __x)
: __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))

size_type __n = __x.size();
if (__n > 0)

allocate(__n);
__construct_at_end(__x.__begin_, __x.__end_, __n);




which looks like it does an malloc for the actual copied array + copy the array.










share|improve this question



















  • 4





    The copy constructor makes sure it is copied. Also your implementation forgets one crucial detail: the destructor (and the rule of 3/5)

    – UnholySheep
    Mar 22 at 17:28







  • 1





    All C++ standard containers act like value types.

    – melpomene
    Mar 22 at 17:29











  • To define a function signature guaranteeing not to copy a std::vector<T> and not copy T's in turn use a const reference: void foo(const std::vector<T>& v);

    – πάντα ῥεῖ
    Mar 22 at 17:31











  • Value semantics dictates that when you copy a value, the copy is independent of the original. For std::vector to have value semantics, copying it implies copying it's elements. There exists container implementations which perform copy-on-write but this is not practical for std::vector. And this approach loses a lot of it's benefits since the introduction of move semantics. If you are concerned about the cost of copying your vector then you likely don't mean to copy it and should read about reference types and move semantics instead.

    – François Andrieux
    Mar 22 at 17:32












  • looks like that previously you were using langues where pass by reference is common default langue behavior (like Java or C#). In C structs and in C++ classes, when passed by value copy is made. There are some spatial cases where classes implement pattern COW to save memory. There are different approaches. Note that in functional programing you can't modify anything. To create modification you need to create copy with some variation. I'm just saying this is just langue design and there are different approaches, each have pros and cons.

    – Marek R
    Mar 22 at 17:33


















1















Why does the inner elements of the vector are copied when the vector is passed by value?



#include<vector>
using namespace std;

// this func won't modify v[2].
// Meaning v[2] (and the whole inner array) was copied
// when v is passed to the func?
void modify(vector<int> v)
v[2] = 100;


// this func modify v[2]
void modify(vector<int>& v)
v[2] = 100;


int main()
vector<int> v = 1,2,3,4,5,6;

// still same
modify(v);

// modified
modified2(v);



I find that it's strange that the actual content of the vector is copied when the vector is passed by value. I picture that the std::vector implementation must have a pointer field which maps to an address on heap, where the actual array is located. So when the vector is passed, even by value, the address should stay the same, pointing to the same content. Something like this:



#include<iostream>

using namespace std;

// a dummy wrapper of an array
// trying to mock vector<int>
class vector_int
public:
int* inner_array; // the actual array
vector_int(int *a)
inner_array = a;

int* at(int pos)
return inner_array+pos;

;

// this passes the "mocked vector" by value
// but 'inner_array' is not copied
void modify(vector_int v)
*(v.at(2)) = 10;


int main()
int* a = new int[3] 1,2,3;
vector_int v = vector_int(a);
modify(v); // v[2] is modified



Is this assumption about the std::vector implementation correct? What makes the vector content being copied when passed by value?




EDIT



Thanks to alter igel's answer and UnholySheep's comment, I figured out the reason why std::vector has value sementics (or why the inner array got copied).



If the copy constructor class is defined explicitly in the class definition, the copy constructor will determine how the struct/class instance is copied when the variable is passed in a function call. So I can define a copy constructor to my vector_int, in which I copy the whole inner_array, like



#include<iostream>

using namespace std;

class vector_int
public:
int* inner_array;
int len;
vector_int(int *a, int len)
inner_array = a;
this->len = len;

int* at(int pos)
return inner_array+pos;

// this is the copy constructor
vector_int(const vector_int &v2)
inner_array = new int;
for (int i =0; i < v2.len; i++)
*(inner_array+i) = *(v2.inner_array+i);


;

// Yay, the vector_int's inner_array is copied
// when this function is called
// and no modification of the original vector is done
void modify(vector_int v)
*(v.at(2)) = 10;


int main()
int* a = new int[3] 1,2,3;
vector_int v = vector_int(a,3);
//
modify(v);



I checked the source code of the stdlib implementation on my local computer (g++ Apple LLVM version 10.0.0). The std::vector defines a copy constructor which looks like this



template <class _Tp, class _Allocator>
vector<_Tp, _Allocator>::vector(const vector& __x)
: __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))

size_type __n = __x.size();
if (__n > 0)

allocate(__n);
__construct_at_end(__x.__begin_, __x.__end_, __n);




which looks like it does an malloc for the actual copied array + copy the array.










share|improve this question



















  • 4





    The copy constructor makes sure it is copied. Also your implementation forgets one crucial detail: the destructor (and the rule of 3/5)

    – UnholySheep
    Mar 22 at 17:28







  • 1





    All C++ standard containers act like value types.

    – melpomene
    Mar 22 at 17:29











  • To define a function signature guaranteeing not to copy a std::vector<T> and not copy T's in turn use a const reference: void foo(const std::vector<T>& v);

    – πάντα ῥεῖ
    Mar 22 at 17:31











  • Value semantics dictates that when you copy a value, the copy is independent of the original. For std::vector to have value semantics, copying it implies copying it's elements. There exists container implementations which perform copy-on-write but this is not practical for std::vector. And this approach loses a lot of it's benefits since the introduction of move semantics. If you are concerned about the cost of copying your vector then you likely don't mean to copy it and should read about reference types and move semantics instead.

    – François Andrieux
    Mar 22 at 17:32












  • looks like that previously you were using langues where pass by reference is common default langue behavior (like Java or C#). In C structs and in C++ classes, when passed by value copy is made. There are some spatial cases where classes implement pattern COW to save memory. There are different approaches. Note that in functional programing you can't modify anything. To create modification you need to create copy with some variation. I'm just saying this is just langue design and there are different approaches, each have pros and cons.

    – Marek R
    Mar 22 at 17:33














1












1








1








Why does the inner elements of the vector are copied when the vector is passed by value?



#include<vector>
using namespace std;

// this func won't modify v[2].
// Meaning v[2] (and the whole inner array) was copied
// when v is passed to the func?
void modify(vector<int> v)
v[2] = 100;


// this func modify v[2]
void modify(vector<int>& v)
v[2] = 100;


int main()
vector<int> v = 1,2,3,4,5,6;

// still same
modify(v);

// modified
modified2(v);



I find that it's strange that the actual content of the vector is copied when the vector is passed by value. I picture that the std::vector implementation must have a pointer field which maps to an address on heap, where the actual array is located. So when the vector is passed, even by value, the address should stay the same, pointing to the same content. Something like this:



#include<iostream>

using namespace std;

// a dummy wrapper of an array
// trying to mock vector<int>
class vector_int
public:
int* inner_array; // the actual array
vector_int(int *a)
inner_array = a;

int* at(int pos)
return inner_array+pos;

;

// this passes the "mocked vector" by value
// but 'inner_array' is not copied
void modify(vector_int v)
*(v.at(2)) = 10;


int main()
int* a = new int[3] 1,2,3;
vector_int v = vector_int(a);
modify(v); // v[2] is modified



Is this assumption about the std::vector implementation correct? What makes the vector content being copied when passed by value?




EDIT



Thanks to alter igel's answer and UnholySheep's comment, I figured out the reason why std::vector has value sementics (or why the inner array got copied).



If the copy constructor class is defined explicitly in the class definition, the copy constructor will determine how the struct/class instance is copied when the variable is passed in a function call. So I can define a copy constructor to my vector_int, in which I copy the whole inner_array, like



#include<iostream>

using namespace std;

class vector_int
public:
int* inner_array;
int len;
vector_int(int *a, int len)
inner_array = a;
this->len = len;

int* at(int pos)
return inner_array+pos;

// this is the copy constructor
vector_int(const vector_int &v2)
inner_array = new int;
for (int i =0; i < v2.len; i++)
*(inner_array+i) = *(v2.inner_array+i);


;

// Yay, the vector_int's inner_array is copied
// when this function is called
// and no modification of the original vector is done
void modify(vector_int v)
*(v.at(2)) = 10;


int main()
int* a = new int[3] 1,2,3;
vector_int v = vector_int(a,3);
//
modify(v);



I checked the source code of the stdlib implementation on my local computer (g++ Apple LLVM version 10.0.0). The std::vector defines a copy constructor which looks like this



template <class _Tp, class _Allocator>
vector<_Tp, _Allocator>::vector(const vector& __x)
: __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))

size_type __n = __x.size();
if (__n > 0)

allocate(__n);
__construct_at_end(__x.__begin_, __x.__end_, __n);




which looks like it does an malloc for the actual copied array + copy the array.










share|improve this question
















Why does the inner elements of the vector are copied when the vector is passed by value?



#include<vector>
using namespace std;

// this func won't modify v[2].
// Meaning v[2] (and the whole inner array) was copied
// when v is passed to the func?
void modify(vector<int> v)
v[2] = 100;


// this func modify v[2]
void modify(vector<int>& v)
v[2] = 100;


int main()
vector<int> v = 1,2,3,4,5,6;

// still same
modify(v);

// modified
modified2(v);



I find that it's strange that the actual content of the vector is copied when the vector is passed by value. I picture that the std::vector implementation must have a pointer field which maps to an address on heap, where the actual array is located. So when the vector is passed, even by value, the address should stay the same, pointing to the same content. Something like this:



#include<iostream>

using namespace std;

// a dummy wrapper of an array
// trying to mock vector<int>
class vector_int
public:
int* inner_array; // the actual array
vector_int(int *a)
inner_array = a;

int* at(int pos)
return inner_array+pos;

;

// this passes the "mocked vector" by value
// but 'inner_array' is not copied
void modify(vector_int v)
*(v.at(2)) = 10;


int main()
int* a = new int[3] 1,2,3;
vector_int v = vector_int(a);
modify(v); // v[2] is modified



Is this assumption about the std::vector implementation correct? What makes the vector content being copied when passed by value?




EDIT



Thanks to alter igel's answer and UnholySheep's comment, I figured out the reason why std::vector has value sementics (or why the inner array got copied).



If the copy constructor class is defined explicitly in the class definition, the copy constructor will determine how the struct/class instance is copied when the variable is passed in a function call. So I can define a copy constructor to my vector_int, in which I copy the whole inner_array, like



#include<iostream>

using namespace std;

class vector_int
public:
int* inner_array;
int len;
vector_int(int *a, int len)
inner_array = a;
this->len = len;

int* at(int pos)
return inner_array+pos;

// this is the copy constructor
vector_int(const vector_int &v2)
inner_array = new int;
for (int i =0; i < v2.len; i++)
*(inner_array+i) = *(v2.inner_array+i);


;

// Yay, the vector_int's inner_array is copied
// when this function is called
// and no modification of the original vector is done
void modify(vector_int v)
*(v.at(2)) = 10;


int main()
int* a = new int[3] 1,2,3;
vector_int v = vector_int(a,3);
//
modify(v);



I checked the source code of the stdlib implementation on my local computer (g++ Apple LLVM version 10.0.0). The std::vector defines a copy constructor which looks like this



template <class _Tp, class _Allocator>
vector<_Tp, _Allocator>::vector(const vector& __x)
: __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))

size_type __n = __x.size();
if (__n > 0)

allocate(__n);
__construct_at_end(__x.__begin_, __x.__end_, __n);




which looks like it does an malloc for the actual copied array + copy the array.







c++ arrays vector






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 18:18







phanhuy152

















asked Mar 22 at 17:25









phanhuy152phanhuy152

600610




600610







  • 4





    The copy constructor makes sure it is copied. Also your implementation forgets one crucial detail: the destructor (and the rule of 3/5)

    – UnholySheep
    Mar 22 at 17:28







  • 1





    All C++ standard containers act like value types.

    – melpomene
    Mar 22 at 17:29











  • To define a function signature guaranteeing not to copy a std::vector<T> and not copy T's in turn use a const reference: void foo(const std::vector<T>& v);

    – πάντα ῥεῖ
    Mar 22 at 17:31











  • Value semantics dictates that when you copy a value, the copy is independent of the original. For std::vector to have value semantics, copying it implies copying it's elements. There exists container implementations which perform copy-on-write but this is not practical for std::vector. And this approach loses a lot of it's benefits since the introduction of move semantics. If you are concerned about the cost of copying your vector then you likely don't mean to copy it and should read about reference types and move semantics instead.

    – François Andrieux
    Mar 22 at 17:32












  • looks like that previously you were using langues where pass by reference is common default langue behavior (like Java or C#). In C structs and in C++ classes, when passed by value copy is made. There are some spatial cases where classes implement pattern COW to save memory. There are different approaches. Note that in functional programing you can't modify anything. To create modification you need to create copy with some variation. I'm just saying this is just langue design and there are different approaches, each have pros and cons.

    – Marek R
    Mar 22 at 17:33













  • 4





    The copy constructor makes sure it is copied. Also your implementation forgets one crucial detail: the destructor (and the rule of 3/5)

    – UnholySheep
    Mar 22 at 17:28







  • 1





    All C++ standard containers act like value types.

    – melpomene
    Mar 22 at 17:29











  • To define a function signature guaranteeing not to copy a std::vector<T> and not copy T's in turn use a const reference: void foo(const std::vector<T>& v);

    – πάντα ῥεῖ
    Mar 22 at 17:31











  • Value semantics dictates that when you copy a value, the copy is independent of the original. For std::vector to have value semantics, copying it implies copying it's elements. There exists container implementations which perform copy-on-write but this is not practical for std::vector. And this approach loses a lot of it's benefits since the introduction of move semantics. If you are concerned about the cost of copying your vector then you likely don't mean to copy it and should read about reference types and move semantics instead.

    – François Andrieux
    Mar 22 at 17:32












  • looks like that previously you were using langues where pass by reference is common default langue behavior (like Java or C#). In C structs and in C++ classes, when passed by value copy is made. There are some spatial cases where classes implement pattern COW to save memory. There are different approaches. Note that in functional programing you can't modify anything. To create modification you need to create copy with some variation. I'm just saying this is just langue design and there are different approaches, each have pros and cons.

    – Marek R
    Mar 22 at 17:33








4




4





The copy constructor makes sure it is copied. Also your implementation forgets one crucial detail: the destructor (and the rule of 3/5)

– UnholySheep
Mar 22 at 17:28






The copy constructor makes sure it is copied. Also your implementation forgets one crucial detail: the destructor (and the rule of 3/5)

– UnholySheep
Mar 22 at 17:28





1




1





All C++ standard containers act like value types.

– melpomene
Mar 22 at 17:29





All C++ standard containers act like value types.

– melpomene
Mar 22 at 17:29













To define a function signature guaranteeing not to copy a std::vector<T> and not copy T's in turn use a const reference: void foo(const std::vector<T>& v);

– πάντα ῥεῖ
Mar 22 at 17:31





To define a function signature guaranteeing not to copy a std::vector<T> and not copy T's in turn use a const reference: void foo(const std::vector<T>& v);

– πάντα ῥεῖ
Mar 22 at 17:31













Value semantics dictates that when you copy a value, the copy is independent of the original. For std::vector to have value semantics, copying it implies copying it's elements. There exists container implementations which perform copy-on-write but this is not practical for std::vector. And this approach loses a lot of it's benefits since the introduction of move semantics. If you are concerned about the cost of copying your vector then you likely don't mean to copy it and should read about reference types and move semantics instead.

– François Andrieux
Mar 22 at 17:32






Value semantics dictates that when you copy a value, the copy is independent of the original. For std::vector to have value semantics, copying it implies copying it's elements. There exists container implementations which perform copy-on-write but this is not practical for std::vector. And this approach loses a lot of it's benefits since the introduction of move semantics. If you are concerned about the cost of copying your vector then you likely don't mean to copy it and should read about reference types and move semantics instead.

– François Andrieux
Mar 22 at 17:32














looks like that previously you were using langues where pass by reference is common default langue behavior (like Java or C#). In C structs and in C++ classes, when passed by value copy is made. There are some spatial cases where classes implement pattern COW to save memory. There are different approaches. Note that in functional programing you can't modify anything. To create modification you need to create copy with some variation. I'm just saying this is just langue design and there are different approaches, each have pros and cons.

– Marek R
Mar 22 at 17:33






looks like that previously you were using langues where pass by reference is common default langue behavior (like Java or C#). In C structs and in C++ classes, when passed by value copy is made. There are some spatial cases where classes implement pattern COW to save memory. There are different approaches. Note that in functional programing you can't modify anything. To create modification you need to create copy with some variation. I'm just saying this is just langue design and there are different approaches, each have pros and cons.

– Marek R
Mar 22 at 17:33













2 Answers
2






active

oldest

votes


















3














C++ allows class types to provide their own code for what it means to be created, copied, moved, and destroyed, and that code is called implicitly, without any obvious function calls. This is called value semantics and it's what C++ uses where other languages resort to things like create_foo(foo), foo.clone(), destroy_foo(foo) or foo.dispose().



Each class can define the following special member functions:



  • Constructors, for putting the object into a valid initial state

  • A Destructor, for cleaning up responsibly

  • A Copy Constructor, for creating a new object that is a duplicate of another

  • A Move Constructor, for creating a new object by transferring the data of another

  • A Copy Assignment Operator, for duplicating an object into an existing object

  • A Move Assignment Operator, for transferring data between two existing objects

These are all functions that you can define to do whatever you want. But they are called implicitly, meaning that users of such a class don't see these function calls in their code, and they expect them to do predictable things. You should make sure that your classes behave predictably by following the Rule of Three/Five/Zero.



There are of course other tools for sharing data, like pass-by-reference, which you already know about.



Lots of classes in the standard library use these special member functions to implement special behaviors that are very useful and help users write safe, correct code. For example:




  • std::vector when copied, will always have identical elements, though the underlying array and objects contained will be separate.


  • std::unique_ptr wraps a resource that has only one owner. To enforce this, it can't be copied.


  • std::shared_ptr wraps a resource that has many owners. It's not totally clear when to clean up such a resource, so copying a shared_ptr performs automatic reference counting, and the resource is cleaned up only when the last owner is done with it.





share|improve this answer
































    1














    This is because vector has value semantics: when you copy it you get a true copy of all the elements.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304874%2fwhy-c-vector-inner-elements-array-are-copied-when-passed-by-value%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      C++ allows class types to provide their own code for what it means to be created, copied, moved, and destroyed, and that code is called implicitly, without any obvious function calls. This is called value semantics and it's what C++ uses where other languages resort to things like create_foo(foo), foo.clone(), destroy_foo(foo) or foo.dispose().



      Each class can define the following special member functions:



      • Constructors, for putting the object into a valid initial state

      • A Destructor, for cleaning up responsibly

      • A Copy Constructor, for creating a new object that is a duplicate of another

      • A Move Constructor, for creating a new object by transferring the data of another

      • A Copy Assignment Operator, for duplicating an object into an existing object

      • A Move Assignment Operator, for transferring data between two existing objects

      These are all functions that you can define to do whatever you want. But they are called implicitly, meaning that users of such a class don't see these function calls in their code, and they expect them to do predictable things. You should make sure that your classes behave predictably by following the Rule of Three/Five/Zero.



      There are of course other tools for sharing data, like pass-by-reference, which you already know about.



      Lots of classes in the standard library use these special member functions to implement special behaviors that are very useful and help users write safe, correct code. For example:




      • std::vector when copied, will always have identical elements, though the underlying array and objects contained will be separate.


      • std::unique_ptr wraps a resource that has only one owner. To enforce this, it can't be copied.


      • std::shared_ptr wraps a resource that has many owners. It's not totally clear when to clean up such a resource, so copying a shared_ptr performs automatic reference counting, and the resource is cleaned up only when the last owner is done with it.





      share|improve this answer





























        3














        C++ allows class types to provide their own code for what it means to be created, copied, moved, and destroyed, and that code is called implicitly, without any obvious function calls. This is called value semantics and it's what C++ uses where other languages resort to things like create_foo(foo), foo.clone(), destroy_foo(foo) or foo.dispose().



        Each class can define the following special member functions:



        • Constructors, for putting the object into a valid initial state

        • A Destructor, for cleaning up responsibly

        • A Copy Constructor, for creating a new object that is a duplicate of another

        • A Move Constructor, for creating a new object by transferring the data of another

        • A Copy Assignment Operator, for duplicating an object into an existing object

        • A Move Assignment Operator, for transferring data between two existing objects

        These are all functions that you can define to do whatever you want. But they are called implicitly, meaning that users of such a class don't see these function calls in their code, and they expect them to do predictable things. You should make sure that your classes behave predictably by following the Rule of Three/Five/Zero.



        There are of course other tools for sharing data, like pass-by-reference, which you already know about.



        Lots of classes in the standard library use these special member functions to implement special behaviors that are very useful and help users write safe, correct code. For example:




        • std::vector when copied, will always have identical elements, though the underlying array and objects contained will be separate.


        • std::unique_ptr wraps a resource that has only one owner. To enforce this, it can't be copied.


        • std::shared_ptr wraps a resource that has many owners. It's not totally clear when to clean up such a resource, so copying a shared_ptr performs automatic reference counting, and the resource is cleaned up only when the last owner is done with it.





        share|improve this answer



























          3












          3








          3







          C++ allows class types to provide their own code for what it means to be created, copied, moved, and destroyed, and that code is called implicitly, without any obvious function calls. This is called value semantics and it's what C++ uses where other languages resort to things like create_foo(foo), foo.clone(), destroy_foo(foo) or foo.dispose().



          Each class can define the following special member functions:



          • Constructors, for putting the object into a valid initial state

          • A Destructor, for cleaning up responsibly

          • A Copy Constructor, for creating a new object that is a duplicate of another

          • A Move Constructor, for creating a new object by transferring the data of another

          • A Copy Assignment Operator, for duplicating an object into an existing object

          • A Move Assignment Operator, for transferring data between two existing objects

          These are all functions that you can define to do whatever you want. But they are called implicitly, meaning that users of such a class don't see these function calls in their code, and they expect them to do predictable things. You should make sure that your classes behave predictably by following the Rule of Three/Five/Zero.



          There are of course other tools for sharing data, like pass-by-reference, which you already know about.



          Lots of classes in the standard library use these special member functions to implement special behaviors that are very useful and help users write safe, correct code. For example:




          • std::vector when copied, will always have identical elements, though the underlying array and objects contained will be separate.


          • std::unique_ptr wraps a resource that has only one owner. To enforce this, it can't be copied.


          • std::shared_ptr wraps a resource that has many owners. It's not totally clear when to clean up such a resource, so copying a shared_ptr performs automatic reference counting, and the resource is cleaned up only when the last owner is done with it.





          share|improve this answer















          C++ allows class types to provide their own code for what it means to be created, copied, moved, and destroyed, and that code is called implicitly, without any obvious function calls. This is called value semantics and it's what C++ uses where other languages resort to things like create_foo(foo), foo.clone(), destroy_foo(foo) or foo.dispose().



          Each class can define the following special member functions:



          • Constructors, for putting the object into a valid initial state

          • A Destructor, for cleaning up responsibly

          • A Copy Constructor, for creating a new object that is a duplicate of another

          • A Move Constructor, for creating a new object by transferring the data of another

          • A Copy Assignment Operator, for duplicating an object into an existing object

          • A Move Assignment Operator, for transferring data between two existing objects

          These are all functions that you can define to do whatever you want. But they are called implicitly, meaning that users of such a class don't see these function calls in their code, and they expect them to do predictable things. You should make sure that your classes behave predictably by following the Rule of Three/Five/Zero.



          There are of course other tools for sharing data, like pass-by-reference, which you already know about.



          Lots of classes in the standard library use these special member functions to implement special behaviors that are very useful and help users write safe, correct code. For example:




          • std::vector when copied, will always have identical elements, though the underlying array and objects contained will be separate.


          • std::unique_ptr wraps a resource that has only one owner. To enforce this, it can't be copied.


          • std::shared_ptr wraps a resource that has many owners. It's not totally clear when to clean up such a resource, so copying a shared_ptr performs automatic reference counting, and the resource is cleaned up only when the last owner is done with it.






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 22 at 18:05

























          answered Mar 22 at 17:59









          alter igelalter igel

          3,66811331




          3,66811331























              1














              This is because vector has value semantics: when you copy it you get a true copy of all the elements.






              share|improve this answer



























                1














                This is because vector has value semantics: when you copy it you get a true copy of all the elements.






                share|improve this answer

























                  1












                  1








                  1







                  This is because vector has value semantics: when you copy it you get a true copy of all the elements.






                  share|improve this answer













                  This is because vector has value semantics: when you copy it you get a true copy of all the elements.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 22 at 17:29









                  Maxim EgorushkinMaxim Egorushkin

                  91.3k11105196




                  91.3k11105196



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304874%2fwhy-c-vector-inner-elements-array-are-copied-when-passed-by-value%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript