首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有向量和成员的类有内存问题吗?

有向量和成员的类有内存问题吗?
EN

Stack Overflow用户
提问于 2010-04-07 22:56:40
回答 4查看 242关注 0票数 2

我刚刚开始使用C++,所以如果这是一个愚蠢的问题,很抱歉。我有一个Braid类,它的成员是向量。我还没有写赋值运算符。当我对类型为Braid的对象进行大量赋值时,我遇到了内存问题:

代码语言:javascript
复制
 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=...)

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

代码语言:javascript
复制
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;
               .....
               .....
}

以下是导致该问题的函数:-

代码语言:javascript
复制
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;
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-04-07 23:20:56

另一个问题:

代码语言:javascript
复制
while(depth > 1)
{
  netscore += curr.evaluateMove(1,0,depth,b);
  ....
  --depth;
}

当深度大于1时,导致无休止的递归

票数 3
EN

Stack Overflow用户

发布于 2010-04-07 23:00:50

快速阅读:Braid curr(*this);导致复制。你不能用指针指向编织带吗?

票数 3
EN

Stack Overflow用户

发布于 2010-04-07 23:21:23

我是唯一标记堆栈溢出的人吗?

我涂掉了无用的部分(出于演示的目的)

代码语言:javascript
复制
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;
}

现在如果depth优于1..。oups

假设我这样做:

  • Braid b; b.evaluateMove(1,0,2,b);
  • it调用curr.evaluateMove(1,0,2,b);
  • which调用curr.evaluateMove(1,0,2,b);
  • which ...

系统可能会耗尽内存。

注意:为什么evaluateMove既要复制this,又要Braid (参数b__)的副本?如果我是你,我会检查我的score方法。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2593471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档