访问std :: list instruct struct诱导segfault
accessing std::list inside struct induce segfault
我当前找不到以下程序的分割故障的来源。
struct info
{
std::list<int> bfs;
int *level;
int *distance;
};
...
info* Graph::BFS(int s)
{
info *tmp = (info*) malloc(sizeof(struct info));
tmp->level = new int[V];
tmp->distance = new int[V];
...
tmp->bfs.push_back(s); !! <- causes a segmentation fault
tmp->level[s] = 0; <- seems to work ok
int current;
std::list<int>::iterator i;
while (!myqueue.empty())
{
current = myqueue.front();
myqueue.pop();
tmp->bfs.push_back(current); <- causes segmentation fault
....
return tmp;
}
我也尝试执行以下操作,而没有成功:
info *tmp = (info*) malloc(sizeof(struct info));
std::list<int> bsf;
tmp->bsf = bsf // and then call tmp->bsf.push_back()....
问题在于将C 代码与C代码混合。
在此语句中
info *tmp = (info*) malloc(sizeof(struct info));
为结构分配内存,而无需调用其数据成员的构造函数,
而不是malloc
,您必须使用操作员new
。否则数据成员std::list<int> bfs;
将不会构造。
相关文章:
- C++中带有List类的迭代器Segfault
- 使用Vulkan hpp vk::enumerateInstanceVersion()会导致segfault
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- std::partition segfault issue
- OpenSSL: EC_POINT_set_compressed_coordinates_GFp segfault
- 检查nullptr是否100%保护内存布局不受segfault影响
- OpenCV Tracker 属性访问在 ARM 上因 SEGFAULT 而失败,但在 X86_64 中工作
- 为什么优化大型 std::vector 数组会导致 SegFault?
- C++segfault,可重复的例子
- 带有zip_source_buffer的libzip会导致数据损坏和/或segfault
- 导致SegFault C++的析构函数
- 从引用的Vector获取SEGFAULT
- 尝试读取/写入Graphviz DAG值的工作证明会导致segfault
- Segfault如果更改派生类的指针值
- SegFault deleting QTreeWidgetItem
- PyImport_Import segfault
- 添加#pragma循环后出现Segfault
- 使用 gmock 时的 SegFault
- C 多线程,在并行多个线程时获得SegFault
- 访问std :: list instruct struct诱导segfault