【发布时间】:2015-03-30 00:19:59
【问题描述】:
在Multidimensional arrays as class member with arbitrary bounds 中,有人建议我应该使用整数向量的向量来表示我的数组支持的图形对象中的动态二维数组。但是,每当我尝试运行我的代码时,都会遇到分段错误。 GDB 指向变量定义为arraygraph 类型的行;我想这意味着我在指定edges 的类型时做错了。这是类定义:
class arraygraph {
private:
public:
void open(char *filename);
bool exists(int nodeid);
int node_count(void);
int weight(int nodea, int nodeb);
void print();
private:
int count;
vector <vector <int> > edges; //A vector of vectors! [Inception Noise]
};
该对象的所有方法都已定义(有趣的是如何使用尚未填写的原型进行编译...)。代码编译时没有错误或警告。为什么会出现段错误?
编辑:
这是一些反汇编程序的输出:
163 int main(int argc, char *argv[]) {
main(int, char**):
00401c86: lea 0x4(%esp),%ecx
00401c8a: and $0xfffffff0,%esp
00401c8d: pushl -0x4(%ecx)
00401c90: push %ebp
00401c91: mov %esp,%ebp
00401c93: push %ebx
00401c94: push %ecx
00401c95: sub $0x20,%esp
00401c98: mov %ecx,%ebx
00401c9a: call 0x402350 <__main>
164 arraygraph thegraph;
00401c9f: lea -0x18(%ebp),%eax (this is where the problem occured according to GDB)
00401ca2: mov %eax,%ecx
00401ca4: call 0x403e90 <arraygraph::arraygraph()>
那么显然段错误发生在arraygraph 构造之前?我不知道该怎么做。
编辑:
main() 的整体是这样的:
int main(int argc, char *argv[]) {
arraygraph thegraph;
thegraph.open(argv[1]);
return 0; //Added this after I noticed I'd omitted it - didn't fix anything
}
这里是arraygraph::open():
//Only call .open() once. A second call will leave the old graph's dessicated corpse around the edges of the new one.
void arraygraph::open(char *filename){
int count;
int x, y;
tomgraph loader;
loader.open(filename);
count=loader.node_count();
//delete edges;
//edges = new vector <vector <int> >;
for (x=1; x <= count; x++) {
for (y=1; y <= count; y++) {
int weight;
if (loader.is_connected(x,y,&weight) || loader.is_connected(y,x,&weight)) {
edges[x-1][y-1]=weight;
} else {
edges[x-1][y-1]=0; //0 represents "no edge"
}
}
}
}
但根据反汇编程序,无论如何都不会调用它。
编辑:
完整代码here。嗯...我认为它说它有效?让我试试不同的机器...
编辑:
不。在完全独立的 Linux 机器上编译后出现类似的段错误。
编辑:
为了确认,我注释掉了对 thegraph.open() 的调用。没修好。
【问题讨论】:
-
你可能想要一个构造函数,你能提供错误发生的源代码吗?错误不一定是由声明引起的,可能是它周围的代码。
-
@AlejandroDíaz 构造函数究竟会做什么?它周围没有代码;
arraygraph mygraph是main()中的第一行。 -
它将确保对象处于定义状态(即计数为 0),ideone.com/U3pHyg 您的代码按原样工作,因此类定义不是问题。
-
你的实例化实际上是什么样子的?
-
能否提供一个完整的编译示例来说明问题。
标签: c++ vector segmentation-fault