简体   繁体   English

分配的内存在堆栈或堆中

[英]Allocated memory is in the stack or heap

Im declaring a static vector, and allocating/contructing the object within a function (create). 我声明一个静态向量,并在一个函数内分配/构造对象(创建)。 I want to know if the memory allocated was in the heap or in the stack. 我想知道分配的内存是在堆中还是在堆栈中。 Im confused 我糊涂了

// Example program
#include <iostream>
#include <vector>

using namespace std;


typedef std::vector<int> vector1Int;

static vector1Int hello;

void create(){
    hello = vector1Int(8,12);
}

int main()
{

  create();

  return 0;
}

I want to know if the memory allocated was in the heap or in the stack. 我想知道分配的内存是在堆中还是在堆栈中。 Im confused 我糊涂了

After the line 下线后

hello = vector1Int(8,12);

is executed, you have a mix of objects from couple of places. 执行后,您会从几个地方混合使用对象。

  1. hello always lives in the program's global state. hello总是生活在程序的全局状态。 It's neither heap memory nor stack memory. 它既不是堆内存也不是堆栈内存。

  2. The elements of hello live in memory that is used by the allocator to allocate memory for objects. hello的元素位于内存中,分配器使用该内存为对象分配内存。 In most cases, the default allocator, std::allocator , uses dynamically allocated memory. 在大多数情况下,默认分配器std::allocator使用动态分配的内存。 But it can also use global memory. 但是它也可以使用全局内存。

PS 聚苯乙烯

Dynamically allocated memory is commonly referred to as heap memory but the standard does not use that term. 动态分配的内存通常称为堆内存,但标准不使用该术语。

Possibly neither. 可能两者都不是。 The precise terms are 准确的术语是

  • static storage: for data that exists as long as the process exists; 静态存储:用于存在过程的数据;
  • automatic storage: for data that is allocated and fred as the process enters/exits different scopes; 自动存储:用于在过程进入/退出不同范围时分配和存储的数据;
  • dynamic storage: for data that must be explicitly requested and exists until it is explicitly fred. 动态存储:用于必须明确请求并一直存在直到明确填充的数据。

Usually automatic memory lives in the stack and dynamic storage lives in the heap. 通常,自动内存位于堆栈中,而动态存储位于堆栈中。 But the compiler is completely free to implement all those storage types in whatever way they want, as long as it respects the rules for lifespan. 但是,编译器可以完全自由地以所需的任何方式实现所有这些存储类型,只要它遵守生命周期规则即可。

So: 所以:

static vector1Int hello;

is in the file scope and creates an object of type vector1Int in static storage. 在文件范围内,并在静态存储中创建类型为vector1Int的对象。

And this 和这个

hello = vector1Int(8,12);

will cause std::vector to create room for at least 8 integers. 将导致std::vector为至少8个整数创建空间。 We can usually assume that this will be taken from dynamic storage. 我们通常可以假设这将从动态存储中获取。 However, this is not a rule. 但是,这不是规则。 For instance, you could easily make std::vector use static or automatic memory by implementing your own allocator (not general purpose memory allocator, but STL allocator). 例如,通过实现自己的分配器(不是通用内存分配器,而是STL分配器),可以轻松地使std::vector使用静态或自动内存。

When your program reaches the end of the main function, the destructor of std::vector will be called for hello , and any dynamic memory that hello had requested will be given back to the memory manager. 当程序到达main函数的末尾时,将为hello调用std::vector的析构函数,并且hello请求的任何动态内存都将返回给内存管理器。

The memory for the object hello itself is not fred because it is static. 对象hello本身的内存不是静态的,因为它是静态的。 Instead, it is given back to the OS together with anything else the process used when the process terminates. 相反,它将与进程终止时使用的其他所有进程一起返回给OS。

Now, if hello had been declared as a local variable of create , then the destructor would be called at the end of that function. 现在,如果已将hello声明为create的局部变量,则将在该函数的末尾调用析构函数。 In that case, hello would have been allocated at automatic storage, and would be fred at the end of create . 在这种情况下, hello会在自动存储空间中分配,并在create结束时返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM