1 the blog provides no more useful information than your post 2 anyway, google zero initialization and you will see why
【在 s*****e 的大作中提到】 : Is there any difference between the following two implementations? : struct Foo : { : enum Enum : { : SIZE = 1024; : } : : char m_buf[SIZE]; : };
我来给你看看真正的例子是怎么写的, 那个街头程序员明显什么都没搞清楚就在那里写 blog. new, new什么? 有没有括号? 这都是不一样的. 当然C++在这个事情上是不intuitive, 也难怪大家抱怨. #include #include using namespace std; struct A { int c[10]; }; struct B { B() {}; int c[10]; }; int main() { /* fill the memory with garbage and release, * just to show the difference */ int *g=new int[10000]; for (int i=0; i<10000; i++) g[i]=rand(); delete g; A* a1=new A; A* a2=new A(); B* b1=new B; B* b2=new B(); cout<c[5]<cout<c[5]<cout<c[5]<cout<c[5]<}
【在 s*****e 的大作中提到】 : Is there any difference between the following two implementations? : struct Foo : { : enum Enum : { : SIZE = 1024; : } : : char m_buf[SIZE]; : };
To make the example portable: ... char g[10000], *m = g; void* operator new(size_t size) { void *r = m; m += size; return r; } int main() { /* fill the memory with garbage * just to show the difference */ for (int i=0; i<10000; i++) g[i]=rand(); A* a1=new A; ...