C ++:静态原语在程序退出时会变得无效吗? [英] C++: Do static primitives become invalid at program exit?

查看:84
本文介绍了C ++:静态原语在程序退出时会变得无效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个像这样的功能:

Assume I have a function like this:

MyClass &MyFunction(void)
{
  static MyClass *ptr = 0;
  if (ptr == 0)
    ptr = new MyClass;
  return MyClass;
}

问题是在程序退出时,ptr变量是否会变得无效(即该ptr的内容由退出进程清除)?我意识到该函数会泄漏,但这只是为了简单起见.

The question is at program exit time, will the ptr variable ever become invalid (i.e. the contents of that ptr are cleaned up by the exiting process)? I realize that this function leaks, but it is only an example for simplicity.

相同的问题也适用于除指针之外的其他原语.如果我有一个静态整数,该整数的值在整个出口期间是否始终存在,或者由于静态销毁顺序问题而可变,该怎么办?

The same question also applies to other primitives besides pointers as well. How about if I have a static integer, does the value of that integer always persist throughout exit or is variable due to static destruction order issues?

仅需澄清一下,我想知道静态指针(或其他任何原始类型,如int或float)的内容实际发生了什么,而不是它所指向的内存.例如,假设ptr指向我要在其他静态类的析构函数中检查的某个内存地址.我可以依靠这样的事实,即ptr的内容不会被更改(即在静态销毁过程中不会清除指针值)吗?

Just to clarify, I want to know what actually happens to the contents of the static pointer (or any other primitive type like an int or a float) and not to the memory it is pointing to. For instance, imagine that the ptr points to some memory address which I want to check in the destructor of some other static class. Can I rely on the fact that the contents of the ptr won't be changed (i.e. that the pointer value won't be cleaned up during the static destruction process)?

谢谢, 乔

推荐答案

回答您的问题:

To answer your question:

'imagine that the ptr points to some memory address which I want to check in the destructor of some other static class'

答案是肯定的.
您可以看到指针的值(地址).
如果尚未在指针上调用delete,则可以查看内容.

The answer is yes.
You can see the value of the pointer (the address).
You can look at the content if you have not called delete on the pointer.

静态函数变量的行为与静态类变量和全局变量( aka 非局部静态)相同,因为析构函数将以相反的创建顺序被调用.整数,浮点数和指针(POD)没有析构函数,因此在删除进程之前,它们不会发生任何事情.

Static function variables behave in the same way as static class variables and global variables (aka non local static), in that there destructors will be called in reverse order of creation. Integers, floats and pointers (POD) do not have destructors so nothing happens to them until the processes is removed.

POD对象:可以安全地从其他对象(甚至是global)的析构函数中引用数据.

POD objects: Data can safely by referenced from the destructor of other objects (even global s).

其他静态对象(即带有析构函数的对象):在一般情况下,退出main()后访问这些对象并不安全,因为不知道破坏的顺序(与创建顺序相反) ,但创建顺序很复杂,请参见:

Other static objects (i.e. those with destructors): In the general case, it is not safe to accesses these objects after main() has exited because the order of destruction is not know (it is the reverse of the order of creation, but the order of creation is complex see: Construction Order ). It can be done but you have to take explicit precautions to make sure the object is still alive.

内存将一直存在,调用析构函数后对象将无效(请注意POD没有析构函数).

The memory will always be there, the object will just not be valid after a destructor is called (note POD does not have a destructor).

仅在声明它们的作用域之前有效.
弹出堆栈后,如果尝试访问它,则位于其上的内存页面可能会被丢弃,从而导致SEG错误.

Only valid until the scope in which they are declared is left.
After the stack is popped the memory page that it is on could potentially be dropped resulting in SEG faults if you attempt to access it.

有效,直到您在分配它的指针上调用delete为止.
删除指针后,该值可能是随机的,因为它可能会被重复使用. 可能还可以删除内存所在的页面.对已删除页面的任何访问都将导致SEG错误.

Valid until you call delete on the pointer that allocated it.
Once a pointer is delete the value is potentially random as it may be re-used. Potentially the page the memory was on can also be dropped. Any access to a dropped page would result in a SEG fault.

这篇关于C ++:静态原语在程序退出时会变得无效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆