这个分段错误的根源是什么?
我这里有一个奇怪的错误: 882 Segmentation failure ./a.out
代码段:
int end=array.Length, loop=0;
cout<<end<<" about to print";
for(;loop<end;loop++){
cout<<"\nMr. loop says: ";
array.get(loop).print(fout);
}
我的程序的输出:
enpty initializer called for llist
enpty initializer called for entry
adding
adding
999deleting999
done deleting
123deleting123
done deleting
333deleting333
done deleting
printing
2 about to print
./g+: line 7: 882 Segmentation fault ./a.out
和重要的输出:
printing
2 about to print
./g+: line 7: 882 Segmentation fault ./a.out
意味着错误是这一行:
for(;loop<end;loop++){
这已经证明良好的价值观,并且在语法上是正确的(是的,我知道它的风格很糟糕)。
有想法吗?我的大学里似乎没有人能够帮助我解决这个问题。
以下是文件:
I've got a bizarre error here: 882 Segmentation fault ./a.out
The segment of code:
int end=array.Length, loop=0;
cout<<end<<" about to print";
for(;loop<end;loop++){
cout<<"\nMr. loop says: ";
array.get(loop).print(fout);
}
Output of my program:
enpty initializer called for llist
enpty initializer called for entry
adding
adding
999deleting999
done deleting
123deleting123
done deleting
333deleting333
done deleting
printing
2 about to print
./g+: line 7: 882 Segmentation fault ./a.out
and vital output of:
printing
2 about to print
./g+: line 7: 882 Segmentation fault ./a.out
meaning that the error is this line:
for(;loop<end;loop++){
which has proven good values and is syntactically correct (yes I know its bad style, though).
Ideas? No one at my university seems to be able to help me with this.
here are the files:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很可能是因为你管理列表的方式。您正在添加/删除动态分配的对象,并通过引用传递它们。这是一个坏主意。
Its most likely because how you manage your list. You're adding/removing dynamically allocated objects, and pass them by reference. It's a bad idea.
好的,我粗略地浏览了一下您的代码,发现了几个问题。
在您的条目中<>类
stud
已经是一个 T*。您现在返回该指针的地址,而不是指针本身。在你的
llist
类中如果此条件为真,则实际上会删除您的列表类。哎哟?
就我个人而言,我认为你需要重新开始你的列表实现:)
Ok, I see several issues with a cursory glance over your code.
In your entry<> class
stud
is already a T*. You are now returning the address of that pointer, rather than the pointer itself.In your
llist
classWhich will, in effect, delete your list class if this condition is true. Ouch?
Personally, I think you need to start again on your list implementation :)
我主要使用 C#,但数组在 C++ 中的索引是否为 0?因此,您需要将 end 设置为 array.Length-1。至少在 C# 中,数组中的第一个元素是 array[0]。
I work mostly with c#, but are arrays 0 index in c++? So, you would need to set end to array.Length-1. At least in c#, the first element in the array would be array[0].
除非您正在执行低级内存操作(并且您没有在发布的程序中执行此操作),否则 分段错误是内存损坏的标志,即您搞乱了一些编码。请注意,内存损坏通常发生在错误触发之前。在极端情况下,初始内存损坏和实际分段错误可能相隔数小时和模块。
第一步应该是在 valgrind 或 gdb 中运行程序以找出分段错误的详细信息。另外,始终至少使用
gcc -Wall
进行编译并记下每个警告 - 除非您正在修改编译器,否则每个警告的更改都表明存在错误。就您而言,错误几乎肯定是在
llist
的实现中。该实现存在许多问题:entry.towardsback
和entry.towardsfront
通常称为prev
(ious) 和next
- 这些名称更短,更容易区分。hold
可能应该称为head
。hold == NULL
进行初始化。add
中):第二行几乎肯定是错误的。您可能想先配置
node
,然后设置hold->towardsback = node;
。Unless you're doing low-level memory operations (and you're not doing this in the posted program), a segmentation fault is a sign of memory corruption, i.e. you've messed up some coding. Note that the memory corruption typically occurs before the error is triggered. In extreme cases, initial memory corruption and actual segmentation fault can be hours and modules apart.
Your first step should be to run the program in valgrind or gdb to find out the details of the segmentation fault. Also, always compile with at least
gcc -Wall
and take note of every warning - unless you're modifying the compiler, changes are every single warning indicates a bug.In your case, the error is almost certainly in the implementation of
llist
. There is a number of problems with that implementation:entry.towardsback
andentry.towardsfront
are usually calledprev
(ious) andnext
- those names are shorter and easier to distinguish.hold
should probably be calledhead
, in accordance with the general naming conventions of linked lists.hold == NULL
.add
):The second line is almost certainly wrong. You probably want to configure
node
first and then just sethold->towardsback = node;
.