【问题标题】:Segmentation Fault 11 whenever I run this. Would like assistance/feedback每当我运行它时,Segmentation Fault 11。需要帮助/反馈
【发布时间】:2015-04-14 02:50:16
【问题描述】:

所以我正在对循环列表进行非常基本的实现。我还没有制作删除功能。每当我运行 cpp 时,我都会收到 seg fault 11。任何反馈都将不胜感激。谢谢。

#include <iostream>
using namespace std;

struct node{
    node* next=NULL;
    bool tail= false;
    int contents;
};
node* start;//start is a pointer that exists at the start of the list before the first element

class CircList{
node *seek;
public:

CircList (){  //creates a list of one node that points to itself
    node *b= new node;
        b->contents=0;
        b->next = b;
        start->next=b;
        b->tail=true;
}

bool empty(){
    if(start->next==NULL){
        return true;
    }
    return false;
}

int size(CircList a){
    if(start->next==NULL){
        cout<<"size is 0 \n";
        return true;
    }
    seek=start->next;
    for(int i=0; i++;){
    if(seek->tail==true){
        cout<<"size is "<<i;
    }
    seek=seek->next;
    }
    return 0;
}

void insert(int pos, int val){
    if(start->next ==NULL){//if inseting when the list is empty
        node *b= new node;
        b->next = b;
        b->tail=true;
        return;
    }

    node *b= new node;
    b->contents= val;
        seek=start->next;
    for(int i=0;i<=pos; i++){
        if(seek->tail==true){//if inserting at the end
            seek->tail=false;
            b->tail=true;
            seek->next=b;
            b->next=start->next;
        }
        if(pos==i){//if inserting between two nodes
            b->next = seek->next;
            seek->next = b;
        }
            seek=seek->next;
    }
    }
void remove(int a){
    seek=start->next;
     for(int i=0;i<=a-1; i++){
        if(i<a){
            seek=seek->next;
        }
        if(i==a-1){

}
     }
}

    void display(){
        cout<<start->next->contents; //will also be completed in the near future
        seek=start->next;
        for(int i=0; ;i++){
        if(seek->tail==false){
           cout<<seek->contents<<"\n";
        }
        if(seek->tail==true){
           cout<<seek->contents<<"\n";
           return;
        }
    }
        }
    };

那是 .h 文件。以下是cpp。我只是插入数字进行测试。我想让程序运行,以便测试它的行为。

#include <iostream>
#include "CircList.h"
using namespace std;

int main(){

CircList a;
a.insert (5,5);
a.insert (5,5);
a.insert (1,4);
a.insert (20,65);
a.insert (3,7);
a.size(a);
a.display();
}

【问题讨论】:

  • 编译时包含所有警告和调试信息(例如g++ -Wall -Wextra -std=c++11 -g),甚至可能与-fsanitize=address(如果可用)一起编译。然后使用调试器 (gdb) 或者valgrind。您的问题是题外话,因为您对它没有任何部分了解。

标签: c++ fault


【解决方案1】:

我一直将 start 视为节点而不是指针。通过使 start = Null 并用“start”替换所有“start->next”,我可以编译和运行它。但现在它只是在内容中无限插入值为 0 的节点。

编辑:我修好了。通过将显示函数中奇怪的 for 循环更改为 while 循环,它不再在构造函数中无限插入节点。它现在似乎工作得很好。

【讨论】:

    【解决方案2】:

    这会导致段错误

    start->next=b;
    

    因为 start 在程序开始时为 NULL,所以您取消引用空指针。

    而是将 start 设置为构造函数中的第一个节点

    start = b;
    

    【讨论】:

    • @MattMcNabb 是的,一旦他修复了我的意思的 seg 错误,但好的,我会删除它。
    【解决方案3】:

    您的全局变量start 是一个未初始化的指针,但您到处取消引用它。

    【讨论】:

    • 谢谢。我将指针设为 NULL 并且它仍然可以编译,但是当我运行代码时我仍然遇到相同的段错误。还有什么我可以做得更好的吗?
    • @Sid 取消引用 NULL 与使用未初始化的指针一样糟糕。没有基本的指针理解,你是怎么写这么多代码的?
    • @Beta 全局变量没有显式初始化是值初始化的(在指针的情况下,这意味着一个空指针)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多