实际上两件事情是通常负责:
当请求4个字节(例如)的存储器,您的底层内存分配器(无论由所选operator new(...)
使用)可能会使用一些额外的字节来存储一些簿记信息。看一个很好的解释here
记账信息通常是为什么delete
工作,而不必告诉它所需的内存的原始大小。
例如:
void* Malloc(std::size_t size){
//Allocator searches for free memory
auto FreeList = GetNextFreeNodeOfAtLeast(size + 16);
//Rounds the requested size to the ceil of CPU word size
size = RoundToWordAlignment(size);
//Allocate with an extra 16 bytes for bookkeeping
void* Memory = FreeList->Allocate(size + 16);
//Use the Upper 16bytes... some implementations use lower..
auto info = static_cast<MallocInformation*>(Memory + size);
//Create an Info object so that `Free` or operator delete can use to free memory
new(info) MallocInformation(size, FreeList, ....);
//Return the Memory
return Memory;
}
分配给你的每个存储附加了一些积压信息。内存分配器有很多种不同的工作方式,一些有一些指向内存管理的主结构的指针的簿记信息。
C++标准不要求连续的内存分配是连续的,也不需要指定它们之间有多少内存“间隙”。
实际上,看你提供的输出,你分配的整数是32字节。 –
完全取决于使用什么内存分配算法。分配的块不太可能与所请求的类型大小完全匹配,但至少与对齐,或者像默认分配器一样使用最小内存块。 –
对齐完成后,CPU可以更快地从主内存中读取 – stackptr