简体   繁体   English

C-无法释放在链表结构中分配的内存

[英]C - Unable to free memory allocated within a linked list structure

Consider the following code snippet 考虑以下代码片段

  struct node {
    char *name;
    int m1;
    struct node *next;
    };

    struct node* head = 0; //start with NULL list

    void addRecord(const char *pName, int ms1)
    {   
        struct node* newNode = (struct node*) malloc(sizeof(struct node)); // allocate node

        int nameLength = tStrlen(pName);
        newNode->name = (char *) malloc(nameLength);
        tStrcpy(newNode->name, pName);

        newNode->m1 = ms1;
        newNode->next = head; // link the old list off the new node
        head = newNode;
    }

    void clear(void)
    {
        struct node* current = head;
        struct node* next;
        while (current != 0) 
        {
            next = current->next; // note the next pointer
    /*      if(current->name !=0)
            {
                free(current->name);
            }
    */
            if(current !=0 )
            {
                free(current); // delete the node
            }
            current = next; // advance to the next node
        }
        head = 0;
    }

Question: I am not able to free current->name, only when i comment the freeing of name, program works. 问题:仅当我评论释放名称时,我才能释放current-> name程序正常运行。 If I uncomment the free part of current->name, I get Heap corruption error in my visual studio window. 如果取消注释current-> name的自由部分,则会在Visual Studio窗口中出现堆损坏错误。 How can I free name ? 如何命名?

Reply: 回复:

@all,YES, there were typos in struct declaration. @all,是的,结构声明中有拼写错误。 Should have been char* name, and struct node* next. 应该是char *名称,然后是struct node *。 Looks like the stackoverflow editor took away those two stars. 看起来stackoverflow编辑器夺走了这两颗星。

The issue was resolved by doing a malloc(nameLength + 1). 通过执行malloc(nameLength + 1)解决了该问题。 However,If I try running the old code (malloc(namelength)) on command prompt and not on visual studio, it runs fine. 但是,如果我尝试在命令提示符下而不是在Visual Studio上运行旧代码(malloc(namelength)),它将运行良好。 Looks like, there are certain compilers doing strict checking. 看起来,某些编译器在进行严格的检查。

One thing that I still do not understand is , that free does not need a NULL termination pointer, and chances to overwrite the allocated pointer is very minimal here. 我仍然不了解的一件事是,free不需要NULL终止指针,并且这里覆盖已分配指针的机会非常小。

user2531639 aka Neeraj user2531639又名Neeraj

This is writing beyond the end of the allocated memory as there is no space for the null terminating character, causing undefined behaviour: 这是在分配的内存末尾进行写操作,因为空终止字符没有空间,从而导致未定义的行为:

newNode->name = (char *) malloc(nameLength);
tStrcpy(newNode->name, pName);

To correct: 纠正:

newNode->name = malloc(nameLength + 1);
if (newNode->name)
{
    tStrcpy(newNode->name, pName);
}

Note calling free() with a NULL pointer is safe so checking for NULL prior to invoking it is superfluous: 请注意,使用NULL指针调用free()是安全的,因此在调用它之前检查NULL是多余的:

free(current->name);
free(current);

Additionally, I assume there are typos in the posted struct definition (as types of name and next should be pointers): 另外,我假设发布的struct定义中存在拼写错误(因为namenext类型应该是指针):

struct node {
    char* name;
    int m1;
    struct node* next;
};

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

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