在 C++ 中调整动态数组的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24719440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resizing dynamic array in c++
提问by homegrown
I have some code that is producing unexpected results. Here is the code:
我有一些产生意外结果的代码。这是代码:
#include <iostream>
using namespace std;
int **nums;
int size;
void A(int** arr)
{
int **resize;
resize = new int*[size*2];
for(int i = 0; i < size; i++)
resize[i] = new int(*arr[i]);
cout << endl;
arr = resize;
size *= 2;
delete[] resize;
}
int main()
{
size = 10;
nums = new int*[size];
for(int i = 0; i < size; i++)
nums[i] = new int(i);
for(int i = 0; i < size; i++)
cout << *nums[i] << endl;
A(nums);
cout << endl;
for(int i = (size / 2); i < size; i++)
nums[i] = new int(i);
for(int i = 0; i < size; i++)
cout << *nums[i] << endl;
}
The function A(int** arr) works fine as far as I can tell and actually resizes the array. However, in the last for loop in main(), when the array is printing, the first two elements of the array are not 0 and 1 like it is supposed to be. Here is the result I am getting:
据我所知,函数 A(int** arr) 工作正常,实际上调整了数组的大小。然而,在 main() 的最后一个 for 循环中,当数组打印时,数组的前两个元素不是 0 和 1 像它应该的那样。这是我得到的结果:
0
1
2
3
4
5
6
7
8
9
16331248
16331712
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Those first two ints after the space are different each time the program is executed. After some debugging I found out that the first two elements print correctly until the iterator i=13 in the second to last for loop in main(). Then the first two elements in the array take on some large numbers. I am not sure why this is happening and I have been working on this for a couple of hours now :( Any help is appreciated.
每次执行程序时,空格后的前两个整数都不同。经过一些调试,我发现前两个元素打印正确,直到 main() 中倒数第二个 for 循环中的迭代器 i=13。然后数组中的前两个元素取一些大数。我不确定为什么会发生这种情况,我已经为此工作了几个小时:( 任何帮助表示赞赏。
回答by Remy Lebeau
A()
is not modifying nums
to point at the new array. Even if it were, it is deleting the new array, so nums
would end up pointing at invalid memory. You need to declare the arr
parameter as a reference, and delete the old array instead of the new array:
A()
没有修改nums
指向新数组。即使是这样,它也会删除新数组,因此nums
最终会指向无效内存。您需要将arr
参数声明为引用,并删除旧数组而不是新数组:
void A(int** &arr)
{
int **resize;
resize = new int*[size*2];
for(int i = 0; i < size; i++)
resize[i] = new int(*arr[i]);
cout << endl;
delete[] arr;
arr = resize;
size *= 2;
}
For what you are attempting, I think you have too much indirection. Try removing a level:
对于您正在尝试的内容,我认为您有太多的间接性。尝试删除一个级别:
#include <iostream>
using namespace std;
int *nums;
int size;
void A(int* &arr)
{
int *resize;
resize = new int[size*2];
for(int i = 0; i < size; i++)
resize[i] = arr[i];
cout << endl;
delete[] arr;
arr = resize;
size *= 2;
}
int main()
{
size = 10;
nums = new int[size];
for(int i = 0; i < size; i++)
nums[i] = i;
for(int i = 0; i < size; i++)
cout << nums[i] << endl;
A(nums);
cout << endl;
for(int i = (size / 2); i < size; i++)
nums[i] = i;
for(int i = 0; i < size; i++)
cout << nums[i] << endl;
delete[] nums;
}
Since you are using C++, you should be using a std::vector
instead of a raw array, then you can eliminate A()
altogether:
由于您使用的是 C++,您应该使用 astd::vector
而不是原始数组,然后您可以A()
完全消除:
#include <iostream>
#include <vector>
using namespace std;
vector<int> nums;
int main()
{
nums.resize(10);
for(int i = 0; i < nums.size(); i++)
nums[i] = i;
for(int i = 0; i < nums.size(); i++)
cout << nums[i] << endl;
nums.resize(nums.size()*2);
cout << endl << endl;
for(int i = (nums.size() / 2); i < nums.size(); i++)
nums[i] = i;
for(int i = 0; i < nums.size(); i++)
cout << nums[i] << endl;
}
回答by Benjamin Lindley
First of all, your function, A
, does not resize anything. It prints a newline character to standard output, it multiplies the global size
variable by 2, and then it leaks some memory. That's it.
首先,您的函数A
不会调整任何大小。它将换行符打印到标准输出,将全局size
变量乘以 2,然后泄漏一些内存。就是这样。
Now, because it multiplies size
by 2 (going from 10, to 20), you run into a problem, here:
现在,因为它乘以size
2(从 10 到 20),你遇到了一个问题,在这里:
for(int i = (size / 2); i < size; i++)
nums[i] = new int(i);
Here, you are trying to access elements 10 through 19 of the array which nums
points to. But the array which nums
points to only has 10 elements (numbered 0 through 9), so your code has undefined behavior.
在这里,您试图访问nums
指向的数组的第 10 到 19 号元素。但是nums
指向的数组只有 10 个元素(编号为 0 到 9),因此您的代码具有未定义的行为。