Search code examples
c++memorynew-operatorrealloc

new and reallocating memory


new[] doesn't support change the size of allocated memory, does it?

For example, I allocated memory with new[] for buffer (array of char) and I want to get the additional memory.

Should I use malloc/realloc instead in this case?


Solution

  • new does not support that.

    You can mimic the behavior of realloc manually using new, delete and memcpy.

    By the way, you should always use the same family of methods i.e. either malloc / realloc / free or new / delete, and not mix realloc with new / delete. See for example the FMM errors reported by Purify.