简体   繁体   English

我需要/可以释放 C 中的空指针吗?

[英]Do I need to/can I free a void pointer in C?

I have a void pointer as a parameter for a function. It is currently pointing to an int.我有一个 void 指针作为 function 的参数。它当前指向一个 int。 When I try to free it, it returns a bus error.当我尝试释放它时,它返回总线错误。 Should I be freeing void pointers?我应该释放空指针吗? If so, how do I do so?如果是这样,我该怎么做?

You have to answer two questions first:你必须先回答两个问题:

  • Was it previously allocated with a malloc family function (eg calloc )?它之前是否分配了malloc系列 function(例如calloc )?
  • Did you inherit ownership of it when making the function call?您在拨打 function 时是否继承了它的所有权?

If the answer to both of those is "Yes", then it's at your discretion, though presumably you'd do it at the appropriate time and place to avoid "use after free" type bugs.如果这两个问题的答案都是“是”,则由您自行决定,但您可能会在适当的时间和地点这样做以避免“免费后使用”类型的错误。

When you inherit ownership of a pointer you inherit the responsibility for calling free() when you're done using it, or passing on ownership to another part of your code.当您继承指针的所有权时,您继承了在使用完它时调用free()的责任,或者将所有权传递给代码的另一部分。 If you fail in this responsibility you have memory leaks.如果您未能履行此职责,您将面临 memory 次泄漏。

As a general rule you should never free() a pointer unless you know with certainty that's what you're supposed to do, and you're allowed to do it.作为一般规则,你永远不应该free()一个指针,除非你确定那是你应该做的,并且你被允许这样做。

Some functions return pointers to things that you do not own, and even if you did, they're not valid for free() because they may be offset somehow.一些函数返回指向您不拥有的东西的指针,即使您拥有,它们对free()也无效,因为它们可能以某种方式被抵消。 Only the original pointer returned from the malloc -type function can be used with free() .只有从malloc类型 function 返回的原始指针可以与free()一起使用。

For example:例如:

void* getBuffer(size_t size) {
  globalBufferOffset += size;
  return &globalBuffer[globalBufferOffset];
}

This returns a pointer in the middle of some structure that may or may not be dynamically allocated.这会返回某个结构中间的指针,该结构可能动态分配也可能不动态分配。 You don't own it.你不拥有它。 You should not call free() on it.你不应该在上面调用free()

Read the documentation very carefully to understand your responsibilities.仔细阅读文档以了解您的职责。 You may need to call a special free-type function when you're done with the pointer.完成指针后,您可能需要调用特殊的自由类型 function。 You may not.你不可以。 You may need to pay attention to thread safety.您可能需要注意线程安全。 There's a lot of things that can be going on here you need to be aware of before making a decision.在做出决定之前,您需要了解这里可能发生的很多事情。

If the pointer is allocated by malloc or something like that, you have to free.如果指针由malloc或类似的东西分配,则必须释放。 Because, the malloc function returns the void * pointer, so YES, you can/need to free this pointer.因为, malloc function 返回void *指针,所以是的,你可以/需要释放这个指针。

There are some cases you should not free, for example, the code below:有些情况你不应该释放,例如下面的代码:

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

int main()
{
    void *p1, *p2, *p3;
    int x, y;

    p1 = &x;
    p2 = malloc(sizeof(int));
    p3 = malloc(10);
    *(int*)p2 = y;
    p3 = "abc";

    free(p1); // it raises the fault because p1 is not allocated by malloc 
    free(p2); // it's OK
    free(p3); // it raises also the fault because p3 points to string literal

    return 0;
}

You need a corresponding delete each new or a free for each malloc .你需要一个相应的delete each new或一个free for each malloc If you never wrote a new or a malloc you don't need a free.如果您从未写过新的或 malloc,则不需要免费。

Some libraries have their own version of new and free.一些图书馆有自己的新版本和免费版本。 They would also have corresponding methods and hopefully some documentation.他们也会有相应的方法,并希望有一些文档。 Like SDL_CreateRGBSurface and SDL_FreeSurface .SDL_CreateRGBSurfaceSDL_FreeSurface一样。

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

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