Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ vector passed by value: did I get it right?

Let's say I have a struct like this:

struct typeA
{
    long first;
    string second
    double third;
};

If I declare

typeA myArray[100];

Then myArray is stored in the stack, consuming sizeof(typeA)*100 bytes of garbage data (until I store some actual data, at least).

Whenever I pass this array as a parameter, I'll always be passing a pointer to the first of the first element in the stack. So the pointer goes from stack to stack.

But if I declare

vector<int> myVector (4, 100);

Then the myVector object is actually stored in the stack, and it contains a pointer to the first element of an array of 4*sizeof(int) bytes stored in the heap, where the actual data is stored. So pointer goes from stack to heap.

Whenever I pass this vector as a parameter, if I add it to the parameter list like this:

vector<int> parameterVector

the function gets a copy of the myVector object and stores it in the stack.

But if I do it like this:

vector<int> &parameterVector

the function gets a reference to myVector stored in the stack, so I now have a variable stored in the stack, referencing a myVector object also stored in the stack, that contains a pointer to an array of actual elements stored in the heap.

Is this correct?

I have a few doubts here:

  1. Do the actual elements get stored in a static array (the ones inherited from C, indicated with square brackets) in the heap?
  2. Does the myVector object have just one pointer to the first element, or it has multiple pointers to each one of the elements?
  3. So passing a vector by value doesn't pose much of a problem, since the only thing that gets copied is the vector object, but not the actual elements. Is that so?
  4. If I got the whole thing wrong and the actual elements are copied as well when passing a vector parameter by value, then why does C++ allow this, considering it discourages it with static arrays? (as far as I know, static arrays always get passed as a reference to the first element).

Thanks!

like image 374
Floella Avatar asked Oct 18 '22 22:10

Floella


1 Answers

Do the actual elements get stored in a static array (the ones inherited from C, indicated with square brackets) in the heap?

Typically the elements of the vector are stored in the free store using a dynamic array like

some_type* some_name = new some_type[some_size]

Does the myVector object have just one pointer to the first element, or it has multiple pointers to each one of the elements?

Typically a vector will have a pointer to the first element, a size variable and a capacity. It could have more but these are implementation details and are not defined by the standard.

So passing a vector by value doesn't pose much of a problem, since the only thing that gets copied is the vector object, but not the actual elements. Is that so?

No. copying the vector is an O(N) operation as it has to copy each element of the vector. If it did not then you would have two vectors using the same underlying array and if one gets destroyed then it would delete the array out from under the other one.

like image 195
NathanOliver Avatar answered Nov 12 '22 21:11

NathanOliver