Vid*_*ohi 3 c++ memory memory-management
我正在尝试创建一个简单的内存管理器来熟悉这个概念,我已经为全局new和delete提供了覆盖,并且刚开始搞乱构造一些对象,当我注意到单个动态字符串分配时,我似乎打了两次新的.第一次new被命中,它甚至在字符串构造函数被调用之前,奇怪的是,这是两个分配中较大的一个,第二次被命中时,调用来自std :: string(basic_string)构造函数.
我想知道两个新的是什么.具体来说,我在这种情况下关心它,因为每个新的都创建了自己的分配标题,如果我使用一个简单的内存管理器,我会对我将要介绍的开销产生学术上的好奇心.
相关代码:
class DumbDataType
{
std::string m_name;
};
int main()
{
printf("STARTING MEMORY MANAGEMENT TESTING \n");
printf("******************* Creating DATA ******************* \n");
std::string* data = new std::string();
delete data;
printf("******************* Creating LORE ******************* \n");
DumbDataType * lore = new DumbDataType();
delete lore;
getchar();
}
Run Code Online (Sandbox Code Playgroud)
运行时输出
STARTING MEMORY MANAGEMENT TESTING
******************* Creating DATA *******************
[New] 28
[New] 8
[Delete] 00D88C18
[Delete] 00D88BC8
******************* Creating LORE *******************
[New] 28
[New] 8
[Delete] 00D88C18
[Delete] 00D88BC8
Run Code Online (Sandbox Code Playgroud)
运营商新增和删除
void * operator new(std::size_t size, MemoryManagement::Heap* heap)
{
assert(heap != nullptr);
assert(size > 0);
return heap->Allocate(size);
}
void * operator new(std::size_t size)
{
printf("[New] %i \n", size);
return operator new (size, MemoryManagement::HeapFactory::GetDefaultHeap());
}
void operator delete (void * memoryLocation)
{
if (memoryLocation != NULL)
{
printf("[Delete] %p \n", memoryLocation);
MemoryManagement::Heap::Deallocate(memoryLocation);
}
}
Run Code Online (Sandbox Code Playgroud)
'GetDefaultHeap'方法只获取静态分配的堆数组中的第一个元素.
为大小和标题分配mallocs足够的内存,并在标头偏移后返回正确的起始地址.Deallocate从它获取的内存地址中减去头部偏移量并释放该内存(如果它们有帮助,我可以发布这些方法,它看起来似乎太多代码)
new std::string()
Run Code Online (Sandbox Code Playgroud)
这需要两个分配:一个用于std::string对象,另一个用于其底层内存缓冲区.
如果你写的
std::string s;
Run Code Online (Sandbox Code Playgroud)
你会看到它调用new一次.