[英]How can I resize a dynamic array of pointers without using a vector
If I wanted to resize this array:如果我想调整这个数组的大小:
int array[5];
I would do this:我会这样做:
int* temp = new int [n];
...
array = temp;
But, how would I do it if I have this array?但是,如果我有这个数组,我该怎么做?
int *array[5];
Maybe like this?也许像这样?
int** temp = new int* [n];
You CAN'T resize a fixed array, period.您不能调整固定数组的大小,句点。 Its size is constant, determined at compile-time.
它的大小是恒定的,在编译时确定。
You CAN'T assign a new[]
'ed pointer to a fixed array, either.不能将分配
new[]
“编指针到一个固定的阵列,无论是。
So, neither of your examples will work.所以,你的例子都不起作用。
In your 1st example, you would need this instead:在你的第一个例子中,你需要这个:
int* array = new int [5];
And in your 2nd example, you would need this instead:在你的第二个例子中,你需要这个:
int** array = new int* [5];
Either way, don't forget to delete[]
the array
before assigning something new to it, eg:无论哪种方式,在为
array
分配新内容之前不要忘记delete[]
array
,例如:
int* array = new int [5];
...
int* temp = new int [n];
...
delete[] array;
array = temp;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.