简体   繁体   English

std:map析构函数是否调用了Key Destructors以及Value Destructors?

[英]Does std:map Destructor call Key Destructors as well as Value Destructors?

For example, does the following leak? 例如,以下泄漏?

Foo ( )
{
   std:map<std:string, int> myMap;
   myMap[std::string("Bar")] = 2983;
}

I believe it does not leak but can't find specific documentation on this point. 我相信它没有泄漏,但在这一点上找不到具体的文件。

Yes, map destructor map::~map() will call destructor for every key and value it manages and free memory. 是的,map析构函数map::~map()将为它管理的每个键和值调用析构函数并释放内存。

§ 23.2.1 Table 96 — Container requirements (continued) §23.2.1表96 - 集装箱要求(续)

(&a)->X() void 
the destructor is applied to every element of a; all the memory is deallocated.

Yes, it certainly does. 是的,确实如此。 This is pretty standard stuff in C++, and basically everything in the standard library and STL works this way--destructors are always called unless you're storing raw pointers. 这是C ++中相当标准的东西,基本上标准库和STL中的所有东西都是这样工作的 - 除非你存储原始指针,否则总是调用析构函数。

You are not allocating any memory dynamically using new . 您没有使用new动态分配任何内存。 All the variables are allocated on the stack. 所有变量都在堆栈上分配。 I can't see any memory leaking here. 我看不到任何内存泄漏。

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

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