当前位置:  开发笔记 > 编程语言 > 正文

为什么这个程序会产生运行时错误?

如何解决《为什么这个程序会产生运行时错误?》经验,为你挑选了1个好方法。

我正在为双向链表编写一个C程序.以下代码只包含main函数和insert函数.

cfile.c

/*
    In the following code doubly linked list is implemented
*/

//header files
#include 
#include 

//create structure for node
struct node{
    int info;
    struct node *LChild;
    struct node *RChild;
}*start;

typedef struct node *NODE;

//function to insert data in the list
void insert(){
    int data;
    NODE q, temp;
    temp = (NODE)malloc(sizeof(struct node));
    if(temp){
        printf("\nEnter the data to be inserted in the list: ");
        scanf("%d", &data);
        temp->info = data;
        temp->RChild = NULL;
    }
    else{
        printf("Out of memory.");
        return;
    }
    if(start == NULL){
        temp->LChild = NULL;
        start->LChild = temp;
        start = temp;
    }
    else{
        q = start;
        while(q->RChild != NULL){
            q = q->RChild;
        }
        q->RChild = temp;
        temp->LChild = q;
    }
    return;
}

//function to insert at the begining
void insert_at_beg(){
    int data;
    NODE temp;
    temp = (NODE)malloc(sizeof(struct node));
    if(temp){
        printf("\nEnter the data to be inserted at the beginning of the list: ");
        scanf("%d", &data);
        temp->info = data;
        temp->LChild = NULL;
        temp->RChild = start;
        start->LChild = temp;
        start = temp;
    }
    else{
        printf("Out of memory.");
    }
    return;
}

//function to insert after a position
void insert_after_pos(){
    int data, pos;
    NODE temp, q;
    printf("\nEnter the position after which data will be inserted: ");
    scanf("%d", &pos);
    q = start;
    for(int i = 0; i < pos-1; i++){
        q = q->RChild;
        if(q == NULL){
            printf("There are less than %d position.", pos);
            return;
        }
    }
    temp = (NODE)malloc(sizeof(struct node));
    if(temp){
        printf("\nEnter the data to be inserted after the %d position: ", pos);
        scanf("%d", &data);
        temp->info = data;
        q->RChild->LChild = temp;
        temp->RChild = q->RChild;
        temp->LChild = q;
        q->RChild = temp;
    }
    return;
}

//function to delete the node
void delete(){
    NODE temp, q;
    int num;
    printf("\nEnter the data to be deleted: ");
    scanf("%d", &num);
    if(start->info == num){
        temp = start;
        start = start->RChild;
        start->LChild = NULL;
        free(temp);
        return;
    }
    q = start;
    while(q->RChild->RChild != NULL){
        if(q->RChild->info == num){
            temp = q->RChild;
            q->RChild = temp->RChild;
            temp->RChild->LChild = q;
            free(temp);
            return;
        }
        q =  q->RChild;
    }
    if(q->RChild->info == num){
        temp = q->RChild;
        free(temp);
        q->RChild = NULL;
        return;
    }
    printf("\nElement %d is not found on this list.", num);
    return;
}

//function to display the list
void display(){
    NODE q;
    if(start == NULL){
        printf("\nList is empty.");
        return;
    }
    q = start;
    while(q != NULL){
        printf("%d ", q->info);
        q = q->RChild;
    }
    return;
}

//function to reverse the list
void reverse(){
    NODE p1, p2;
    p1 = start;
    p2 = p1->RChild;
    p1->RChild = NULL;
    p1->LChild = p2;
    while(p2 != NULL){
        p2->LChild = p2->RChild;
        p2->RChild = p1;
        p1 = p2;
        p2 = p2->LChild;
    }
    start = p1;
    return;
}

int main(){
    int choice;
    char ch;
    start = NULL;
    printf("Menu for doubly linked list");
    do{
        printf("\n1.Insert\n2.Insert at beginning\n3.Insert after position\n4.Display\n5.Delete\n6.Reverse\n7.Exit\nEnter your choice: ");
        scanf("%d", &choice);
        switch(choice){
            case 1: insert();
                    break;
            case 2: insert_at_beg();
                    break;
            case 3: insert_after_pos();
                    break;
            case 4: display();
                    break;
            case 5: delete();
                    break;
            case 6: reverse();
                    break;
            case 7: exit(0);
                    break;
            default: printf("Invalid choice!"); 
        }
        printf("\nDo you want to continue ? ");
        scanf(" %c", &ch);
    }while(ch == 'y' || ch == 'Y');
    return 0;
}

在编译上面的代码时,gcc cfile.c它正在成功编译.但它产生运行时错误.在调试时gdb我发现第28行有一些错误.不幸的是我无法理解错误是什么.它正在显示

Program received signal 
SIGSEGV, Segmentation fault.
0x000000010040116a in insert () at cfile.c:28
28          start->LChild = temp;

我知道它必须使用解除引用指针做一些事情.

但为什么 ?这个代码有什么问题,我start = NULL在调用insert函数之前做过.

这是stackdump

Exception: STATUS_ACCESS_VIOLATION at rip=0010040116A
rax=0000000000000000 rbx=00000000FFFFCC40 rcx=0000000600018040
rdx=0000000600028460 rsi=00000006000283B0 rdi=0000000000000000
r8 =00000000FFFFB5FC r9 =000000018013E150 r10=0000000100000000
r11=000000010040111C r12=00000000FFFFCC61 r13=0000000000000000
r14=00000000FFFFCC61 r15=000000018021AE83
rbp=00000000FFFFCBC0 rsp=00000000FFFFCB80
program=E:\Learning\Languages\C\a.exe, pid 8612, thread main
cs=0033 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame        Function    Args
000FFFFCBC0  0010040116A (000FFFFCC61, 00000000000, 000FFFFCC61, 000FFFFCCC0)
000FFFFCBF0  001004011E6 (00000000020, FF0700010302FF00, 00180047891, 00000000000)
000FFFFCCC0  00180047902 (00000000000, 00000000000, 00000000000, 00000000000)
00000000000  00180045693 (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFFFF0  00180045744 (00000000000, 00000000000, 00000000000, 00000000000)
End of stack trace

我对stackdump一无所知.所以我无法理解它.

gcc版本:5.4.0 gdb版本:7.10.1



1> Paul R..:

在这个块中:

if(start == NULL){
    temp->LChild = NULL;
    start->LChild = temp; // <<< `start` is NULL here !!!
    start = temp;
}

你试图start在它仍然存在的情况下取消引用NULL,因此是seg错误.

推荐阅读
虎仔球妈_459
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有