Memory reallocation - keep my data in place

c++, memory-management

Solution

Yes, `realloc` is what you are looking for. Note that it won't work with `new`, you will have to use `malloc` (or, say, `calloc`). Also, sometimes it is just impossible to extend memory, so `realloc` will try to do it for you, but if it couldn't — it will resort to allocating new memory, copying your contents to a new place and freeing the old memory.

Problem

Assuming I have some array in heap doesn't matter constructed by `malloc` or `new`. I need the most efficient way to enlarge it. I mean if it has enough free space which lying after already allocated data can I keep my data untouched. Is it possible to maintain in C++? Does `realloc` work in such manner?

Original source