Do classes which have a vector has a member have memory issues
- by user263766
I am just starting out C++, so sorry if this is a dumb question. I have a class Braid whose members are vectors. I have not written an assignment operator. When I do a lot of assignments to an object of the type Braid, I run into memory issues :-
0 0xb7daff89 in _int_malloc () from /lib/libc.so.6
#1 0xb7db2583 in malloc () from /lib/libc.so.6
#2 0xb7f8ac59 in operator new(unsigned int) () from /usr/lib/libstdc++.so.6
#3 0x0804d05e in __gnu_cxx::new_allocator<int>::allocate (this=0xbf800204, __n=1)
at /usr/lib/gcc/i686-pc-linux-gnu/4.4.3/../../../../include/c++/4.4.3/ext/new_allocator.h:89
#4 0x0804cb0e in std::_Vector_base<int, std::allocator<int> >::_M_allocate (this=0xbf800204, __n=1)
at /usr/lib/gcc/i686-pc-linux-gnu/4.4.3/../../../../include/c++/4.4.3/bits/stl_vector.h:140
#5 0x0804c086 in _Vector_base (this=0xbf800204, __n=1, __a=...)
at /usr/lib/gcc/i686-pc-linux-gnu/4.4.3/../../../../include/c++/4.4.3/bits/stl_vector.h:113
#6 0x0804b4b7 in vector (this=0xbf800204, __x=...)
at /usr/lib/gcc/i686-pc-linux-gnu/4.4.3/../../../../include/c++/4.4.3/bits/stl_vector.h:242
#7 0x0804b234 in Braid (this=0xbf800204) at braid.h:13
#8 0x080495ed in Braid::cycleBraid (this=0xbf8001b4) at braid.cpp:191
#9 0x080497c6 in Braid::score (this=0xbf800298, b=...) at braid.cpp:251
#10 0x08049c46 in Braid::evaluateMove (this=0xbf800468, move=1, pos=0, depth=2, b=...)
I suspect that these memory issues are because the vectors are getting resized. What I want to know is whether objects of type Braid automatically expand when its members expand? he code I am writing is really long so I will post the section which is causing the problems. Here is the relevant section of the code :-
class Braid
{
private :
vector<int> braid; //Stores the braid.
int strands;
vector < vector<bool> > history;
vector < vector<bool> > CM;
public :
Braid () : strands(0) {}
Braid operator * (Braid);
Braid* inputBraid(int,vector<int>);
int printBraid();
int printBraid(vector<vector<int>::iterator>);
vector<int>::size_type size() const;
.....
.....
}
Here is the function which causes the issue :-
int Braid::evaluateMove(int move,int pos,int depth,Braid b)
{
int netscore = 0;
Braid curr(*this);
curr = curr.move(move,pos);
netscore += curr.score(b);
while(depth > 1)
{
netscore += curr.evaluateMove(1,0,depth,b);
netscore += curr.evaluateMove(2,0,depth,b);
for(int i = 0; i < braid.size();++i)
{
netscore += curr.evaluateMove(3,i,depth,b);
netscore += curr.evaluateMove(4,i,depth,b);
netscore += curr.evaluateMove(5,i,depth,b);
curr = curr.cycleBraid();
netscore += curr.evaluateMove(6,0,depth,b);
}
--depth;
}
return netscore;
}