简体   繁体   English

为什么我的代码中出现分段错误(核心转储)错误?

[英]Why am I getting segmentation fault (core dumped) error in my code?

This code is for creating a graph using adjacency-list.此代码用于使用邻接表创建图形。
node is the structure for horizontal linked list for every element of the vertical linked list, node是垂直链表每个元素的水平链表结构,
whereas graphnode is the structure for vertical linked list.graphnode是垂直链表的结构。

The code is compiling properly, but when the first input is given, it shows segmentation fault after it.代码编译正确,但是当给出第一个输入时,它在它之后显示分段错误。

#include<iostream>
using namespace std;
struct node        //struct
{
    struct node *next;
    char value;
};
struct graphnode
{
    struct node *start;
    struct graphnode *down;
    char value;
    int count;
};
graphnode * creategraphnode(char val)
{
    struct graphnode *newnode=new graphnode;
    newnode->start=NULL;
    newnode->down=NULL;
    newnode->value=val;
    newnode->count=0;
    return newnode;
}
node *createnode(char val)
{
    struct node *newnode=new node;
    newnode->next=NULL;
    newnode->value=val;
}
void insertgraphnode(char value,graphnode *graph)
{
    struct graphnode *newnode=creategraphnode(value);
    if(graph==NULL)
    {
        graph=newnode;
    }
    else
    {
        graphnode *temp=graph;
        while(temp->down)
            temp=temp->down;
        temp->down=newnode;
    }
}
void insertnode(graphnode *graph)
{
    char val;
    struct node *temp=graph->start;
    cout<<"What is the outdegree of "<<graph->value;
    cin>>graph->count;
    cout<<"\nEnter"<<graph->count<<" nodes separated by space:";
    for(int i=1;i<=graph->count;i++)
    {
        cin>>val;
        node* newnode=createnode(val);
        temp=newnode;
        temp=temp->next;
    }
}
void display(struct graphnode *graph)
{
    if(graph==NULL)
    {
        cout<<"\nNo data to display";
        return;
    }
    struct graphnode *temp=graph;
    while(temp)
    {
        struct node *temp1=temp->start;
        cout<<temp->value<<"=>> ";
        if(temp1==NULL)
        {
            continue;
        }
        else
        {
            while(temp1)
            {
                cout<<temp1->value<<"-> ";
                temp1=temp1->next;
            }
        }
        cout<<"/\n";
    }
}
int main()
{
    struct graphnode *graph=NULL;
    int totalnodes;
    char val;
    cout<<"\nHow many nodes does the graph contain? : ";
    cin>>totalnodes;
    cout<<"\nEnter "<<totalnodes<<" space separated nodes : ";
    for(int i=1;i<=totalnodes;i++)
    {
        cin>>val;
        insertgraphnode(val,graph);
    }
    struct graphnode *temp=graph;
    while(temp->down)
    {
        insertnode(temp);
        temp=temp->down;
    }
    display(graph);
    return 0;
}

If I'm not wrong, your code has at least three problems.如果我没记错的话,你的代码至少有三个问题。

1) your createnode() don't return the created node; 1)您的createnode()不返回创建的节点; add return newnode;添加return newnode;

2) your insertgraphnode() don't change the second parameter in the calling function; 2)您的insertgraphnode()不更改调用函数中的第二个参数; so, when you call it in main()所以,当你在main()调用它时

insertgraphnode(val,graph); insertgraphnode(val,graph);

graph is NULL ; graph NULL inside insertgraphnode() you change the value but outside the function graph remain NULL ;insertgraphnode()内更改值但在函数graph保持NULL ever.曾经。 This cause the segmentation fault.这会导致分段错误。

You can solve this changing insertgraphnode() so the second parameter is a pointer-to-pointer and passing &graph .您可以解决这个不断变化的insertgraphnode()因此第二个参数是一个指向指针的指针并传递&graph

Another solution is modify insertgraphnode() to return the graph value另一种解决方案是修改insertgraphnode()以返回graph

graphnode *  insertgraphnode(char value,graphnode *graph)
{
    struct graphnode *newnode=creategraphnode(value);
    if(graph==NULL)
    {
        graph=newnode;
    }
    else
    {
        graphnode *temp=graph;
        while(temp->down)
            temp=temp->down;
        temp->down=newnode;
    }
    return graph;
}

and calling it in this way并以这种方式调用它

graph = insertgraphnode(val,graph);

3) the while in display() goes in loop because you don't change the value of temp 3) whiledisplay()去中环,因为你不改变的值temp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在使用二维数组时我的程序会出现分段错误(核心转储)错误? - Why am I getting the Segmentation fault (core dumped) error in my program when using 2d arrays? 为什么我在 systemC 中遇到分段错误(核心转储)? - Why am I getting a segmentation fault (core dumped) in systemC? 为什么我的代码显示错误细分错误(核心已转储) - Why is my code showing error Segmentation fault(core dumped) 为什么我的代码包含错误分段错误(核心转储)? - Why does my code contain the error segmentation fault(core dumped)? 为什么在 main 执行之前我会出现分段错误(核心转储)? - Why am I getting Segmentation Fault (core dumped) before main is even executed? 为什么我的代码结果显示分段错误(核心转储)? - why is my code result showing Segmentation fault (core dumped)? 在我的程序中出现错误:“分段错误(核心转储)”。 一切正常。 为什么? - Getting an error: “Segmentation fault (Core Dumped)” on my program. Everything working otherwise. Why? 为什么我的代码中出现分段错误? - Why am I getting Segmentation fault in my code? 为什么会出现这个错误? -分段错误(核心已转储) - Why this error? - Segmentation fault (core dumped) 为什么我没有使用此代码获得分段错误? (总线错误) - Why am I not getting a segmentation fault with this code? (Bus error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM