【问题标题】:Segmentation fault while doing malloc for a struct pointer为结构指针执行 malloc 时出现分段错误
【发布时间】:2014-04-26 06:00:14
【问题描述】:

我尝试编写基本的链表代码。我有两个函数可以将数据添加到列表的开头和列表的末尾。在开始时添加数据的功能每次都可以正常工作。

我总是在insert_end 函数中遇到分段错误。 在对它执行malloc 之后尝试访问指向结构的temp1 指针时出现错误。但这与我在 insert_first 函数中所做的完全相同,但每次都有效。我尝试谷歌搜索并尝试在论坛中,没有答案。请帮忙。

这个链接是我的确切问题.. 但我不完全理解解决方案

Segmentation fault in C with malloc

我遇到了错误,特别是在这个块上

struct node *temp1,*trav;
    temp1 = (struct node *)malloc(sizeof(struct node));
    trav = (struct node *)malloc(sizeof(struct node));
    if (temp1 != NULL) { //******************Segmentation fault at this line**********
        temp1->data = input;
    }






#include<stdio.h>
#include<stdlib.h>

//*******Structure to hold linked list*********//
struct node {
    int data;
    struct node *next;
}*head,*temp;


//***********Function to Display everything**********//

void display(struct node *head) {
    struct node *trav;
    trav= (struct node *)malloc(sizeof(struct node));
    printf("Entering into Display\n");
    if (head == NULL) {
        printf("Oh My God, the list is empty\n");
    }
    else {
        trav = head;
        while (trav != NULL) {
            printf("Value stored in [%p] is [%d]\n",trav,trav->data);
            trav = trav->next;
        }
    }
}


//***********Function to Insert at beginning*********//

struct node *insert_first(struct node *head,int input) {
    temp = (struct node *)malloc(sizeof(struct node));
    temp->data = input;
    printf("\nEntering insert first");
    if (head == NULL) {
        head = temp;
        head->next = NULL;
    }
    else {
        temp->next = head;
        head = temp;
    }
    return head;
}


//**************Function to Insert at End******************//
struct node *insert_last(struct node *head,int input) {
    struct node *temp1,*trav;
    temp1 = (struct node *)malloc(sizeof(struct node));
    trav = (struct node *)malloc(sizeof(struct node));
    if (temp1 != NULL) {
        temp1->data = input;
    }
    else {
        printf("empty");
    }
    printf("\nEntering insert last");
    if (head == NULL) {
        head = temp1;
        head->next = NULL;
    }
    else {
        trav = head;
        while (trav != NULL) {
            trav = trav->next;
        }
        trav->next = temp1;
    }
    return head;
}




//*************Main Fucntion***********//

int main() {
    int choice,value;
    head = NULL;
    while(1) {
        printf("\n******Please Enter your choice****\n1. To insert at beginning\n2. To insert at End\n3. To Insert middle\n4. To delete\n5. To display\n0. To Exit\n");
        scanf("%d",&choice);
        switch(choice){
            case 1:
                printf("Please Enter the value to be added\n");
                scanf("%d",&value);
                head = insert_first(head,value);
                break;
            case 2:
                printf("Please Enter the value to be added\n");
                scanf("%d",&value);
                head = insert_last(head,value);
                break;

            case 5:
                display(head);
                break;
            case 0:
                return 0;
            default:
                printf("Thats a wrong choice\n");
                break;
        }
    }
}

【问题讨论】:

  • 你是什么insert_end?
  • 这段代码有很多问题:memleak:display() "malloc trav" 没有 free 或任何赋值,memleak:insert_last() "malloc trav" 没有 free 或任何赋值。
  • 对不起!!1 insert_last,事实上
  • 是的@harper。我知道有很多问题,我现在是初学者,正在尝试学习 C。将在需要时对其进行更正。但是我现在被屏蔽了。请帮忙

标签: c pointers struct segmentation-fault malloc


【解决方案1】:

这是有问题的块。

else {
    trav = head;
    while (trav != NULL) {
        trav = trav->next;
    }
    trav->next = temp1;

当你退出 while 循环时,trav 是 NULL。这使得这条线

    trav->next = temp1;

因分段违规而失败。

将该块更改为:

else {
    trav = head;
    while (trav->next != NULL) {
        trav = trav->next;
    }
    trav->next = temp1;

【讨论】:

  • 还需要设置temp1->next,根本不应该分配trav。
  • @Sahu 在我的情况下,分段错误发生在到达这个特定块之前。在 malloc 后访问 temp1 时出现错误!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 2020-06-05
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
相关资源
最近更新 更多