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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include <iostream>
#include <vector>

using namespace std;


void reverseVec(vector<int>* numbs){

    vector<int>* newNumbs = new vector<int>(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<int>* n = new vector<int>;
    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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40


#include <iostream>
#include <vector>

using namespace std;


vector<int>* reverseVec(vector<int>* numbs){

    vector<int>* newNumbs = new vector<int>(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<int>* n = new vector<int>;
    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:
1
2
3
4
5
6
7
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:
1
2
3
4
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> 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

1
2
3
4
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:
1
2
3
4
5
void reverseVec( std::vector<int>& numbs )
{
  std::vector<int> newNumbs( numbs.rbegin(), numbs.rend() );
  numbs.swap( newNumbs );
}
Registered users can post here. Sign in or register to post.