Segmentation Fault in Loop's Condition

自作多情 提交于 2019-12-12 05:11:07

问题


The following code is to sort a linked list after creating it. The sorting algorithm used is somewhat similar to Bubble Sort. I am checking the two consecutive nodes and swapping them if necessary. I used the debugger which told me that the fault is raised while condition checking for the loops which are used while sorting.

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>

using namespace std;

struct link_list
{
       char value[20];
       struct link_list *next;
};

int main()
{
    struct link_list *head=NULL;
    int i,j;
    char input[20];
    char ch;
    struct link_list *loop_var,*temp2,*prev_node,*temp4=NULL;
    temp3=NULL;
    do
    {
        cout<<"\nEnter the string you want to insert";
        cin>>input;

        cout<<"\nDo you want to continue entering?";
        cin>>ch;

        if  (head==NULL)
        {
            head=new link_list;
            strcpy(head->value,input);
            head->next=NULL;
            continue;
        }
        for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next);
        temp2=new link_list;
        loop_var->next=temp2;
        strcpy(temp2->value,input);
        temp2->next=NULL;
    }while(ch=='y' || ch=='Y');

    for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
    {
        cout<<loop_var->value<<"\n";
    }
    cout<<loop_var->value<<"\n";

    char arr[20];

    for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
    {
        cout<<"\nLoop1";
        for (temp4=head;temp4->next!=NULL;temp4=temp4->next)
        {
            cout<<"\nLoop2";
            temp2=temp4;
            if  (strcmp(temp2->value,temp2->next->value)>0)
            {
                cout<<"\nSwap Enter";

                if  (temp2==head && temp2->next->next==NULL)
                {
                    cout<<"\nSpecial1";
                    temp2->next->next=temp;
                    temp2->next=NULL;
                }
                else if (temp2==head)
                {
                     cout<<"\nSpecial2";
                     head=temp2->next;
                     temp2->next=head->next;
                     head->next=temp2;
                }
                else if (temp2->next->next==NULL)
                {
                     cout<<"\nSpecial3";
                     prev_node->next=temp2->next;
                     prev_node->next->next=temp2;
                     temp2->next=NULL;
                }
                else
                {
                    cout<<"\nNormal1";
                    prev_node->next=temp2->next;
                    temp2->next=prev_node->next->next;
                    prev_node->next->next=temp2;
                    cout<<"\nNormal2";
                }
            }
            prev_node=temp4; 
            cout<<"\nLoop2PreExit";
            fflush(stdin);
            cout<<"\nLoop2Exit";
        }
        cout<<"\nLoop1Exit";
    }
    for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
    {
        cout<<loop_var->value<<"\n";
    }
    cout<<loop_var->value;
    getch();
}

回答1:


                    temp2->next->next=temp;

"temp" is not defined anywhere... if your compiler filled in that hole for you, then this is what is causing your loop's condition to segfault.

Also, naming every other variable "temp#" is an easy way to have mistakes like this.



来源:https://stackoverflow.com/questions/11707773/segmentation-fault-in-loops-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!