我正在尝试创建一个简单的内存管理器来熟悉这个概念,我已经为全局new和delete提供了覆盖,当我注意到单个动态字符串分配时,我开始搞乱构造一些对象,我似乎打了两次新的。第一次new被命中,它甚至在字符串构造函数被调用之前,奇怪的是,这是两个分配中较大的一个,第二次被命中时,调用来自std :: string(basic_string)构造函数。 / p>
我想知道两个新的是什么。具体来说,我在这种情况下关心它,因为每个新的都创建了自己的分配标题,如果我使用一个简单的内存管理器,我会对我将要介绍的开销产生学术上的好奇心。
相关代码:
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();
}
运行时的输出
STARTING MEMORY MANAGEMENT TESTING
******************* Creating DATA *******************
[New] 28
[New] 8
[Delete] 00D88C18
[Delete] 00D88BC8
******************* Creating LORE *******************
[New] 28
[New] 8
[Delete] 00D88C18
[Delete] 00D88BC8
运营商新增和删除
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);
}
}
'GetDefaultHeap'方法只获取静态分配的堆数组中的第一个元素。
为mallocs分配足够大小和标题的内存,并在标头偏移后返回正确的起始地址。 Deallocate从它获取的内存地址中减去标头偏移量并释放该内存 (如果他们有帮助,我可以发布这些方法,它看起来似乎太多了代码)
答案 0 :(得分:6)
new std::string()
这需要两个分配:一个用于std::string
对象,另一个用于其底层内存缓冲区。
如果你写了
std::string s;
你会看到它调用new
一次。
答案 1 :(得分:1)
作为@YSC pointed out,这就是为什么有两次调用itself
的原因。第一个 def picturables
unless contact.nil?
Picturable.where('(picturable_id = ? AND picturable_type = ?) OR (picturable_id = ? AND picturable_type = ?)', id, 'Event', contact.id, 'Contact')
else
super
end
end
def pictures
Picture.where(id: picturables.map(&:picture_id))
end
分配new
对象并返回指向它的指针,但new
对象在其中也有一个指针,当它构造时,它也调用std::string
。
std::string
此代码的输出是
new