这个分段错误的根源是什么?

发布于 2024-12-11 18:30:28 字数 1321 浏览 0 评论 0原文

我这里有一个奇怪的错误: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情域 2024-12-18 18:30:29

这很可能是因为你管理列表的方式。您正在添加/删除动态分配的对象,并通过引用传递它们。这是一个坏主意。

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.

方圜几里 2024-12-18 18:30:29

好的,我粗略地浏览了一下您的代码,发现了几个问题。

  1. 在您的条目中<>类

    operator T* () {return &stud;}
    

stud 已经是一个 T*。您现在返回该指针的地址,而不是指针本身。

  1. 在你的llist类中

    if(temp==temp->向后){
            删除这个;
    

如果此条件为真,则实际上会删除您的列表类。哎哟?

就我个人而言,我认为你需要重新开始你的列表实现:)

Ok, I see several issues with a cursory glance over your code.

  1. In your entry<> class

    operator T* () {return &stud;}
    

stud is already a T*. You are now returning the address of that pointer, rather than the pointer itself.

  1. In your llist class

    if(temp==temp->towardsback){
            delete this;
    

Which 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 :)

睫毛上残留的泪 2024-12-18 18:30:29

我主要使用 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].

梦中的蝴蝶 2024-12-18 18:30:28

除非您正在执行低级内存操作(并且您没有在发布的程序中执行此操作),否则 分段错误是内存损坏的标志,即您搞乱了一些编码。请注意,内存损坏通常发生在错误触发之前。在极端情况下,初始内存损坏和实际分段错误可能相隔数小时和模块。

第一步应该是在 valgrind 或 gdb 中运行程序以找出分段错误的详细信息。另外,始终至少使用 gcc -Wall 进行编译并记下每个警告 - 除非您正在修改编译器,否则每个警告的更改都表明存在错误。

就您而言,错误几乎肯定是在 llist 的实现中。该实现存在许多问题:

  • 实现不属于 .h 文件,它们应该位于 .cpp 文件中。
  • entry.towardsbackentry.towardsfront 通常称为 prev(ious) 和 next - 这些名称更短,更容易区分。
  • 根据链表的一般命名约定,hold 可能应该称为 head
  • 长度为 0 的列表不应包含任何条目,即使用 hold == NULL 进行初始化。
  • 从第 61 行开始(在 add 中):
hold->towardsback = node;
hold->towardsback->towardshead = node; // node->towardshead = node;

第二行几乎肯定是错误的。您可能想先配置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:

  • Implementations don't belong in .h files, they should be in .cpp files.
  • entry.towardsback and entry.towardsfront are usually called prev(ious) and next - those names are shorter and easier to distinguish.
  • hold should probably be called head, in accordance with the general naming conventions of linked lists.
  • A list of length 0 shouldn't have any entries, i.e. be initialized with hold == NULL.
  • From line 61 (in add):
hold->towardsback = node;
hold->towardsback->towardshead = node; // node->towardshead = node;

The second line is almost certainly wrong. You probably want to configure node first and then just set hold->towardsback = node;.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文