分配向量时,它们使用堆上的内存还是堆栈上的内存?

发布于 2024-12-14 05:39:14 字数 444 浏览 0 评论 0原文

以下所有陈述都正确吗?

vector<Type> vect; //allocates vect on stack and each of the Type (using std::allocator) also will be on the stack

vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack

vector<Type*> vect; //vect will be on stack and Type* will be on heap. 

vector 或任何其他 STL 容器中,如何在内部为 Type 分配内存?

Are all of the following statements true?

vector<Type> vect; //allocates vect on stack and each of the Type (using std::allocator) also will be on the stack

vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack

vector<Type*> vect; //vect will be on stack and Type* will be on heap. 

How is the memory allocated internally for Type in a vector or any other STL container?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

つ可否回来 2024-12-21 05:39:15
vector<Type> vect;

将在堆栈上分配向量(即标头信息),但在空闲存储(“堆”)上分配元素。

vector<Type> *vect = new vector<Type>;

分配空闲存储上的所有内容(除了位于堆栈上的 vect 指针)。

vector<Type*> vect;

将在堆栈上分配向量并在自由存储上分配一堆指针,但这些点的位置取决于您如何使用它们(您可以将元素 0 指向自由存储,将元素 1 指向堆栈,说)。

vector<Type> vect;

will allocate the vector, i.e. the header info, on the stack, but the elements on the free store ("heap").

vector<Type> *vect = new vector<Type>;

allocates everything on the free store (except vect pointer, which is on the stack).

vector<Type*> vect;

will allocate the vector on the stack and a bunch of pointers on the free store, but where these point is determined by how you use them (you could point element 0 to the free store and element 1 to the stack, say).

最佳男配角 2024-12-21 05:39:15
向量<类型>向量; // 在堆栈上分配 vect 并且每个类型(使用 std::allocator)也将在堆栈上

不,vect 将位于堆栈上,但它内部用于存储项目的数组将位于堆上。这些项目将驻留在该数组中。

向量<类型> *vect = 新向量<类型>; //在堆上分配vect,每个类型将在堆栈上分配

不会。与上面相同,只是 vector 类也将位于堆上。

向量<类型*>向量; //vect 将位于堆栈上,而 Type* 将位于堆上。 

vect 将位于堆栈上,其项(指向 Type 的指针)将位于堆上,并且您无法判断 Type 将位于何处code> 是指针所指向的。可能在堆栈上,可能在堆上,可能在全局数据中,也可能不在任何地方(即NULL指针)。

顺便说一句,该实现实际上可以将一些向量(通常尺寸较小)完全存储在堆栈上。我不知道有任何这样的实现,但它可以。

vector<Type> vect; //allocates vect on stack and each of the Type (using std::allocator) also will be on the stack

No, vect will be on the stack, but the array it uses internally to store the items will be on the heap. The items will reside in that array.

vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack

No. Same as above, except the vector class will be on the heap as well.

vector<Type*> vect; //vect will be on stack and Type* will be on heap. 

vect will be on the stack, its items (pointers to Type) will be on the heap, and you can't tell where will be the Types the pointers point to. Could be on stack, could be on the heap, could be in the global data, could be nowhere (ie. NULL pointers).

BTW the implementation could in fact store some vectors (typically of small size) on the stack entirely. Not that I know of any such implementation, but it can.

空城旧梦 2024-12-21 05:39:15

假设一个实现实际上有一个栈和一个堆(标准 C++ 不要求有这样的东西),唯一正确的语句是最后一个。

vector<Type> vect;
//allocates vect on stack and each of the Type (using std::allocator) also will be on the stack

这是真的,除了最后一部分(Type 不会在堆栈上)。想象一下:

  void foo(vector<Type>& vec) {
     // Can't be on stack - how would the stack "expand"
     // to make the extra space required between main and foo?
     vec.push_back(Type());
  }

  int main() {
    vector<Type> bar;
    foo(bar);
  }

同样:

 vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack

除了最后一部分之外,

  void foo(vector<Type> *vec) {
     // Can't be on stack - how would the stack "expand"
     // to make the extra space required between main and foo?
     vec->push_back(Type());
  }

  int main() {
    vector<Type> *bar = new vector<Type>;
    foo(bar);
  }

为真,有一个类似的反例:对于:

vector<Type*> vect; //vect will be on stack and Type* will be on heap. 

这是真的,但请注意,Type* 指针将位于堆上,但 Type< /code> 它们指向的实例不必是:

  int main() {
    vector<Type*> bar;
    Type foo;
    bar.push_back(&foo);
  }

Assuming an implementation which actually has a stack and a heap (standard C++ makes no requirement to have such things) the only true statement is the last one.

vector<Type> vect;
//allocates vect on stack and each of the Type (using std::allocator) also will be on the stack

This is true, except for the last part (Type won't be on the stack). Imagine:

  void foo(vector<Type>& vec) {
     // Can't be on stack - how would the stack "expand"
     // to make the extra space required between main and foo?
     vec.push_back(Type());
  }

  int main() {
    vector<Type> bar;
    foo(bar);
  }

Likewise:

 vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack

True except the last part, with a similar counter example:

  void foo(vector<Type> *vec) {
     // Can't be on stack - how would the stack "expand"
     // to make the extra space required between main and foo?
     vec->push_back(Type());
  }

  int main() {
    vector<Type> *bar = new vector<Type>;
    foo(bar);
  }

For:

vector<Type*> vect; //vect will be on stack and Type* will be on heap. 

this is true, but note here that the Type* pointers will be on the heap, but the Type instances they point to need not be:

  int main() {
    vector<Type*> bar;
    Type foo;
    bar.push_back(&foo);
  }
喵星人汪星人 2024-12-21 05:39:15

矢量有一个内部分配器,负责从中为矢量元素分配/释放内存。因此,无论您如何创建向量,其元素始终分配在上。至于矢量的元数据,这取决于您创建它的方式。

vector has an internal allocator which is in charge of allocating/deallocating memories from heap for the vector element. So no matter how you create a vector, its element is always allocated on the heap. As for the vector's metadata, it depends on the way you create it.

池予 2024-12-21 05:39:15

只有这句话是正确的:

vector <Type*> vect; //vect will be on stack and Type* will be on heap.

Type* 指针存储在堆上,因为指针的数量可以动态变化。

在这种情况下,vect 是在堆栈上分配的,因为您将其定义为本地堆栈变量。

Only this statement is true:

vector <Type*> vect; //vect will be on stack and Type* will be on heap.

Type* pointers are stored on heap, because amount of the pointers can change dynamically.

vect in this case is allocated on stack, because you defined it as a local stack variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文