如何在 C++ 中创建一个位于堆上而不是堆栈上的数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/675817/
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
How do I create an array in C++ which is on the heap instead of the stack?
提问by Nick Bolton
I have a very large array which must be 262144 elements in length (and potentially much larger in future). I have tried allocating the array on the stack like so:
我有一个非常大的数组,它的长度必须是 262144 个元素(将来可能会更大)。我尝试在堆栈上分配数组,如下所示:
#define SIZE 262144
int myArray[SIZE];
However, it appears that when I try and add elements past a certain point, the values are different when I try to access them. I understand that this is because there is only a finite amount of memory on the stack, as opposed to the heap which has more memory.
但是,似乎当我尝试添加超过某个点的元素时,当我尝试访问它们时,这些值会有所不同。我知道这是因为堆栈上只有有限数量的内存,而不是具有更多内存的堆。
I have tried the following without much luck (does not compile):
我尝试了以下但没有太多运气(不编译):
#define SIZE 262144
int *myArray[SIZE] = new int[SIZE];
And then I considered using malloc
, but I was wondering if there was a more C++ like way of doing this...
然后我考虑使用malloc
,但我想知道是否有更像 C++ 的方式来做到这一点......
#define SIZE 262144
int *myArray = (int*)malloc(sizeof(int) * SIZE);
Should I just go with malloc
?
我应该一起去malloc
吗?
回答by Reed Copsey
You'll want to use new like such:
你会想要像这样使用 new:
int *myArray = new int[SIZE];
I'll also mention the other side of this, just in case....
我也会提到这一点的另一面,以防万一......
Since your transitioning from the stack to the heap, you'll also need to clean this memory up when you're done with it. On the stack, the memory will automatically cleanup, but on the heap, you'll need to delete it, and since its an array, you should use:
由于您从堆栈转换到堆,您还需要在完成后清理此内存。在堆栈上,内存会自动清理,但在堆上,你需要删除它,因为它是一个数组,你应该使用:
delete [] myArray;
回答by Brian Neal
The more C++ way of doing it is to use vector. Then you don't have to worry about deleting the memory when you are done; vector will do it for you.
更多的 C++ 方法是使用向量。这样你就不用担心完成后删除内存了;矢量会为你做的。
#include <vector>
std::vector<int> v(262144);
回答by Mitch Wheat
new
allocates on the heap.
new
在堆上分配。
#define SIZE 262144
int * myArray = new int[SIZE];
回答by Johannes Schaub - litb
As the number is not necessarily known at compile time, the type is a pointer:
由于在编译时不一定知道数字,因此类型是指针:
int *myArray = new int[262144];
回答by John MacIntyre
I believe
我相信
#define SIZE 262144
int *myArray[SIZE] = new int[262144];
should be
应该
#define SIZE 262144
int *myArray = new int[262144];
or better yet
或者更好
#define SIZE 262144
int *myArray = new int[SIZE];
回答by Wilka
Your syntax for using new
was wrong, it should be:
您使用的语法new
错误,应该是:
int *myArray = new int[262144];
you only need to put the size on the right of the assignment.
你只需要把尺寸放在作业的右边。
However, if you're using C++ you might want to look at using std::vector
(which you will have) or something like boost::scoped_array
to make the the memory management a bit easier.
但是,如果您使用的是 C++,您可能需要考虑使用std::vector
(您将拥有)或类似的东西boost::scoped_array
来使内存管理更容易一些。
回答by Tommy Hui
The reason the first try didn't work is that the syntax is incorrect. Try this:
第一次尝试不起作用的原因是语法不正确。尝试这个:
int *myArray = new int[SIZE];