简体   繁体   English

释放等于malloc指针的指针

[英]Free a pointer equal to a malloc pointer

I'm having trouble understanding some of the free() behavior. 我在理解某些free()行为时遇到了麻烦。

int* ptr1 = (int*)malloc(sizeof (int)*10);
int* ptr2 = ptr1;
free(ptr2);

Is the free(ptr2) going to delete my array as I want ? free(ptr2)会根据需要删除我的数组?

What if I do : 如果我该怎么办:

int** ptr1 = (int**)malloc(sizeof (int)*10);
int* ptr2 = (int*)malloc(sizeof (int));
*ptr2 = 10;
ptr1[0] = ptr2;
free(ptr1);

Is this code correct ? 此代码正确吗? Will free(ptr1) will also delete the space of ptr2 ? free(ptr1)也会删除ptr2的空间吗?

Thanks 谢谢

Yes and no, respectively. 是和否。

Also note that malloc(sizeof(int)*10); 另请注意, malloc(sizeof(int)*10); in the second example will work, but it will not necessarily allocate space for ten pointers. 在第二个示例中可以使用,但不一定会为十个指针分配空间。


To explain what happens in the first example, after the assignment to ptr2 you have something like this: 为了解释在第一个示例中发生的情况,在分配给ptr2之后,您将获得以下内容:

+------+
| ptr1 | ---\
+------+     \      +----------------------------+
              >---> | memory allocated by malloc |
+------+     /      +----------------------------+
| ptr2 | ---/
+------+

Both variables contain the same value, the pointer returned by malloc , so you can use either of them to access the allocated memory. 这两个变量包含相同的值,即malloc返回的指针,因此您可以使用它们之一来访问分配的内存。

For the second question, what you have is something like this: 对于第二个问题,您所拥有的是这样的:

+---------+---------+---------+-----+
| ptr1[0] | ptr1[1] | ptr1[2] | ... |
+---------+---------+---------+-----+
  |
  |
  v
+----------------------------+
| memory allocated by malloc |
+----------------------------+
  ^
  |
  |
+------+
| ptr2 |
+------+

When you free the memory pointer to by ptr1 that only frees that memory, the memory pointed to by ptr2 is still there and accessible through ptr2 . 当您释放内存指针ptr1 ,只有释放内存,内存指向ptr2仍然存在且可通过ptr2

malloc and free do not know about your data structures. mallocfree不了解您的数据结构。 All they know about is blobs of bytes. 他们所知道的只是字节的斑点。

As a general rule, there should be a one-to-one relationship between your malloc and free calls. 通常,您的mallocfree调用之间应该存在一对一的关系。 (There are plenty of exceptions, but it's a good general rule.) (有很多例外,但这是一个很好的一般规则。)

It is never the case that a single call to free will simultaneously free two separate blocks allocated by two calls to malloc . 从来没有一次调用free会同时释放两次调用malloc分配的两个单独的块。

So, as others have said, the answers to your questions are "Yes" and "No". 因此,正如其他人所说,您的问题的答案是“是”和“否”。

int* ptr1 = (int*)malloc(sizeof(int)*10);
int* ptr2 = ptr1;
free(ptr2);

That is fine, ptr2 contains the same value as ptr1 , and thus the memory you passed to malloc() is free() d. 很好, ptr2包含与ptr1相同的值,因此您传递给malloc()的内存为free() d。

int** ptr1 = (int**)malloc(sizeof(int)*10);
int* ptr2 = (int*)malloc(sizeof(int));
*ptr2=10;
ptr1[0]=ptr2;
free(ptr1);

That is not fine. 那不好 You are storing the value of ptr2 in the ptr1 array, but not freeing it. 您正在将ptr2的值存储在ptr1数组中,但没有释放它。 You want: 你要:

int** ptr1 = (int**)malloc(sizeof(int*)*10);
int* ptr2 = (int*)malloc(sizeof(int));
*ptr2=10;
ptr1[0]=ptr2; 
...
free(ptr1[0]);
free(ptr1);

Note I have also changed the malloc() to allocate 10 pointers to int , not merely ten int s (which may not be the same size). 注意,我还更改了malloc()以分配10个指向int指针,而不仅仅是分配10个int (其大小可能不相同)。

Finally note that you don't need to cast the return value from malloc() in C. I haven't fixed that for you as it is not a problem in itself and is unrelated to your question, but it is (arguably) bad style. 最后请注意,您不需要在C中malloc()的返回值 。我没有为您解决此问题,因为它本身不是问题,并且与您的问题无关,但是(可以说)不好样式。

Is the free(ptr2) going to delete my array as I want ? free(ptr2)是否会根据需要删除我的数组?

I think the term delete cannot be used here. 我认为delete一词在这里不能使用。 The GNU manual says : GNU手册说:

When you no longer need a block that you got with malloc, use the function free to make the block available to be allocated again. 当不再需要使用malloc获得的块时,请使用free函数使该块可再次分配。

However free doesn't set pointer automatically to NULL because the function return type itself is void . 但是free不会将指针自动设置为NULL,因为函数返回类型本身为void Also, 也,

Freeing a block alters the contents of the block. 释放一个块会改变该块的内容。 Do not expect to find any data (such as a pointer to the next block in a chain of blocks) in the block after freeing it. 释放数据后,不要期望在该数据块中找到任何数据(例如,指向链中下一个块的指针)。

I am not spurring a debate on whether you should set the pointer to NULL after freeing it. 我并没有引发关于是否应在释放指针后将其设置为NULL的争论。 However,I felt tempted to post this example which crashes and burns : 但是,我很想发布这个崩溃和烧伤的示例:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int* ptr1 = malloc(sizeof(int)*10);
    int* ptr2 = ptr1;
    free(ptr2);
    if(ptr2!=NULL)
    {
        printf("ptr is %p\n",ptr2); // still printf
        printf("Enter a number for ptr2 :");
        scanf("%d",ptr2);
        printf("Value pointed to by ptr2 : %d\n",*ptr2);
        printf("Value pointed to by ptr1 : %d\n",*ptr1);
    }
    else
        printf("Ptr2 pointed to null");
    printf("Size of ptr1 : %d\n",sizeof(ptr1));

    if(ptr1==NULL)
        printf("ptr1 NULL\n");
    printf("Enter a number for ptr1 :");
    scanf("%d",ptr1);
    printf("Value pointed to by ptr1 : %d\n",*ptr1);
    printf("Value pointed to by ptr2 : %d\n",*ptr2);

    free(ptr1);
    if(ptr1==NULL)
        printf("ptr1 freed\n");

    return 0;
}

Output 产量

ptr is 0x1c92010
Enter a number for ptr2 :4
Value pointed to by ptr2 : 4
Value pointed to by ptr1 : 4
Size of ptr1 : 8
Enter a number for ptr1 :1
Value pointed to by ptr1 : 1
Value pointed to by ptr2 : 1
*** glibc detected *** ./testp: double free or corruption (fasttop): 0x0000000001c92010 ***
Segmentation fault (core dumped)

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

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