c++静态 非静态
在 C++ 中 静态或非静态变量存放在哪里?我的意思是在记忆中。
并且,静态或非静态变量何时初始化?
需要有人帮助我理清思路。 谢谢你!
那么C呢?相同的?
in c++
where are static or non-static variables stay? I mean in memory.
and, When are static or non-static variables initialized?
Need someone help me get my thought clear.
Thank you!
and what about C? same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它们可以放在编译器(或链接器或加载器)想要将它们放入内存的任何地方,C 和 C++ 标准不强制要求这种详细程度。它们仅强制行为。
通常,静态成员会在程序启动时(包括在编译时,以便它们简单地以已初始化状态加载)或在首次使用之前初始化一次。
They can go wherever the compiler (or linker or loader) wants to put them in memory, the C and C++ standards don't mandate that level of detail. They only mandate the behaviour.
Typically, static members are initialised once, either on program startup (including at compile time so that they're simply loaded in an already-initialised state) or immediately before first use.
非静态成员的驻留位置取决于对象的实例化方式。
我不确定静态成员。
Non static members residing place depends up on how the object is instantiated.
I amn't sure about static members.
静态与全局变量位于同一位置,这往往由编译器确定,并且在加载程序时创建并持续到程序结束
非静态位于您将它们放置的任何位置(在堆栈或堆上)
Statics go in the same place as globals, which tends to be determined by the compiler, and are created when the program is loaded and persist until the program ends
Non-statics go where-ever you put them (on the stack or the heap)