C语言 释放'void *'可以吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2182103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it OK to free 'void*'?
提问by CodingLab
Consider:
考虑:
struct foo
{
int a;
int b;
};
void* p = (void*)malloc(sizeof(struct foo));
((foo*)p)->a; // Do something.
free(p); // Is this safe?
回答by Laurence Gonsalves
Yes.
是的。
malloc returns void *and free takes void *, so some of your casts are meaningless, and you're always freeing a void *even if you're starting with some other sort of pointer.
malloc 返回void *和 free take void *,所以你的一些强制转换是没有意义的,void *即使你从其他类型的指针开始,你也总是释放 a 。
回答by Pepor
Yes, it's safe. When allocating memory, the runtime library keeps track of the size of each allocation. When you call free(), it looks up the address, and if it finds an allocation for that address, the correct amount of memory is freed (the block that was allocated at that address).
是的,它很安全。分配内存时,运行时库会跟踪每次分配的大小。当您调用 free() 时,它会查找地址,如果找到该地址的分配,则释放正确数量的内存(在该地址分配的块)。
回答by Jerry Coffin
Yes -- freetakes a pointer to void, so when you call it, the pointer is (implicitly) cast to a pointer to void in any case.
是的 -free需要一个指向 void 的指针,因此当您调用它时,该指针在任何情况下都会(隐式)转换为指向 void 的指针。
The rest of your code isn't quite so safe:
您的其余代码不太安全:
void* p = (void*)malloc(sizeof(foo));
You should notcast the return from malloc (in C). This can cover up the mistake of forgetting to #include <stdlib.h>
你应该不投的回报将通过malloc(C语言)。这可以掩盖忘记的错误#include <stdlib.h>
回答by jamesdlin
Yes. The function prototype for freeis even:
是的。的函数原型为free偶数:
void free(void *ptr);
回答by matt
In C it is perfectly safe, because there are no destructors to call.
在 C 中它是完全安全的,因为没有可调用的析构函数。
The memory system keeps track of the size of allocations.
内存系统会跟踪分配的大小。
In C++ you must delete the same type you new, including using the delete[]operator to delete new'ed arrays.
在 C++ 中,您必须删除与new相同的类型,包括使用delete[]运算符删除新的数组。
This is just to make sure destructors are called.
这只是为了确保调用析构函数。
回答by rickmode
Perhaps it doesn't feelsafe because of the magic happening behind the scenes. The C runtime and/or the OS itself is actively tracking the memory returned by malloc including its size and location. See though it feels like you are passing a typeless pointer back to free(), you in fact passing back a reference to an object the memory manager is actively tracking.
也许是因为幕后发生的魔法而让人感觉不安全。C 运行时和/或操作系统本身正在主动跟踪 malloc 返回的内存,包括其大小和位置。虽然感觉就像是将无类型指针传递回 free(),但实际上您传递回的是对内存管理器正在主动跟踪的对象的引用。
回答by Hogan
yes it is safe.
是的,它是安全的。
回答by Malcolm McLean
Yes, but normally it's a sign of poor design.
是的,但通常这是设计不佳的标志。
malloc() is typically used to allocate buffers (large arrays of the same primitive type) or objects (structs with fields initialised). In both cases, the malloc and the free should match so,
malloc() 通常用于分配缓冲区(相同基本类型的大数组)或对象(具有初始化字段的结构)。在这两种情况下, malloc 和 free 应该匹配,
unsigned char *rgba_buffer = malloc(width * height * 4);
/* Use the buffer here */
free(rgba_buffer);
BITSTREAM *bs = bitstream("bitfile.boin");
/* Use the bitstream here */
destroy_bitstream(bs);
typedef struct
{
FILE *fp;
unsigned char ch;
int index;
} BITSTREAM;
BITSTREAM *bitstream(const char *filename)
{
BITSTREAM *bs = malloc(sizeof(BITSTREAM));
bs->fp = fopen(filename "rb");
/* etc */
return bs;
}
void destroybitstream(BITSTREAM *bs)
{
if(bs)
{
if(bs->fp)
fclose(bs->fp);
free(bs);
}
}
In one case, malloc and free match, and in the other the allocated memory is returned. There are also secondary resources, and the constructor and destructor match. It should be rare to allocate a region of memory, but not know what it is used for. And you shouldn't be interleaving allocations and frees chaotically.
在一种情况下,malloc 和 free 匹配,而在另一种情况下,分配的内存被返回。还有次要资源,构造函数和析构函数匹配。应该很少分配内存区域,但不知道它的用途。而且你不应该无序地交错分配和释放。
Modern C++ tightens this all up with unique pointers which "own" the object. While you can have a unique pointer to void, it would be very rare.
现代 C++ 使用“拥有”对象的独特指针来加强这一切。虽然您可以拥有指向 void 的唯一指针,但这种情况非常罕见。

