c++静态 非静态

发布于 2024-10-21 22:43:07 字数 109 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别在捏我脸啦 2024-10-28 22:43:07

它们可以放在编译器(或链接器或加载器)想要将它们放入内存的任何地方,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.

意中人 2024-10-28 22:43:07

非静态成员的驻留位置取决于对象的实例化方式。

class foo
{
    int num ; // Non-Static member 
    // ....
};

foo obj ; // In this case `num` resides on stack. In fact, obj it self resides on stack
foo *temp = new foo;  // In this case `num` resides on heap or in memory location acquired from the free store.

我不确定静态成员。

Non static members residing place depends up on how the object is instantiated.

class foo
{
    int num ; // Non-Static member 
    // ....
};

foo obj ; // In this case `num` resides on stack. In fact, obj it self resides on stack
foo *temp = new foo;  // In this case `num` resides on heap or in memory location acquired from the free store.

I amn't sure about static members.

深白境迁sunset 2024-10-28 22:43:07

静态与全局变量位于同一位置,这往往由编译器确定,并且在加载程序时创建并持续到程序结束

非静态位于您将它们放置的任何位置(在堆栈或堆上)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文