内存管理和std :: allocator [英] memory management & std::allocator

查看:105
本文介绍了内存管理和std :: allocator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看代码时,我看到一个类(称为"map")使用的丑陋"结构,我有一个包含"data"类的向量:

In reviewing my code I see some "ugly" structure I use, in a class (called "map") I have a vector which contains a "data" class:

std::vector<PointerToHUGEClass> vector;

其中PointerToHUGEClass就像名称所描述的那样. (尽管指向的对象也归map类所有,并在构造函数中使用"new"参数创建).这一切都很好(目前).但是我仍然觉得这更像是一种解决方法.

Where PointerToHUGEClass is just like the name describes. (though the object pointed too is also owned by the map class, and created with the "new" parameter in the constructor). This works all good (at the moment). However I still feel it is more of a work-around.

我仅使用"PointerToHUGEClass"而不是"HUGEClass"的原因是因为我想确保未从堆栈中声明该对象.但是,这是在我了解分配器之前完成的.现在,我觉得分配器的任务差不多是确保不从堆栈中声明内存.

The only reason I am using a "PointerToHUGEClass" instead of just "HUGEClass", is because I wanted to make sure the object is not declared from the stack. This was made however before I understood allocaters. Now I feel it is more or less the task of the allocator to ensure the memory isn't declared from the stack.

我的问题:

  • 我是否可以假设分配器负责项目中的内存管理? (并确保从stack/freestore/heap/whatever中声明它)
  • std :: allocator有什么作用? -它是从堆栈还是从堆中声明?
  • (从上一个问题开始):如果我将堆栈中声明的项目复制到数据结构中,它是否仍在堆中声明?

再次感谢, paul23

Thanks again, paul23

推荐答案

  • 我是否可以假设分配器负责这些项的内存管理? (并确保从stack/freestore/heap/whatever中声明它)
  • Am I correct in assuming the allocator is responsible for the memory management from the items? (And making sure it is declared from stack/freestore/heap/whatever)

不,您不是.分配器只是在newdelete上涂糖,通常负责决定位置的分配位置.调用allocatedeallocateconstructdestruct的责任在于其用户(在这里是std::vector).从您的角度来看,它将是自动的,这毕竟很重要.

No you are not. The allocator is just sugar coating over new and delete and in general responsible of deciding where the memory will be allocated. The responsibility of calling allocate, deallocate, construct and destruct is to its users (which means here, std::vector). From your point of view, it'll be automatic, which is what matters here after all.

  • std :: allocator有什么作用? -它是从堆栈还是从堆中声明?
  • What does the std::allocator do? - Does it declare from the stack, or from the heap?

强制

std::allocator使用::operator new(size_t)进行分配,因此它取决于全局new运算符的定义.通常,这意味着堆.堆栈用于具有自动存储期限的对象.

std::allocator is mandated to allocate using ::operator new(size_t), thus it depends on the definition of the global new operator. Generally, this means the heap. The stack is for object with automatic storage duration.

  • (从上一个问题开始):如果我将堆栈中声明的项目复制到数据结构中,它是否仍在堆中声明?
  • (follow up from previous question): if I copy an item declared in the stack to the datastructure is it still declared in the heap?

如果复制项目,则会在复制位置分配一个副本.这意味着将项目从堆栈复制到堆.然后,您有该对象的两个副本,这意味着一个副本上的更改不会反映在另一个副本上.

If you copy an item, then a copy is allocated where you copy it. Here it means copying an item from the stack to the heap. You then have two copies of the object, meaning that changes on one copy are not reflected on the other.

请注意,我们正在谈论的是默认复制模式,即复制.如果您复制一个对象,它将被完全复制.如果您复制一个指针,则仅复制该指针,而不复制所指向的数据.

Beware though, that we are talking about the default copying mode, that is a shallow copy. If you copy an object, it'll get copied all fine; if you copy a pointer, only the pointer will be copied, not the data pointed to.

这篇关于内存管理和std :: allocator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆