清除已经为空的向量会导致未定义的行为吗?

发布于 2024-12-06 17:32:29 字数 3358 浏览 1 评论 0原文

如果我清除之前已清除的向量会发生什么? 我在 Visual Studio 中尝试了一下,没有导致任何运行时错误。但我正在寻找异常的可能原因(如下)并想知道这是否可能是原因?

*** glibc detected *** process.out: double free or corruption (out): 0x01a0b588 *** 

更新代码:

bool myclass::Sort(SortType enSortOption)
{

  bool bRet = TRUE;


  m_oVecOfIntsOrig.clear();
  m_oVecOfIntsSorted.clear();

  for(int i = 0;i<m_oList.listsize();i++)
  {
    m_oVecOfIntsOrig.push_back(i);
  }
  m_oVecOfIntsSorted = m_oVecOfIntsOrig; 
  //Just sort the indices     
  switch(enSortOption)
  {
  case Alpha:
    { 
      t_SortStructAlpha sSortAlpha(this);
      sort( m_oVecOfIntsSorted.begin(), m_oVecOfIntsSorted.end(), sSortAlpha );        
    }
    break;
  case Dist:
    {        
      t_SortStructDist sSortDist(this);
      sort( m_oVecOfIntsSorted.begin(), m_oVecOfIntsSorted.end(), sSortDist );
    }
    break;
  case none:  
    {

      //Nothing to do
      return TRUE;
    }
    break;  
  default:
    {
      bRet = FALSE;

    }
    break;
  }

  //Update the list based on sorted index vector
  ListElem oSortedListElements;
  //this clear function simply clears all the 7 constituent vectors. 
  oSortedListElements.vClear();
  if(TRUE == bGetSortedList(oSortedListElements))
  {
    //If successfully copied to temp object, then update member variable
    //m_oList is what I'm intending to display
    m_oList.vClear();
    m_oList = oSortedListElements;
  }
  else
  {
    //copy failed
    bRet = FALSE;

  }
  return bRet;
}


bool myclass::bGetSortedList(ListElem& oSortedListElements)
{

  bool bRet = TRUE;

  //Creating object from beginning, so clear old contents if any
  oSortedListElements.vClear();
  /* Sort done. Now update the list based on new indices*/
  for(int u16Index = 0; u16Index<m_oVecOfIntsSorted.size(); u16Index++)
  {
    if(
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec1.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec2.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec3.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec4.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec5.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec6.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec7.size())
      )
    {
      //out of bounds check
      return FALSE;
    }
    //m_oListConstituents contains overall list
    oSortedListElements.oVec1.push_back(m_oListConstituents.oVec1.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec2.push_back(m_oListConstituents.oVec2.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec3.push_back(m_oListConstituents.oVec3.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec4.push_back(m_oListConstituents.oVec4.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec5.push_back(m_oListConstituents.oVec5.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec6.push_back(m_oListConstituents.oVec6.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec7.push_back(m_oListConstituents.oVec7.at(m_oVecOfIntsSorted.at(u16Index)));     

  }

  return bRet;

}

What happens if I clear a vector which was already cleared before?
I tried it out in Visual Studio and it did not lead to any run-time errors. But I'm hunting for a possible cause to an exception (below) And wanted to know if this could be the reason?

*** glibc detected *** process.out: double free or corruption (out): 0x01a0b588 *** 

Updating code:

bool myclass::Sort(SortType enSortOption)
{

  bool bRet = TRUE;


  m_oVecOfIntsOrig.clear();
  m_oVecOfIntsSorted.clear();

  for(int i = 0;i<m_oList.listsize();i++)
  {
    m_oVecOfIntsOrig.push_back(i);
  }
  m_oVecOfIntsSorted = m_oVecOfIntsOrig; 
  //Just sort the indices     
  switch(enSortOption)
  {
  case Alpha:
    { 
      t_SortStructAlpha sSortAlpha(this);
      sort( m_oVecOfIntsSorted.begin(), m_oVecOfIntsSorted.end(), sSortAlpha );        
    }
    break;
  case Dist:
    {        
      t_SortStructDist sSortDist(this);
      sort( m_oVecOfIntsSorted.begin(), m_oVecOfIntsSorted.end(), sSortDist );
    }
    break;
  case none:  
    {

      //Nothing to do
      return TRUE;
    }
    break;  
  default:
    {
      bRet = FALSE;

    }
    break;
  }

  //Update the list based on sorted index vector
  ListElem oSortedListElements;
  //this clear function simply clears all the 7 constituent vectors. 
  oSortedListElements.vClear();
  if(TRUE == bGetSortedList(oSortedListElements))
  {
    //If successfully copied to temp object, then update member variable
    //m_oList is what I'm intending to display
    m_oList.vClear();
    m_oList = oSortedListElements;
  }
  else
  {
    //copy failed
    bRet = FALSE;

  }
  return bRet;
}


bool myclass::bGetSortedList(ListElem& oSortedListElements)
{

  bool bRet = TRUE;

  //Creating object from beginning, so clear old contents if any
  oSortedListElements.vClear();
  /* Sort done. Now update the list based on new indices*/
  for(int u16Index = 0; u16Index<m_oVecOfIntsSorted.size(); u16Index++)
  {
    if(
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec1.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec2.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec3.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec4.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec5.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec6.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec7.size())
      )
    {
      //out of bounds check
      return FALSE;
    }
    //m_oListConstituents contains overall list
    oSortedListElements.oVec1.push_back(m_oListConstituents.oVec1.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec2.push_back(m_oListConstituents.oVec2.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec3.push_back(m_oListConstituents.oVec3.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec4.push_back(m_oListConstituents.oVec4.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec5.push_back(m_oListConstituents.oVec5.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec6.push_back(m_oListConstituents.oVec6.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec7.push_back(m_oListConstituents.oVec7.at(m_oVecOfIntsSorted.at(u16Index)));     

  }

  return bRet;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放赐 2024-12-13 17:32:29

如果您所做的等于

v.clear();
v.clear();

“否”,则不存在 UB:v.clear() 的后置条件之一是 v.size() == 0,因此第二次调用 clear 应该是无操作。

If what you did amounts to

v.clear();
v.clear();

then no, there's no UB: one of the postconditions of v.clear() is that v.size() == 0, so the second call to clear should be a no-op.

旧时光的容颜 2024-12-13 17:32:29

连续调用两次 clear 方法应该没问题。您的错误看起来像是您正在释放已经释放的东西。发生这种情况的原因是您对同一个对象调用了两次 free ,或者可能将一个对象放入向量中,然后该对象被删除。在向量上调用 clear 将会导致该错误(这是一个问题,因为 clear 对向量中包含的任何对象调用析构函数)。

编辑:

at 返回一个引用,因此删除设置为从 at 返回的值的内容可能会导致问题。

Calling the clear method twice in a row should be fine. Your error looks like you are freeing something which was already freed. This can happen because you called free twice on the same object or possibly put an object in your vector which was then deleted. Calling clear on your vector would then cause that error (this is a problem because clear calls the destructor on any objects contained in the vector).

Edit:

at returns a reference so deleting something set to a value returned from an at might cause problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文