z****e 发帖数: 2024 | 1 为了验证我的一些想法,我写了如下代码,
标号2,4的运行区别不是很理解。
class X{
public:
int i;
};
X* px=new X;
X gox;
int main(){
X* lpx=new X;
std::cout<i<
std::cout<i<
X ox;
std::cout<
std::cout<
}
结果:
0
0
0
-1077381160
主要是2,和4,为什么2和4有区别呢?
stack和heap上,初始化还不一样?
stack和静态区不一样是可以理解的。
另外
X* lpx=new X;
和
X* lpx=new X();
貌似没有区别,实际有没有区别呢?
因为heap上要zero out,所以heap的效率要轻微的低于stack??? |
B*******g 发帖数: 1593 | 2 http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-
type-name-make-a-difference-with-new
基本上回答了关于new X和new X()的区别
heap或stack上会不会zero initialization...我觉得太偏了吧。。实际中也不会冒险让
compiler来初始化啊
【在 z****e 的大作中提到】 : 为了验证我的一些想法,我写了如下代码, : 标号2,4的运行区别不是很理解。 : class X{ : public: : int i; : }; : X* px=new X; : X gox; : int main(){ : X* lpx=new X;
|
m******1 发帖数: 418 | 3 弱弱地问一下:POD是指什么?
险让
【在 B*******g 的大作中提到】 : http://stackoverflow.com/questions/620137/do-the-parentheses-after-the- : type-name-make-a-difference-with-new : 基本上回答了关于new X和new X()的区别 : heap或stack上会不会zero initialization...我觉得太偏了吧。。实际中也不会冒险让 : compiler来初始化啊
|
l******e 发帖数: 12192 | 4 plain-old data
【在 m******1 的大作中提到】 : 弱弱地问一下:POD是指什么? : : 险让
|
P********e 发帖数: 2610 | 5 今天刚看到这里,global variable will be initialized to zero
【在 z****e 的大作中提到】 : 为了验证我的一些想法,我写了如下代码, : 标号2,4的运行区别不是很理解。 : class X{ : public: : int i; : }; : X* px=new X; : X gox; : int main(){ : X* lpx=new X;
|