【问题标题】:Char array in linked list causes core dump链表中的字符数组导致核心转储
【发布时间】:2015-04-17 06:28:34
【问题描述】:

我创建了一个链表程序,它与 c 中的整数完美配合。 但如果将参数更改为 char 数组,并尝试执行 strcpy,则会导致核心转储。

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

struct node {
    char mac[25];
    struct node * next;
};
typedef struct node *list;

int main(void) {

   lista c;

   c = creoLista();
   c = insert_start(c, "aa:bb:cc:dd:e1");
   c = insert_start(c, "aa:bb:cc:dd:e2");
   c = insert_start(c, "aa:bb:cc:dd:e3");

   showList(c);
   return 0;
}

list createList() {
   return NULL;
}

list insert_start(list l1, char val[]) {
    list n;
    n =(list )malloc(sizeof(list));
    strcpy(n->mac,val);
    printf("ADDED: %s en ADDRESS:%p NEXT ADDRESS: %p\n", n->mac,(void *)(&n), (void *) (&n->next));
    n -> next = l1;

    return n;
}

void showList(list l1) {
     while (l1 != NULL){
         printf("Value: %s Address: %p\n",l1 -> mac,(void *) (&l1 -> next) );
         l1 = l1 -> next;
    }
}

关于我做错了什么以及为什么它适用于 int 而不是 char 数组的任何提示

谢谢

【问题讨论】:

  • 您没有检查代码中任何地方可能存在的空指针。
  • 这个malloc(sizeof(list)) 分配空间来保存一个指针。可能你需要这个malloc(sizeof(struct node)) 来分配空间来保存节点而不是指向节点的指针?
  • creoLista() 定义在哪里?此外,您也不需要强制转换 printf

标签: c arrays linked-list strcpy


【解决方案1】:
  1. 你的分配是错误的,因为你被typedef指针弄糊涂了,不要那样做

    n = malloc(sizeof(*n));
    

    不容易出错。

  2. 检查malloc()的返回值,不需要强制转换,所以

    n = malloc(sizeof(*n));
    if (n == NULL)
        return NULL;
    
  3. 你在初始化之前打印n-&gt;next指针的地址,改变这个

    printf("ADDED: %s en ADDRESS:%p NEXT ADDRESS: %p\n", n->mac,(void *)     (&n), (void *) (&n->next));
    n->next = l1;
    

    n->next = l1;
    printf("ADDED: %s en ADDRESS:%p NEXT ADDRESS: %p\n", n->mac, (void *)n, (void *)n->next);
    
  4. 你没有函数原型,所以你的编译器使用隐式函数声明,这很糟糕,因为它会假设所有函数都返回int,所以你需要在main()的定义之前添加这些

    list createList();
    list insert_start(list l1, char val[]);
    void showList(list l1);
    

    特别是前两个非常重要,启用编译器警告以防止这种情况发生。

这是您修复的代码:

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

struct node {
    char mac[25];
    struct node * next;
};
typedef struct node *list;

list insert_start(list l1, char val[]);
void showList(list l1);

int main(void) {
    lista c;

    c = insert_start(NULL, "aa:bb:cc:dd:e1");
    c = insert_start(c, "aa:bb:cc:dd:e2");
    c = insert_start(c, "aa:bb:cc:dd:e3");

    showList(c);
    return 0;
}

list insert_start(list l1, char val[]) {
    list n;

    n = malloc(sizeof(*n));
    if (n == NULL)
        return NULL;
    strcpy(n->mac, val);
    n->next = l1;

    printf("ADDED: %s en ADDRESS:%p NEXT ADDRESS: %p\n", n->mac, (void *)n, (void *)n->next);
    return n;
}

void showList(list l1) {
     while (l1 != NULL) {
        printf("Value: %s Address: %p\n", l1->mac, (void *)l1->next);
        l1 = l1->next;
    }
}

你还需要一个freeList() 函数。

【讨论】:

  • 我尝试更改代码。并且在第二次 insert_start 调用后仍然返回核心转储
【解决方案2】:

问题是这个分配:

malloc(sizeof(list))

它显示了制作指针的类型别名的问题,因为您在这里只分配指针的大小而不是整个结构。

【讨论】:

    【解决方案3】:

    typedef struct node *list;

    n =(list )malloc(sizeof(list));

    list 是指向结构node 的指针,malloc() 应该传递有效字节的大小以执行strcpy,如果您使用的是 64 位机器,则列表可能只是8 字节。将 malloc() 分配更改为指针指向的大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      相关资源
      最近更新 更多