0

这里我做了一个简单的测试来检查堆栈上的元素是否可以被删除。

// This program test whether an object is dynamically allocated and passed as a parameter to a function , \
//free()ed in that function , then would it really get free()ed or still existing

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

typedef struct node
{
    int data ;
    struct node *next;
} node ;

void deleteElement(int *p)
{

    free(p);
}

void deleteNode(node *node)
{
    free(node);
}

int main()
{
    int i = 10 ;
    int *p = &i ;

    node *parent = (node *)malloc(sizeof(node));
    parent->data = 10;

    node *child = (node *)malloc(sizeof(node));
    parent->next = child ;

    deleteElement(p);
    printf("\np : %d",*p);

    deleteNode(parent);
    printf("\nparent node: %d",parent->data);
    printf("\nchild node : %d",child->data);
    return 0 ;
}

但我收到以下错误

codejack@ubuntu:~/Code::Blocks/ProgrammingTests$ ./a.out 
*** glibc detected *** ./a.out: free(): invalid pointer: 0x00007fff9e9fa284 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f34da65cb96]
./a.out[0x4005c4]
./a.out[0x400635]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f34da5ff76d]
./a.out[0x4004c9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 07:00 383254                             /home/codejack/Code::Blocks/ProgrammingTests/a.out
00600000-00601000 r--p 00000000 07:00 383254                             /home/codejack/Code::Blocks/ProgrammingTests/a.out
00601000-00602000 rw-p 00001000 07:00 383254                             /home/codejack/Code::Blocks/ProgrammingTests/a.out
0190a000-0192b000 rw-p 00000000 00:00 0                                  [heap]
7f34da3c8000-7f34da3dd000 r-xp 00000000 07:00 197806                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f34da3dd000-7f34da5dc000 ---p 00015000 07:00 197806                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f34da5dc000-7f34da5dd000 r--p 00014000 07:00 197806                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f34da5dd000-7f34da5de000 rw-p 00015000 07:00 197806                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f34da5de000-7f34da793000 r-xp 00000000 07:00 700468                     /lib/x86_64-linux-gnu/libc-2.15.so
7f34da793000-7f34da992000 ---p 001b5000 07:00 700468                     /lib/x86_64-linux-gnu/libc-2.15.so
7f34da992000-7f34da996000 r--p 001b4000 07:00 700468                     /lib/x86_64-linux-gnu/libc-2.15.so
7f34da996000-7f34da998000 rw-p 001b8000 07:00 700468                     /lib/x86_64-linux-gnu/libc-2.15.so
7f34da998000-7f34da99d000 rw-p 00000000 00:00 0 
7f34da99d000-7f34da9bf000 r-xp 00000000 07:00 700456                     /lib/x86_64-linux-gnu/ld-2.15.so
7f34daba5000-7f34daba8000 rw-p 00000000 00:00 0 
7f34dabbc000-7f34dabbf000 rw-p 00000000 00:00 0 
7f34dabbf000-7f34dabc0000 r--p 00022000 07:00 700456                     /lib/x86_64-linux-gnu/ld-2.15.so
7f34dabc0000-7f34dabc2000 rw-p 00023000 07:00 700456                     /lib/x86_64-linux-gnu/ld-2.15.so
7fff9e9db000-7fff9e9fc000 rw-p 00000000 00:00 0                          [stack]
7fff9e9ff000-7fff9ea00000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

实际上我在实现删除树时被卡住了。我使用删除了叶节点free(),现在父节点将成为叶节点,并使用递归删除这些节点。但问题是叶节点实际上并没有被删除,它仍然存在。而且我在删除树时遵循的方法如下

void deleteTree(struct node *root)
{
    if(root->left == NULL && root->right == NULL)
    {
       free(root);
    }
    else
    {
       if(root->left != NULL)
            deleteTree(root->left);
       if(root->right != NULL)
            deleteTree(root->right);
    }
}

该方法只删除了叶子节点,并没有删除相应的父节点。在 XCode 中调试后,我发现叶子节点没有被删除,它们仍然存在。 那么为什么这个方法在我做的测试中显示任何错误???

4

2 回答 2

1

您不能free()指向未分配内存的指针。在您的情况下,您正在尝试释放有效堆栈的内存。

void deleteElement(int *p)
{

    free(p);
}

int main()
{
    int i = 10 ;
    int *p = &i ;
    ....
    deleteElement(p);
    ...
}

在方法中,您应该在 ed之后适当地deleteTree()重置->leftor ,否则它将尝试访问无效/释放的内存。->rightfree()

void deleteTree(struct node *root)
{
    if(root->left == NULL && root->right == NULL)
    {
       free(root);
    }
    else
    {
       if(root->left != NULL)
       {
            deleteTree(root->left);
            root->left = NULL;
       }
       if(root->right != NULL)
       {
            deleteTree(root->right);
            root->right = NULL;
       }
    }
}
于 2013-06-16T07:32:29.730 回答
1

三个明显的错误:

(1)deleteElement(p)最终调用free()未分配的指针malloc()

(2) 此代码:

deleteNode(parent);
printf("\nparent node: %d",parent->data);

在变量parent被删除后引用它。

(3) 在deleteTree()中,释放子节点后,保留对它们的引用,这样就永远不会删除非叶节点。

于 2013-06-16T08:51:17.823 回答