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? the 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<vecto开发者_JAVA百科r<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;
}
Another problem :
while(depth > 1)
{
netscore += curr.evaluateMove(1,0,depth,b);
....
--depth;
}
causes endless recursion when depth >1
quick read: Braid curr(*this);
causes a copy. Can't you use a pointer to the Braid?
Am I the only remarking a Stack Overflow there ?
I colored out the useless bits (for the purpose of the demo)
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;
}
Now if depth
is superior to 1
... oups
Let's say I do:
Braid b; b.evaluateMove(1,0,2,b);
- it invokes
curr.evaluateMove(1,0,2,b);
- which invokes
curr.evaluateMove(1,0,2,b);
- which ...
Well, the system might run out of memory.
Note: why does evaluateMove
both copy this
and ask for a copy of Braid
(parameter b
) ? I'd check my score
method if I was you.
On an unrelated point, you probably do not want:
vector<bool>
as vector<bool>
is a template specialisation which you should only use if you really, really know what you are doing. Consider vector <char>
or deque<bool>
instead.
精彩评论