开发者社区> 问答> 正文

C++ vector执行push_back时出现段错误

代码:

// 结构
struct SControlCards
{
	std::vector<short> vecCards;
	int iCount;
};

// 执行代码块
{
	SControlCards Scontrol;
	std::vector<SControlCards> vecControl; // 控牌结构体
	vecControl.clear();

	// 存进结构体
	int index = 0;
	for (int i = 0; i < m_DealCardCount * m_iPlayingCount; i++)
	{
		vecCards.push_back(m_cards[i]);
		index++;
		if (index % 12 == 0)
		{
			Scontrol.vecCards = vecCards;
			Scontrol.iCount = m_pMakeCard->GetControlGroupsCount(vecCards);
			vecControl.push_back(Scontrol); // 出现core
			vecCards.clear();
		}
	}
}

coredump信息:

#0  0x000000000059135e in std::_Vector_base<short, std::allocator<short> >::_Vector_impl::_Vector_impl(std::allocator<short>&&) (this=0x20,
    __a=<unknown type in xxx, CU 0x0, DIE 0x36710>) at /usr/include/c++/4.8.2/bits/stl_vector.h:97
#1  0x000000000058f9c1 in std::_Vector_base<short, std::allocator<short> >::_Vector_base(std::_Vector_base<short, std::allocator<short> >&&) (this=0x20,
    __x=<unknown type in xxx, CU 0x0, DIE 0x345ae>) at /usr/include/c++/4.8.2/bits/stl_vector.h:143
#2  0x000000000058cce4 in std::vector<short, std::allocator<short> >::vector(std::vector<short, std::allocator<short> >&&) (this=0x20,
    __x=<unknown type in xxx, CU 0x0, DIE 0x3117a>) at /usr/include/c++/4.8.2/bits/stl_vector.h:328
#3  0x00000000005e62bd in SControlCards::SControlCards(SControlCards&&) (this=0x20) at Game/def.h:389
#4  0x00000000005ece58 in std::_Construct<SControlCards<SControlCards> > (__p=0x20) at /usr/include/c++/4.8.2/bits/stl_construct.h:75
#5  0x00000000005ec10d in std::__uninitialized_copy<false>::__uninit_copy<std::move_iterator<SControlCards*>, SControlCards*> (__first=..., __last=...,
    __result=0x0) at /usr/include/c++/4.8.2/bits/stl_uninitialized.h:75
#6  0x00000000005eb3d2 in std::uninitialized_copy<std::move_iterator<SControlCards*>, SControlCards*> (__first=..., __last=..., __result=0x0)
    at /usr/include/c++/4.8.2/bits/stl_uninitialized.h:117
#7  0x00000000005e91bc in std::__uninitialized_copy_a<std::move_iterator<SControlCards*>, SControlCards*, SControlCards> (__first=..., __last=...,
    __result=0x0) at /usr/include/c++/4.8.2/bits/stl_uninitialized.h:258
#8  0x00000000005e60a5 in std::__uninitialized_move_if_noexcept_a<SControlCards*, SControlCards*, std::allocator<SControlCards> > (
    __first=0x7f1fc226c010, __last=0x7f1fe226c010, __result=0x0, __alloc=...) at /usr/include/c++/4.8.2/bits/stl_uninitialized.h:281
#9  0x00000000005e3238 in std::vector<SControlCards, std::allocator<SControlCards> >::_M_emplace_back_aux<SControlCards const&> (this=0x7ffe028a4a30)
    at /usr/include/c++/4.8.2/bits/vector.tcc:412
#10 0x00000000005e0eef in std::vector<SControlCards, std::allocator<SControlCards> >::push_back (this=0x7ffe028a4a30, __x=...)
    at /usr/include/c++/4.8.2/bits/stl_vector.h:911

展开
收起
31535191 2020-03-07 10:26:09 5410 0
2 条回答
写回答
取消 提交回答
  • 看上去vector在增长的过程中使用了移动构造函数而不是拷贝构造函数,有可能SControlCards这个类的移动构造函数实现有问题,需要检查一下。

    2020-03-23 17:18:23
    赞同 展开评论 打赏
  • 技术架构师 阿里云开发者社区技术专家博主 CSDN签约专栏技术博主 掘金签约技术博主 云安全联盟专家 众多开源代码库Commiter

    image.png 检查下类型问题,出现问题的应该是第一个push

    2020-03-12 15:54:30
    赞同 展开评论 打赏
问答分类:
C++
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载