我很好奇知道初始化的成员变量在内存中的位置吗?即css,bss,数据段,堆...

如果我的问题很愚蠢,请不要诅咒我:)
例如

class A
{
    A();
    int m_iVar;
}

A::A(int i)
{
    m_iVar = i;
}

A::A()
{
    m_iVar = 99;
}

main()
{
    A o; // => After compilation, the object o, in which segment does it reside? and especially  where does "m_iVar" reside
    A o1(5); // => After compilation here object o1, in which segment does it reside?and especially where does "m_iVar" reside?

    A *pA = new A; // After compilation, here the memory pointed by pA, I guess it goes to heap, but the variable "m_iVar" where does it reside
}

最佳答案

与其他任何变量相同。它在标准中并未真正指定,但是如果您创建本地类实例,则通常将其驻留在堆栈中,如果对其进行new编码,则它将驻留在免费商店中。正如您所期望的,成员变量位于实例内部。静态类变量可能会驻留在数据段之一中。

非静态实例不在“编译后”的任何地方,它们是在运行时创建的。

10-04 14:33