C++ 在堆栈和堆上创建对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1598397/
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
Creating array of objects on the stack and heap
提问by Light_handle
Consider the following code:
考虑以下代码:
class myarray
{
int i;
public:
myarray(int a) : i(a){ }
}
How can you create an array of objects of myarray on the stack and how can you create an array of objects on the heap?
如何在堆栈上创建 myarray 的对象数组以及如何在堆上创建对象数组?
回答by GManNickG
You can create an array of objects on the stack?via:
您可以在堆栈上创建一个对象数组吗?通过:
myarray stackArray[100]; // 100 objects
And on the heap?(or "freestore"):
而在堆上?(或“免费商店”):
myarray* heapArray = new myarray[100];
delete [] heapArray; // when you're done
But it's best not manage memory yourself. Instead, use a std::vector:
但最好不要自己管理内存。相反,使用std::vector:
#include <vector>
std::vector<myarray> bestArray(100);
A vector is a dynamic array, which (by default) allocates elements from the heap.??
向量是一个动态数组,它(默认情况下)从堆中分配元素。??
Because your class has no default constructor, to create it on the stack you need to let the compiler know what to pass into the constructor:
因为您的类没有默认构造函数,所以要在堆栈上创建它,您需要让编译器知道要传递给构造函数的内容:
myarray stackArray[3] = { 1, 2, 3 };
Or with a vector:
或者使用向量:
// C++11:
std::vector<myarray> bestArray{ 1, 2, 3 };
// C++03:
std::vector<myarray> bestArray;
bestArray.push_back(myarray(1));
bestArray.push_back(myarray(2));
bestArray.push_back(myarray(3));
Of course, you could always give it a default constructor:
当然,你总是可以给它一个默认的构造函数:
class myarray
{
int i;
public:
myarray(int a = 0) :
i(a)
{}
};
? For the pedants: C++ doesn't really have a "stack" or "heap"/"freestore". What we have is "automatic storage" and "dynamic storage" duration. In practice, this aligns itself with stack allocation and heap allocation.
? 对于学究们:C++ 并没有真正的“堆栈”或“堆”/“自由商店”。我们拥有的是“自动存储”和“动态存储”时长。实际上,这与堆栈分配和堆分配保持一致。
?? If you want "dynamic" allocation from the stack, you'd need to define a max size (stack storage is known ahead of time), and then give vector a new allocator so it uses the stack instead.
?? 如果你想从堆栈中“动态”分配,你需要定义一个最大大小(堆栈存储提前知道),然后给 vector 一个新的分配器,以便它使用堆栈。
回答by Alexander Solovets
Since C++11 std::array<T,size>
is available for arrays allocated on the stack. It wraps T[size]
providing the interface of std::vector
, but the most of the methods are constexpr
. The downside here is that you never know when you overflow the stack.
由于 C++11std::array<T,size>
可用于在堆栈上分配的数组。它包装T[size]
提供 的接口std::vector
,但大多数方法是constexpr
. 这里的缺点是您永远不知道何时溢出堆栈。
std::array<myarray, 3> stack_array; // Size must be declared explicitly.VLAs
For arrays allocated with heap memory use std::vector<T>
. Unless you specify a custom allocator the standard implementation will use heap memory to allocate the array members.
对于使用堆内存分配的数组,请使用std::vector<T>
. 除非您指定自定义分配器,否则标准实现将使用堆内存来分配数组成员。
std::vector<myarray> heap_array (3); // Size is optional.
Notethat in both cases a default constructor is required to initialize the array, so you must define
请注意,在这两种情况下,都需要一个默认构造函数来初始化数组,因此您必须定义
myarray::myarray() { ... }
There are also options to use C's VLAsor C++'s new
, but you should refrain from using them as much as possible, because their usage makes the code prone to segmentation faults and memory leaks.
也有使用 C 的VLA或 C++ 的选项new
,但您应该尽可能避免使用它们,因为它们的使用会使代码容易出现分段错误和内存泄漏。
回答by Tanuj
If you create an array of objects of class myarray ( either on stack or on heap) you would have to define a default constructor.
如果您创建一个 myarray 类的对象数组(在堆栈或堆上),您将必须定义一个默认构造函数。
There is no way to pass arguments to the constructor when creating an array of objects.
创建对象数组时,无法将参数传递给构造函数。
回答by Alok
I know how to create object with out of the default constructor, but only on stack:
我知道如何使用默认构造函数创建对象,但仅限于堆栈:
Suppose you want to create 10 objects for MyArray class with a = 1..10
:
假设您要为 MyArray 类创建 10 个对象a = 1..10
:
MyArray objArray[] = { MyArray[1], MyArray[2]......MyArray[10]}
No need to call the destructor, because they are created in the stack.
不需要调用析构函数,因为它们是在堆栈中创建的。
回答by Sam Daniel T
#include <stdio.h>
class A
{
public:
A(int a){
printf("\nConstructor Called : %d\n",a);
aM = a;
}
~A(){
printf("\ndestructor Called : %d\n",aM);
}
private:
int aM;
};
int main()
{
A **a = new A*[10];
for (int i = 0;i<10;i++)
a[i] = new A(i+1);
for (int i = 0;i<10;i++)
delete a[i];// = new A(i+1);
delete []a;
}