简体   繁体   English

为什么在此链表创建中出现分段错误?

[英]Why am I getting a segmentation fault on this linked list creation?

I am very new to C and I am not understanding where the segmentation fault is occurring in my code. 我对C非常陌生,我不了解我的代码中发生分段错误的位置。 I am just trying to create a linked list which will be the type unit for a hash table. 我只是试图创建一个链表,该链表将成为哈希表的类型单元。 I understand that I probably didn't use a malloc where I was supposed to. 我知道我可能没有在原本应该的地方使用malloc。 The main function will run but as soon as I add one item I get the segmentation fault. 主要功能将运行,但是一旦添加一项,就会出现细分错误。

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


typedef struct word_link
{
    char* val;
    struct word_link * next;
} word_link;

void add_to_list(char* word, word_link *head);

void add_to_list(char* word, word_link *head){

  int i;
  word_link * temp = NULL;
  word_link * p = NULL;

  temp = (word_link*)malloc(sizeof(word_link));
  temp->val = word;
  temp->next = NULL;

  if(head == NULL){
    head = temp;
  } else {
    p = head;
    while(p->next != NULL){
      p = p->next;
    }
    p->next = temp;
  }
}
void main() {

  int i = 0;
  struct word_link *lst = malloc(sizeof(word_link));
  char* word = "";
  while(i == 0){
  printf("what to add?  ");
  scanf("%s",word);
  add_to_list(word, lst);
  printf("continue?  ");
  scanf("%d", i);
  }
  printf("%s", lst->val);
  printf("%s", "asdf;kl");
}

Your head is local to add_to_list function and will be destroyed once control exits add_to_list function. 您的head位于add_to_list函数的本地,一旦控制退出add_to_list函数, add_to_list head将被销毁。

Also any changes done to head inside add_to_list will not affect the original head lst . 同样,在add_to_list内部对head所做的任何更改都不会影响原始head lst

Solution: 解:

You can pass the reference of original head to insert in order to retain changes made in add_to_list as below. 您可以传递要插入的原始add_to_list的引用,以保留在add_to_list所做的add_to_list ,如下所示。

void add_to_list(char* word, word_link **head);

void add_to_list(char* word, word_link **head){    
  int i; 
  word_link * temp = NULL;
  word_link * p = NULL;

  temp = (word_link*)malloc(sizeof(word_link));
  if (temp == NULL) return;

  temp->val = word;
  temp->next = NULL;

  if(*head == NULL){
    *head = temp;
  } else {
    p = *head;
    while(p->next != NULL){
      p = p->next;
   }
    p->next = temp;
  }
}

And you call add_to_list as below. 然后按如下所示调用add_to_list

add_to_list(word, &lst);

Another Issue: 另一个问题:

char* word = ""; this will create word as pointer to immutable string literal. 这将创建word作为指向不可变字符串文字的指针。 Modifying the word content scanf("%s",word); 修改word内容scanf("%s",word); will result in UB and since every time you are passing the same pointer to add_to_list every node in the list will point to same word . 会导致UB,并且因为每次传递相同的指针到add_to_list时,列表中的每个节点都指向同一word

You might want to declare it as below. 您可能要声明如下。

char *word = malloc(256); Inside the while loop and pass it to add_to_list . 在while循环内,并将其传递给add_to_list

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM