Is it safe to free() memory allocated by new? [duplicate]

天涯浪子 提交于 2020-01-02 03:45:49

问题


I'm working on a C++ library, one of whose functions returns a (freshly allocated) pointer to an array of doubles. The API states that it is the responsibility of the caller to deallocate the memory.

However, that C++ library used to be implemented in C and the function in question allocates the memory with malloc(). It also assumes that the caller will deallocate that memory with free().

Can I safely replace the call to malloc() with a call to new? Will the existing client code (that uses free() break if I do so? All I could find so far was the official documentation of free(), which states that

If ptr does not point to a block of memory allocated with [malloc, calloc or realloc], it causes undefined behavior.

But I believe this was written before C++ came along with its own allocation operators.


回答1:


You are not allowed to mix and match malloc and free with new and delete the draft C++ standard refers back to the C99 standard for this and if we go to the draft C++ standard section 20.6.13 C library it says (emphasis mine going forward):

The contents are the same as the Standard C library header stdlib.h, with the following changes:

and:

The functions calloc(), malloc(), and realloc() do not attempt to allocate storage by calling ::operator new() (18.6).

and:

The function free() does not attempt to deallocate storage by calling ::operator delete(). See also: ISO C Clause 7.11.2.

and includes other changes, none of which state that we can use free on contents allocated with new. So section 7.20.3.2 The free function from the draft C99 standard is still the proper reference and it says:

Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.




回答2:


You MUST match calls to malloc with free and new with delete. Mixing/matching them is not an option.




回答3:


As you've heard now, you can't mix them.

Keep in mind that in C++ it's common to have lots of relatively small temporary objects dynamically allocated (for instance, it's easy to write code like my_string + ' ' + your_string + '\n'), while in C memory allocation's typically more deliberate, often with a larger average allocation size and longer lifetime (much more likely someone would directly malloc(strlen(my_string) + strlen(your_string) + 3) for the result without any temporary buffers). For that reason, some C++ libraries will optimise for large numbers of small transient objects. They might, for instance, use malloc() to get three 16k blocks, then use each for fixed-size requests of up to 16, 32 and 64 bytes respectively. If you call delete in such a situation, it doesn't free anything - it just returns the particular entry in the 16k buffer to a C++-library free list. If you called free() and the pointer happened to be to the first element in the 16k buffer, you'd accidentally deallocate all the elements; if it wasn't to the first you have undefined behaviour (but some implementations like Visual C++ apparently still free blocks given a pointer anywhere inside them).

So - really, really don't do it.

Even if it ostensibly works on your current system, it's a bomb waiting to go off. Different runtime behaviour (based on different inputs, thread race conditions etc.) could cause a later failure. Compilation with different optimisation flags, compiler version, OS etc. could all break it at any time.




回答4:


The library should really provide a deallocation function that forwards to the correct function.

In addition to what the others already said (no compatibility guarantee), there is also the possibility that the library is linked to a different C library than your program, and so invoking free() on a pointer received from them would pass it to the wrong deallocation function even if the function names are correct.




回答5:


You must use delete operator to deallocate memory when it is allocated by new operator.




回答6:


malloc() allocates the memory and sends the address of the first block to the assigned pointer variable,in the case of new it allocates the memory and returns the address .it is a convention that when you use a malloc() function we should use delete function and when you are allocating memory with the help of new function the usage of free() function is comfortable.when malloc()it is a convention that we should use the corresponding realloc(),calloc(),delete() functions and similarly,when you use new() function the corresponding free()function is used.



来源:https://stackoverflow.com/questions/22406278/is-it-safe-to-free-memory-allocated-by-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!