push_back std::pair into std::vector error

push_back std::pair into std::vector error

本文关键字:std vector error into push back pair      更新时间:2023-10-16

当我尝试向量添加一对时,出现此错误:

table: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted

这是方法:

std::vector<std::pair <int, int> > AI::genPosNormal(int p, int dice)
{
  std::vector<std::pair <int, int> > pos;
  std::pair<int, int> step;
  for(int i = p; i < 25-dice; i++){
    if(board[BLACK][i] > 0 && board[WHITE][i+dice] < 2){
      step.first = i;
      step.second = dice;
      pos.push_back(step);
    }
  }
  return pos;
}

有点晚了。但我发现在 std::vector of std::p air 中使用 .reserve() 可以解决这个问题。我仍然需要弄清楚为什么会导致错误,但似乎在引发错误标志的 stl_uninitialized.h 中。