有向量且有成员的类是否存在内存问题 我刚刚开始C++,如果这是一个愚蠢的问题,那么抱歉。我有一个类Braid,它的成员是向量。我还没有写一个赋值运算符。当我对Braid类型的对象执行大量赋值时,我会遇到内存问题:- 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=...) /lib/libc.so.6中的_int_malloc()中的0 0xb7daff89 #/lib/libc.so.6中的malloc()中的1 0xb7db2583 #来自/usr/libstdc++.so.6的运算符new(unsigned int)()中的2 0xb7f8ac59 #3 0x0804d05e在_gnu_cxx::new_分配器::allocate中(this=0xbf800204,_n=1) 在/usr/lib/gcc/i686 pc-linux-gnu/4.4.3/../../../../../../../../include/c++/4.4.3/ext/new_-allocator.h:89 #标准中的4 0x0804cb0e::_Vector_base::_M_allocate(this=0xbf800204,u 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英寸向量基(此=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(这=0xbf800204,uux=…) at/usr/lib/gcc/i686 pc-linux-gnu/4.4.3/../../../../../../../include/c++/4.4.3/bits/stl_-vector.h:242 #编织带处编织带中的7 0x0804b234(该值=0xbf800204)。h:13 #8 0x080495ed编织:编织处的cycleBraid(此=0xbf8001b4)。cpp:191 #编织中的9 0x080497c6::编织处的分数(此=0xbf800298,b=…)。cpp:251 #编织中的10 0x08049c46::evaluateMove(此=0xbf800468,移动=1,位置=0,深度=2,b=…)

有向量且有成员的类是否存在内存问题 我刚刚开始C++,如果这是一个愚蠢的问题,那么抱歉。我有一个类Braid,它的成员是向量。我还没有写一个赋值运算符。当我对Braid类型的对象执行大量赋值时,我会遇到内存问题:- 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=...) /lib/libc.so.6中的_int_malloc()中的0 0xb7daff89 #/lib/libc.so.6中的malloc()中的1 0xb7db2583 #来自/usr/libstdc++.so.6的运算符new(unsigned int)()中的2 0xb7f8ac59 #3 0x0804d05e在_gnu_cxx::new_分配器::allocate中(this=0xbf800204,_n=1) 在/usr/lib/gcc/i686 pc-linux-gnu/4.4.3/../../../../../../../../include/c++/4.4.3/ext/new_-allocator.h:89 #标准中的4 0x0804cb0e::_Vector_base::_M_allocate(this=0xbf800204,u 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英寸向量基(此=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(这=0xbf800204,uux=…) at/usr/lib/gcc/i686 pc-linux-gnu/4.4.3/../../../../../../../include/c++/4.4.3/bits/stl_-vector.h:242 #编织带处编织带中的7 0x0804b234(该值=0xbf800204)。h:13 #8 0x080495ed编织:编织处的cycleBraid(此=0xbf8001b4)。cpp:191 #编织中的9 0x080497c6::编织处的分数(此=0xbf800298,b=…)。cpp:251 #编织中的10 0x08049c46::evaluateMove(此=0xbf800468,移动=1,位置=0,深度=2,b=…),c++,memory-management,vector,C++,Memory Management,Vector,我怀疑这些内存问题是因为向量正在调整大小。我想知道的是Braid类型的对象是否会在其成员展开时自动展开?我正在写的代码非常长,所以我将发布导致问题的部分。以下是守则的相关章节:- class Braid { private : vector<int> braid; //Stores the braid. int strands; vector < vector<bool> > history; vector < vector<

我怀疑这些内存问题是因为向量正在调整大小。我想知道的是Braid类型的对象是否会在其成员展开时自动展开?我正在写的代码非常长,所以我将发布导致问题的部分。以下是守则的相关章节:-

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;
               .....
               .....
}
类编织
{
私人:
矢量编织;//存储编织。
内特股;
向量<向量>历史;
向量CM;
公众:
辫子():股线(0){}
编织操作员*(编织);
编织*输入编织(整数,矢量);
int printbaid();
int打印编织(矢量);
向量::size_type size()常量;
.....
.....
}
以下是导致问题的函数:-

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;
}
int Braid::evaluateMove(int move、int pos、int depth、Braid b)
{
int netscore=0;
编织货币(*本);
当前=当前移动(移动,位置);
netscore+=当前分数(b);
而(深度>1)
{
netscore+=当前评估移动(1,0,深度,b);
netscore+=当前评估移动(2,0,深度,b);
对于(int i=0;i
快速阅读:
编织货币(*此)导致复制。你不能用指针指向辫子吗?

在一个不相关的点上,你可能不想要:

 vector<bool>
向量
正如
vector
是一种模板专业化,只有当您真正了解自己在做什么时,才应该使用它。考虑<代码>向量<代码>或<代码> DeQue>代码> .< /P> < P>另一个问题:

while(depth > 1)
{
  netscore += curr.evaluateMove(1,0,depth,b);
  ....
  --depth;
}

当深度>1时,会导致无休止的递归。是否只有我在那里标记堆栈溢出

我把无用的部分涂上颜色(为了演示的目的)

int Braid::evaluateMove(int move、int pos、int depth、Braid b)
{
#int netscore=0;
#编织货币(*本);
#当前=当前移动(移动,位置);
#netscore+=当前分数(b);
而(深度>1)
{
netscore+=当前评估移动(1,0,深度,b);
#netscore+=当前评估移动(2,0,深度,b);
#对于(int i=0;i
现在如果
深度
优于
1
。。。分组

让我们假设我这样做:

  • 编织b;b、 评价运动(1,0,2,b)
  • 它调用当前evaluateMove(1,0,2,b)
  • 调用当前evaluateMove(1,0,2,b)的代码
  • 哪个
嗯,系统可能会耗尽内存


注意:为什么
evaluateMove
两者都复制
并要求复制
辫子(参数
b
)?如果我是你,我会检查我的
分数
方法。

Braid*输入Braid(int,vector)
-可能您希望传递一个
常量向量&
,以避免复制整个向量。什么内存问题?内存使用率过高?从外观上看,您应该熟悉参考资料和
const
关键字。@KennyTM:-谢谢您的提示@我说过我是一个完全的新手。我有点陷入了一个令人不快的境地,不得不在很短的时间内编写C++代码,而没有正确地学习语言。因此,肯定会有很多新手犯错误。无论如何,我没有运行的内存,但我的程序崩溃与分段故障。从gdb回溯来看,这看起来像是内存分配错误,但我可能完全错了。函数cycleBraid modify是辫子,所以我做了一个临时副本。@Billy:为你做的@用户:
curr=curr.move(移动,pos)
看起来像是过度杀戮,彻底吹走了
辫子。这段代码看起来需要一些引用、指针和重构,但我不知道Braid试图实现什么。这是某种神秘的博弈论吗?@Mike我正在尝试实现编织群,我想你的意思是“考虑一下上面的
vector/vector
或其他东西,
deque
”。。。“我说的对吗?”比利,不,我是认真的
deque
不是一个专业,它几乎具有
vector
应该具备的所有功能
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;
}