The problem with realloc is that it can move existing data to a different range of contiguous addresses. If you need to do this, given that the C function copies the data without any restrictions on the life of the C ++ object:
- Constructors
- copy / move not used
- destructors are not subsequently called for source objects
This can lead to fatal consequences - for example, when roaming objects contain pointers / links that remain pointers to addresses in the freed memory area.
Unfortunately, the usual malloc implementations do not allow you to use a callback that allows you to replace the code for copying content with your own C ++ - a safe implementation option. If you decide, you can try to find a more flexible malloc library, but it is unlikely to be worth the hassle and risks.
Therefore, in the general case, you should use new to change your capacity, copy / move each object, and delete descendants.
If you are sure that your data is simple enough that moving memcpy style will not lead to adverse consequences, you can use realloc (at your own risk).
source share