C++ 删除静态数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6850009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Deleting Static Data
提问by Ell
If I have a class that contains private static data allocated on the heap that never changes, when, if at all, should I delete it?
如果我有一个包含在堆上分配的私有静态数据的类,该数据永远不会改变,我应该在什么时候删除它?
As I understand it, a class itself is never constructed (because classes aren't first class objects in C++) then there is no destructor to delete the static data in? Im new at C++ so sorry if my understanding of c++ is fundamentaly flawed or if the answer is obvious! Thanks in advance, ell.
据我了解,类本身永远不会被构造(因为类不是 C++ 中的第一类对象)然后没有析构函数来删除静态数据?我是 C++ 新手,如果我对 C++ 的理解存在根本性的缺陷或者答案很明显,那么我很抱歉!提前致谢,嗯。
采纳答案by James Kanze
If the data is static, it isn't allocated on the heap, and it will be destructed during the shutdown of the process.
如果数据是静态的,则不会在堆上分配,会在进程关闭时被销毁。
If it is a pointer to the data which is static, e.g.:
如果它是指向静态数据的指针,例如:
Something* MyClass::aPointer = new Something;
then like all other dynamically allocated data, it will only be destructed when you delete it. There are two frequent solutions:
那么像所有其他动态分配的数据一样,它只会在您删除它时被破坏。有两种常见的解决方案:
use a smart pointer, which has a destructor which deletes it, or
don't delete it; in most cases, there's really no reason to call the destructor, and if you happen to use the instance in the destructors of other static objects, you'll run into an order of destruction problem.
使用智能指针,它有一个删除它的析构函数,或者
不要删除它;在大多数情况下,确实没有理由调用析构函数,如果您碰巧在其他静态对象的析构函数中使用了该实例,则会遇到破坏顺序问题。
回答by Nawaz
static
data means, it persists the entire duration of the program.
static
data 意味着,它会在程序的整个持续时间内持续存在。
However, if you use static
in pointer as:
但是,如果您使用static
in 指针作为:
static A *pA = new A();
then you can delete this, by writing delete pA
. But that doesn't invalidate my first statement. Because the object which is being pointed to by the staticpointer is not static. Its the pointerwhich is static, not the object which is being pointed to by the pointer.
然后你可以删除它,通过写delete pA
。但这并没有使我的第一句话无效。因为静态指针指向的对象不是静态的。它是静态的指针,而不是指针指向的对象。
回答by ks1322
You can place this class in std::auto_ptr
. Then it will be deleted automatically on program shutdown. Otherwise memory leak tools will complain, that your class leaks. On the other hand this memory leak is harmless because the program finished running.
你可以把这个类放在std::auto_ptr
. 然后它会在程序关闭时自动删除。否则内存泄漏工具会抱怨你的类泄漏。另一方面,这种内存泄漏是无害的,因为程序已完成运行。
回答by Mark Ransom
I presume you're actually referring to a static pointer to an object on the heap?
我认为您实际上是指指向堆上对象的静态指针?
This will never be deleted automatically, you must do it yourself. Most of the time it's sufficient to let the program end and the OS do the cleanup, unless you're using a memory checking tool or the destructor has side effects that you require.
这个永远不会被自动删除,你必须自己做。大多数情况下,让程序结束并让操作系统进行清理就足够了,除非您使用内存检查工具或析构函数具有您需要的副作用。
The easiest thing to do is use a smart pointer, which will automatically delete the object when nobody is referring to it anymore. You can keep a copy of the pointer in main
if there are times when nobody will have a copy, then the object will be deleted when main
exits.
最简单的方法是使用智能指针,当没有人再引用它时,它会自动删除该对象。main
如果有时没有人拥有副本,您可以保留指针的副本,然后在main
退出时删除该对象。
回答by cprogrammer
static data allocated on the heap means a member pointer that is static. If this is the case you can allocate memory to it.
在堆上分配的静态数据意味着静态成员指针。如果是这种情况,您可以为其分配内存。