开发者

c++ static non-static

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?


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.

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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜