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?
Yes,
reallocis what you are looking for. Note that it won’t work withnew, you will have to usemalloc(or, say,calloc). Also, sometimes it is just impossible to extend memory, soreallocwill 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.