【发布时间】:2012-11-08 13:47:17
【问题描述】:
我正在为图形结构创建一个邻接列表。下面是我的代码 sn-p 当我在 gdb 中运行时,给出“程序收到信号 SIGSEGV,分段错误。std::_List_node_base::hook () 中的 0x0040340f”错误。有人可以指出代码中的错误。
struct graph{
list<int> vertex;
}*v;
list<int>::iterator it;
cin>>num_vertices;
v = new graph[num_vertices];
if (v == 0)
cout << "Error: memory could not be allocated";
for(i=1;i<=num_vertices;i++)
{
cin>>num_connected;
for(j=1;j<=num_connected;j++)
{
cin>>m;
(v+i)->vertex.push_back(m);
}
}
for(i=1;i<=num_vertices;i++)
for(it= (v+i)->vertex.begin();it!= (v+i)->vertex.end();it++)
cout<<*it<<"->";
【问题讨论】:
-
你有没有尝试在gdb中一步步运行,看看是哪一行导致了错误?
-
您可以在 gdb 中使用
backtrace或简单地使用bt来查看导致错误的调用。并且不要使用v+i,使用v[i]