c - C 中的 malloc 和内存泄漏问题

标签 c memory-leaks linked-list malloc

我很难弄清楚我的程序出了什么问题。我试图通过在 createNode 函数中创建节点,然后使用 addNode 将它们添加到列表的头部来创建一个链表。当我尝试创建节点并出现段错误时,程序失败。

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

struct listnode {
    int line;
   char *word;
   struct lnode *next;
};


struct listnode* createNode (char* word, int line) {
  int strlen1 = strlen(word)+1;
    struct lnode *node = malloc(sizeof(struct lnode));
    node->word = malloc(sizeof(char)*strlen1);
    strcpy(node->word,word);
    node->word[strlen1] = '\0';
    node->next = NULL;
    node->line = line;
    return node;
}


void addNode (struct listnode** head, struct listnode* node) {
    if ((*head)==NULL){
    head = &node;
    }
    else if((*head)->next!=NULL){
        struct lnode *temp = *head;
        node->next = *head;
    }else if(*head!=NULL&&(*head)->next==NULL){
        (*head->next) = node;
    }
    }

通过 valgrind 运行程序会产生以下错误:

==14661== Command: ./testlist
==14661== 
==14661== Invalid write of size 1
==14661==    at 0x4006E3: createNode (in /u/data/u95/testprogs/testlist)
==14661==    by 0x40091C: main (in /u/data/u95/testprogs/testlist)
==14661==  Address 0x51dc0a6 is 0 bytes after a block of size 6 alloc'd
==14661==    at 0x4C2AF5D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64  linux.so)
==14661==    by 0x4006AF: createNode (in /u/data/u95/testprogs/testlist)
==14661==    by 0x40091C: main (in /u/data/u95/testprogs/testlist)
==14661== 
==14661== Use of uninitialised value of size 8
==14661==    at 0x40071C: addNode (in /u/data/u95/testprogs/testlist)
==14661==    by 0x400933: main (in /u/data/u95/testprogs/testlist)
==14661==  Uninitialised value was created by a stack allocation
==14661==    at 0x4008E8: main (in /u/data/u95/testprogs/testlist)
==14661== 
==14661== Invalid read of size 8
==14661==    at 0x40071C: addNode (in /u/data/u95/testprogs/testlist)
==14661==    by 0x400933: main (in /u/data/u95/testprogs/testlist)
==14661==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==14661== 
==14661== 
==14661== Process terminating with default action of signal 11 (SIGSEGV)
==14661==  Access not within mapped region at address 0x0
==14661==    at 0x40071C: addNode (in /u/data/u95/testprogs/testlist)
==14661==    by 0x400933: main (in /u/data/u95/testprogs/testlist)
==14661==  If you believe this happened as a result of a stack
==14661==  overflow in your program's main thread (unlikely but
==14661==  possible), you can try to increase the size of the
==14661==  main thread stack using the --main-stacksize= flag.
==14661==  The main thread stack size used in this run was 8388608.

我是 C 新手,我不确定为什么会抛出这些错误。有人可以帮忙吗?

最佳答案

以下内容肯定是错误的:

node->word = malloc(sizeof(char)*strlen1);
...
node->word[strlen1] = '\0';

您为单词分配了 strlen1 个字节,所以您的意思可能是:

node->word[strlen1 - 1] = '\0';

请注意,您甚至不需要编写空字节,因为 strcpy 会为您完成此操作。目前尚不清楚代码中还有哪些其他错误,并且在修复此错误之前并不值得查看。

关于c - C 中的 malloc 和内存泄漏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14914269/

相关文章:

c - 什么是双关语?类型双关如何与 C 中的 union 一起使用?

swift - 为什么存储子字符串可能会导致 Swift 中的内存泄漏?

ruby-on-rails - 错误 500 导致中间件内存膨胀/泄漏

c - 带有 GMP 变量的链表

c - #define 和 && 有问题吗

c - 无论如何,VLA 的意义何在?

java - 对(可能的)Android 内存泄漏一无所知

c++ - 如何从C++中的链表中提取列表

c++ - 具有返回类型 node* 的函数与 C++ 中的 OOP 结合使用

Ctrl + z : why it resets count in C program that counts key strokes