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

Phe*_*das 140 c++ heap stack stl vector

以下所有陈述都是正确的吗?

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. 
Run Code Online (Sandbox Code Playgroud)

如何存储在内部分配Typevector或任何其他STL容器?

Fre*_*Foo 201

vector<Type> vect;
Run Code Online (Sandbox Code Playgroud)

vector在堆栈上分配,即头信息,但在免费商店("堆")上分配元素.

vector<Type> *vect = new vector<Type>;
Run Code Online (Sandbox Code Playgroud)

在免费商店分配所有东西.

vector<Type*> vect;
Run Code Online (Sandbox Code Playgroud)

将分配vector堆栈和免费商店上的一堆指针,但这些点由你如何使用它们决定(你可以将元素0指向免费商店,将元素1指向堆栈,比如说).

  • 但我有一个场景,当Type在第二个声明上变大时,我遇到了分段错误.我假设这是因为在堆栈上分配了Type. (3认同)
  • @Phelodas:没有看到你的代码,这是不可能评估的.请打开一个新问题. (3认同)
  • @flyrain:向量清理后自己.阅读[RAII](https://en.wikipedia.org/wiki/RAII). (3认同)
  • 关于`vector <Type> vect;`因为元素在堆上,并且标题信息在堆栈上,当从内存中删除标题信息时,如函数返回,元素内存会发生什么?是否使用标题信息回收它们?如果不是,这会导致内存泄漏吗? (2认同)

Fle*_*exo 23

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

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

这是真的,除了最后一部分(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);
  }
Run Code Online (Sandbox Code Playgroud)

同样:

 vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack
Run Code Online (Sandbox Code Playgroud)

除了最后一部分,它是真的,有一个类似的反例:

  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);
  }
Run Code Online (Sandbox Code Playgroud)

对于:

vector<Type*> vect; //vect will be on stack and Type* will be on heap. 
Run Code Online (Sandbox Code Playgroud)

这是事实,但请注意,Type*指针将在堆上,但Type它们指向的实例不必是:

  int main() {
    vector<Type*> bar;
    Type foo;
    bar.push_back(&foo);
  }
Run Code Online (Sandbox Code Playgroud)

  • @Nerdtron - IIRC在一些小型微控制器上你有一个调用堆栈,它可以在最后一次调用时存储除PC(程序计数器)以外的任何内容,为RET做好准备.因此,您的编译器可能会选择将"自动存储"(对于非递归函数)放置在与执行流程几乎没有关系的固定位置.它可以非常明智地将整个程序弄平.即使对于递归的情况,你可以有一个"每个函数的堆栈"策略或一个单独的堆栈用于自动变量和返回地址,这使得短语"堆栈"有点无意义. (3认同)

jpa*_*cek 22

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

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

vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack
Run Code Online (Sandbox Code Playgroud)

不.与上面相同,只是vector类将在堆上.

vector<Type*> vect; //vect will be on stack and Type* will be on heap. 
Run Code Online (Sandbox Code Playgroud)

vect将在堆栈上,它的项目(指针Type)将在堆上,你无法分辨Type指针指向的位置.可能在堆栈上,可能在堆上,可能在全局数据中,可能无处(即NULL指针).

实际上,实现可以完全在堆栈上存储一些向量(通常是小尺寸的).不是我知道任何这样的实现,但它可以.

  • 对于所有 STL 容器还是仅 std::vector 都是如此? (2认同)

Bin*_*ngo 5

矢量有一个内部allocator负责分配/解除分配heap内存vector element。因此,无论您如何创建向量,它element总是分配在heap. 至于矢量的元数据,这取决于您创建它的方式。