+ 2
You should not combine new and realloc in C++. Use new and delete together , or malloc(), calloc(), free() together(C's way). Their allocators could be different and therefore may not result what you really want.
Of course, you can use realloc(), don't use new and delete then. But it's more like a solution in C, not C++.
https://stackoverflow.com/questions/33706528/is-it-safe-to-realloc-memory-allocated-with-new
+ 1
Martin Taylor any idea why C++ provided 'new' in place of malloc/calloc but didn't provide an alternative to realloc?
0
No. Array is fixed size. You can, however, create a new array with different size, and copy the entire array to the new one. Remember to delete the old array.
Or better, use vector.