int *p=new int[5]()
int *p=new int[5]
两个的区别???
求指教

解决方案 »

  1.   

    看看这个也许你就明白了/*That's not quite true (you should almost certainly get yourself an alternative reference), you are allowed an empty initializer (()) which will value-initialize the array but yes, you can't initialize array elements individually when using array new. (See ISO/IEC 14882:2003 5.3.4 [expr.new] / 15)E.g.
    */
    int* p = new int[5](); // array initialized to all zero
    int* q = new int[5];   // array elements all have indeterminate value/*There's no fundamental reason not to allow a more complicated initializer it's just that C++03 didn't have a grammar construct for it. In the next version of C++ you will be able to do something like this.
    */
    int* p = new int[5] {0, 1, 2, 3, 4};
      

  2.   

    VC里的new有两种,一种是对对象进行操作的new,另一种则是对基础数据类型进行操作的new,楼主问题里的,是后一种。不管是哪一种new操作,本质上分为两部,1是计算并分析空间,2是在分析的空间上进行初始化操作。
    对于基础数据来说,是否需要初始化,是需要显示指示的。如这里的,
    int *p=new int[5]()在分配空间后,对每个int进行了默认的初始化操作。即p[n]的值是为0的。
    int *p=new int[5]只是分配了空间,但没有进行默认初始化。对于对象,使用new操作符,则将自动进行默认初始化,无论后面是否有(),如:
    class A
    {
        int a;
    public:
        A() : a(0) {}
    };A *p1 = new A[5]();
    A *p2 = new A[5];
      

  3.   

    真的是初始化吗?
    #include<iostream.h>
    int main()
    {
    int *p=new int[5]();
            int *q=new int[5];
    cout<<p[1]<<" "<<q[0];
    return 0;
    }
    vc6.0运行结果均是随机值
      

  4.   


    int* p = new int[5](); // array initialized to all zero
    int* q = new int[5];   // array elements all have indeterminate value
    这个注释是正确的,至于为什么vc6调试发现结果均为随机值,可参考如下官方解释:
    But many older compilers "forget" to perform
    the initialization when the '()' initializer is used. This used to be a
    popular bug among several compilers.

    在vs2003及后续版本中此BUG已经修复