### vectors size is exponentially big, but why? Hey guys,

my vector size seems to be strangely big, the revesreVev function takes in a pointer to a vector as it's arguments and sets that pointer equal to a new reversed vector

but when I return from the function and print the vector n's size it prints a huge number , it should print 4 as that is the size of the vector

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` `````` #include #include using namespace std; void reverseVec(vector* numbs){ vector* newNumbs = new vector(0); cout << "size" << newNumbs->size() << endl; // prints 0 as expected cout << numbs->size()-1 << endl; // prints 3 as expected for(int i = numbs->size()-1; i >= 0; i--){ cout << "adding" << endl; newNumbs->push_back(numbs->at(i)); } delete numbs; numbs = newNumbs; cout << "size ::" << numbs->size() << endl; // prints 4 as expected } int main() { vector* n = new vector; n->push_back(6); n->push_back(7); n->push_back(8); n->push_back(9); reverseVec(n); cout << n->size() << endl; // prints 4294961506, how did it get that big ?????? // for(int i = 0; i < n->size(); i++){ // // cout << n->at(i) << endl; // } } ``````

but when I return a pointer and set n = to the pointer returned by the reverseVec function it works fine as expected

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` `````` #include #include using namespace std; vector* reverseVec(vector* numbs){ vector* newNumbs = new vector(0); for(int i = numbs->size()-1; i >= 0; i--){ newNumbs->push_back(numbs->at(i)); } delete numbs; numbs = newNumbs; return newNumbs; } int main() { vector* n = new vector; n->push_back(6); n->push_back(7); n->push_back(8); n->push_back(9); n = reverseVec(n); for(int i = 0; i < n->size(); i++){ cout << n->at(i) << endl; } } ``````

any reasoning to why this is happening??

thanks
Last edited on You pass parameter by value. That is a copy.

Lets do this:
 ``1234567`` ``````T x; T* ptr = &x; { // imitate function call T* numbs = ptr; // imitate parameter by value numbs = nullptr; } // the ptr still points to x ``````

On top of that you have:
 ``1234`` ``````int* x = new int; int* y = x; delete y; // the x is now invalid pointer `````` oh ok so when I pass n to the reverseArray vec, a copy is created called nums?

so now both nums and n are pointing to the same data?

in the function I delete the data that is pointed to by both nums and n, and set numbs only to point to the new vector, but n does not point to this vector?

I have essentially deleted n's data and when the function goes out of scope newNumbs is now also out of scope and I have leaked memory?

are all of those assumptions correct? The short answer: pass numb by reference:
`void reverseVec(vector<int>* &numbs){`

Right now you're passing the pointer numb by value. So numb (in reverseVec) is a different variable than n (in main). However, they point to the same vector.

Line 20 in the first program deletes numb. Now numb and n point to unallocated memory.

Line 21 assigns numb to the new vector, but when reverseVec returns, that value is lost. n still points to the now-deleted vector and when you print the size at line 35, you get garbage. Yes.

Overall, why do you `new` a vector?
 ``12345678910111213`` ``````#include #include #include int main() { std::vector n {6, 7, 8, 9}; std::reverse( n.begin(), n.end() ); for ( int x : n ) { std::cout << x << '\n'; } }`````` that would have been a lot simpler, yeah there isn't much point of a having the vector allocated on anything other than the stack for this simple case. that is much more elegant, I was just doing an exercise in Bjarnes book to reverse the contents of a vector, I was trying to do it as many ways as I could think of

thanks guys :)
Last edited on > there isn't much point of a having the vector allocated on anything other
> than the stack for this simple case
¿can you think of any case were there is any point in doing `new vector<int>`? to be honest, I can't think of much situations

then again maybe when returning a vector from a function( which you delete after you're done with it) the reason I say this is because you could obviously return a copy of a vector but that copy is expensive(especially if the vector contains thousands of objects ) and if you return a pointer to a vector it will be less expensive processing wise.

what would be the other cases? > returning a vector from a function
move assignment

 ``1234`` ``````class vector{ T *data; int size, capacity; };``````
the containers are lightweight, they just have a couple pointers
`a = b` doing a deep-copy is expensive, but if `b' is going to die inmediately after, you can make it shallow (with some care) The std::reverse() works in-place; without additional memory.

You had a temporary vector into which you did copy values (like std::reverse_copy() does) and then attempted to swap the vectors. That could be shortened to:
 ``12345`` ``````void reverseVec( std::vector& numbs ) { std::vector newNumbs( numbs.rbegin(), numbs.rend() ); numbs.swap( newNumbs ); }``````