[英]Is the destructor of the local variable always guaranteed to be called when it goes out of scope?
编译器可以进行很多优化(比如内联一些函数),我有点怀疑在我的程序中调用函数(基于OS X的系统监视器)之后,不是所有为局部变量分配的内存都被清除了,所以这就是我要问的原因:标准是否保证局部变量的所有析构函数在它们超出范围时都会被调用?
Yes. 是。 Per paragraph 3.7.3 of the C++11 Standard:
根据C ++ 11标准的3.7.3段:
Block-scope variables explicitly declared
register
or not explicitly declaredstatic
orextern
have automatic storage duration .块范围变量显式声明为
register
或未显式声明为static
或extern
具有自动存储持续时间 。 The storage for these entities lasts until the block in which they are created exits .这些实体的存储将持续到创建它们的块为止 。
Notice, however, that this concerns variables with automatic storage duration. 但请注意,这涉及具有自动存储持续时间的变量。 If you created an object dynamically with
new
and assigned the result to a local raw pointer, then only the raw pointer will be destroyed, and not the pointed object: 如果使用
new
动态创建对象并将结果分配给本地原始指针,则只会销毁原始指针 ,而不是指向对象:
{
int* foo = new int(42);
} // Here you have a memory leak: the foo pointer is destroyed,
// but not the object foo pointed to
Yes, it's guaranteed. 是的,这是有保证的。
Monitoring memory usage with system monitor might be not precise, because application does not return memory to the system. 使用系统监视器监视内存使用情况可能不准确,因为应用程序不会将内存返回给系统。 Once allocated it belongs to the application and even if your objects are destroyed you might not see any difference.
一旦分配它属于应用程序,即使您的对象被销毁,您可能看不到任何差异。
If you want to guarantee that your application does not have memory leaks you may want to use such tools as valgrind, or drMemory from google, or several others (google for "memory leak detection"). 如果你想保证你的应用程序没有内存泄漏,你可能想要使用像valgrind这样的工具,或谷歌的drMemory或其他几个(google用于“内存泄漏检测”)。 In this case you will have the most precise information about allocations, deallocations, leaks, memory access violations etc.
在这种情况下,您将获得有关分配,解除分配,泄漏,内存访问违规等的最准确信息。
Yes it is guaranteed that every variable that goes out of scope will get it's destructor called. 是的,保证超出范围的每个变量都会调用它的析构函数。
2 Variables with automatic storage duration (3.7.3) are initialized each time their declaration-statement is executed.
2每次执行声明语句时,都会初始化具有自动存储持续时间(3.7.3)的变量。 Variables with automatic storage duration declared in the block are destroyed on exit from the block (6.6).
块中声明的具有自动存储持续时间的变量在从块 (6.6) 退出时被销毁 。
It will if you have code like this: 如果你有这样的代码,它会:
void f()
{
A a; // create a local instance of A
// memory will be allocated on the stack,
// and the constructor for `a` will be called.
// various code here
// here at the end of the scope,
// the destructor for `a` will be called,
// and the memory on the stack will be freed.
}
[11.5] Should I explicitly call a destructor on a local variable? [11.5]我应该在局部变量上显式调用析构函数吗?
No! 没有!
The destructor will get called again at the close } of the block in which the local was created. 析构函数将在创建本地的块的close}处再次被调用。 This is a guarantee of the language;
这是语言的保证; it happens automagically;
它自动发生; there's no way to stop it from happening.
没有办法阻止它发生。 But you can get really bad results from calling a destructor on the same object a second time!
但是你可以通过第二次在同一个对象上调用析构函数来获得非常糟糕的结果! Bang!
砰! You're dead!
你死定了!
It goes a little beyond your question, but the base is the same. 它有点超出你的问题,但基数是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.