#include <iostream>
#include <cstring>
#include <cstdlib>
int main() {
// (1)
constexpr static size_t table_size = 20;
int * table1 = new int[table_size];
// table usage...
// allocate new table
int * newTable1 = new int[table_size * 2];
// copy data from old table to new table
memcpy(newTable1, table1, table_size * sizeof(table1[0]));
// delete old table
delete [] table1;
// delete old table
delete [] newTable1;
// (2)
int * table2 = new int[table_size];
// mem realloc and copy data
int * newTable2 = static_cast<int *>(realloc(table2, table_size * 2 * sizeof(table2[0])));
if(newTable2 == NULL) {
std::cerr << "realloc() error\n";
exit(EXIT_FAILURE);
}
// new table usage
// delete new table
delete [] newTable2;
return EXIT_SUCCESS;
}
Masz tu 2 sposoby dość niskopoziomowe.
PS. W drugim sposobie jest spora szansa że dostaniesz ten sam wskaźnik i przez to spadnie chwilowe zużycie pamięci. Czasem może to mieć znaczenie :-)