使用free()释放new分配的内存空间

时间:2022-03-01 01:02:55

As I understand, delete[] is used to release the memory space allocated by new. free() can also be used to release that memory space. So which type of problems will I have to face when releasing memory using free() in place of delete[]?

据我了解,delete []用于释放new分配的内存空间。 free()也可用于释放该内存空间。那么在使用free()代替delete []释放内存时,我将面临哪些类型的问题?

1 个解决方案

#1


"free() can also be used to release that memory space."

“free()也可以用来释放那个内存空间。”

No, free() cannot be used to release memory allocated with new() or new[].

不,free()不能用于释放用new()或new []分配的内存。

"So which type of problems will I have to face when releasing memory using free() in place of delete[]?"

“那么在使用free()代替delete []释放内存时,我将面临哪些类型的问题?”

Using free() instead of delete[] for memory allocated with new[] is undefined behavior (besides destructor functions of complex objects won't be called), and such may likely end up with a runtime exception.

对new []分配的内存使用free()而不是delete []是未定义的行为(除了不会调用复杂对象的析构函数),这样可能最终会出现运行时异常。

From the POSIX compliant reference (emphasis mine):

从POSIX兼容参考(强调我的):

The free() function shall cause the space pointed to by ptr to be deallocated; that is, made available for further allocation. If ptr is a null pointer, no action shall occur. Otherwise, if the argument does not match a pointer earlier returned by the calloc(), malloc(), posix_memalign(), realloc(), or strdup() function, or if the space has been deallocated by a call to free() or realloc(), the behavior is undefined.

free()函数将导致ptr指向的空间被释放;也就是说,可供进一步分配。如果ptr是空指针,则不会发生任何操作。否则,如果参数与之前由calloc(),malloc(),posix_memalign(),realloc()或strdup()函数返回的指针不匹配,或者如果通过调用free()释放了空间或realloc(),行为未定义。

Any use of a pointer that refers to freed space results in undefined behavior.

任何使用引用释放空间的指针都会导致未定义的行为。

Well, some implementations of new[] or new() might rely on that malloc() function family, others may not. So you can't certainly tell that allocated memory pointer can be safely used with free (even if you don't care about the destructor function call issue).

好吧,new []或new()的一些实现可能依赖于malloc()函数系列,其他实现可能不依赖。因此,您无法确定分配的内存指针可以安全地使用(即使您不关心析构函数调用问题)。


Conclusion:

Usage of delete[] is only well defined to operate on memory that was allocated using new[], delete corresponds to results of new.

delete []的用法仅定义为对使用new []分配的内存进行操作,delete对应于new的结果。

As well usage of free() is only well defined in conjunction for memory that was allocated using the malloc() function family.

同样,free()的使用只能与使用malloc()函数系列分配的内存一起很好地定义。


As far concerning the tag applied to your (original) question, since c doesn't support delete or new asking this for the c language is pretty useless.

至于应用于你的(原始)问题的c标签,因为c不支持删除或新问这个c语言是没有用的。

#1


"free() can also be used to release that memory space."

“free()也可以用来释放那个内存空间。”

No, free() cannot be used to release memory allocated with new() or new[].

不,free()不能用于释放用new()或new []分配的内存。

"So which type of problems will I have to face when releasing memory using free() in place of delete[]?"

“那么在使用free()代替delete []释放内存时,我将面临哪些类型的问题?”

Using free() instead of delete[] for memory allocated with new[] is undefined behavior (besides destructor functions of complex objects won't be called), and such may likely end up with a runtime exception.

对new []分配的内存使用free()而不是delete []是未定义的行为(除了不会调用复杂对象的析构函数),这样可能最终会出现运行时异常。

From the POSIX compliant reference (emphasis mine):

从POSIX兼容参考(强调我的):

The free() function shall cause the space pointed to by ptr to be deallocated; that is, made available for further allocation. If ptr is a null pointer, no action shall occur. Otherwise, if the argument does not match a pointer earlier returned by the calloc(), malloc(), posix_memalign(), realloc(), or strdup() function, or if the space has been deallocated by a call to free() or realloc(), the behavior is undefined.

free()函数将导致ptr指向的空间被释放;也就是说,可供进一步分配。如果ptr是空指针,则不会发生任何操作。否则,如果参数与之前由calloc(),malloc(),posix_memalign(),realloc()或strdup()函数返回的指针不匹配,或者如果通过调用free()释放了空间或realloc(),行为未定义。

Any use of a pointer that refers to freed space results in undefined behavior.

任何使用引用释放空间的指针都会导致未定义的行为。

Well, some implementations of new[] or new() might rely on that malloc() function family, others may not. So you can't certainly tell that allocated memory pointer can be safely used with free (even if you don't care about the destructor function call issue).

好吧,new []或new()的一些实现可能依赖于malloc()函数系列,其他实现可能不依赖。因此,您无法确定分配的内存指针可以安全地使用(即使您不关心析构函数调用问题)。


Conclusion:

Usage of delete[] is only well defined to operate on memory that was allocated using new[], delete corresponds to results of new.

delete []的用法仅定义为对使用new []分配的内存进行操作,delete对应于new的结果。

As well usage of free() is only well defined in conjunction for memory that was allocated using the malloc() function family.

同样,free()的使用只能与使用malloc()函数系列分配的内存一起很好地定义。


As far concerning the tag applied to your (original) question, since c doesn't support delete or new asking this for the c language is pretty useless.

至于应用于你的(原始)问题的c标签,因为c不支持删除或新问这个c语言是没有用的。