new A v.s. new A()

Discussion in 'C++' started by Wenjie, Jun 26, 2003.

  1. Wenjie

    Wenjie Guest

    Hello,


    Is there any difference between:
    A* p = new A;
    and
    A* p = new A();
    ?

    I know it must have been dicussed here, but just
    not easy to search. BTW, I think
    A a;
    is alike new A as far as constructor call concerned
    (default construtor call?) Right?

    And
    A a();
    is just a function declaration?


    Thanks and best regards,
    Wenjie
    Wenjie, Jun 26, 2003
    #1
    1. Advertising

  2. "Wenjie" <> wrote...
    > Is there any difference between:
    > A* p = new A;
    > and
    > A* p = new A();
    > ?


    There is no difference for classes, there is a difference
    for POD: the former leaves the object uninitialised, the
    latter zero-initialises it.

    > I know it must have been dicussed here, but just
    > not easy to search. BTW, I think
    > A a;
    > is alike new A as far as constructor call concerned
    > (default construtor call?) Right?


    It's a declaration (and a definition in certain circumstances)
    of an object 'a' of type A. If it's a definition, the object
    is default-initialised, which for a class means that its c-tor
    is invoked, yes.

    >
    > And
    > A a();
    > is just a function declaration?


    Yes, it is.


    Victor
    Victor Bazarov, Jun 26, 2003
    #2
    1. Advertising

  3. Wenjie wrote:
    >
    > Hello,
    >
    > Is there any difference between:
    > A* p = new A;
    > and
    > A* p = new A();
    > ?


    Yes, there is a difference in certain circumstances.
    It has to do with POD's (POD = plain old data structure,
    basically a class or a struct built from only builtin types
    and having no member functions, in short: a struct as it was
    ment to be in C)

    new A
    creates a new POD-object of type A and leaves all members uninitialized

    new A()
    creates a new POD-object of type A but 0-initializes all members.

    > A a();
    > is just a function declaration?


    Yep. That's a pitfall.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jun 26, 2003
    #3
  4. Wenjie

    Ron Natalie Guest

    "Victor Bazarov" <> wrote in message news:...

    > There is no difference for classes,


    non-POD classes (but you knew that).

    > there is a difference
    > for POD: the former leaves the object uninitialised, the
    > latter zero-initialises it.
    Ron Natalie, Jun 26, 2003
    #4
  5. Wenjie

    Ron Natalie Guest

    "Karl Heinz Buchegger" <> wrote in message news:...
    >
    > Yes, there is a difference in certain circumstances.
    > It has to do with POD's (POD = plain old data structure,
    > basically a class or a struct built from only builtin types
    > and having no member functions, in short: a struct as it was
    > ment to be in C)
    >


    That's not the definition of POD.

    POD classes can't have
    non-static data of non-pod type (other non-POD structs or unions, pointer-to-members or references).
    user-defined constructors, destructors, or copy-assignment operators
    base classes
    protected or private non-static data members
    virtual functions.

    They can however have other member functions not specifically mentioned above.

    In short, a class whose data behaves identically to a C struct.
    Ron Natalie, Jun 26, 2003
    #5
  6. "Wenjie" <> wrote...
    > "Victor Bazarov" <> wrote in message

    news:<>...
    > > "Wenjie" <> wrote...
    > > > Is there any difference between:
    > > > A* p = new A;
    > > > and
    > > > A* p = new A();
    > > > ?

    > >
    > > There is no difference for classes, there is a difference
    > > for POD: the former leaves the object uninitialised, the
    > > latter zero-initialises it.

    >
    > The default constructor (whether user defined or not) is
    > called in both cases like for a definition "A a;" right?


    If it's not defined ("trivial"), how would it be called?

    >
    > Then "zero-init" is only for simplifying the anwser, since
    > what I know a default constructor could be written as:


    Zero-initialisation is only performed for POD. As soon as
    you define a constructor, it's not a POD any longer.

    > class A {
    > public:
    > A(): a_(12) {}
    > //...
    >
    > private:
    > int a_;
    > };
    >
    >
    > >
    > > > I know it must have been dicussed here, but just
    > > > not easy to search. BTW, I think
    > > > A a;
    > > > is alike new A as far as constructor call concerned
    > > > (default construtor call?) Right?

    > >
    > > It's a declaration (and a definition in certain circumstances)
    > > of an object 'a' of type A. If it's a definition, the object
    > > is default-initialised, which for a class means that its c-tor
    > > is invoked, yes.
    > >
    > > >
    > > > And
    > > > A a();
    > > > is just a function declaration?

    > >
    > > Yes, it is.
    > >
    > >
    > > Victor

    >
    >
    > Thanks,
    Victor Bazarov, Jun 26, 2003
    #6
  7. Wenjie

    Yu Cao Guest

    "Victor Bazarov" <> writes:

    >"Wenjie" <> wrote...
    >> "Victor Bazarov" <> wrote in message

    >news:<>...
    >> > "Wenjie" <> wrote...
    >> > > Is there any difference between:
    >> > > A* p = new A;
    >> > > and
    >> > > A* p = new A();
    >> > > ?
    >> >
    >> > There is no difference for classes, there is a difference
    >> > for POD: the former leaves the object uninitialised, the
    >> > latter zero-initialises it.


    This is all fine. I just got into a heated debate with a colleague
    about operator new[] though:

    Q1. Is operator new[] allowed to take a non-default constructor for
    a user-defined type? Example:

    class A { public: A(int, int, int); };
    A* p = new A[100](1, 2, 3);

    My position is yes. GCC 3.2 apparently agrees.

    Q2. For a POD type, is operator new[] allowed to take non-default
    initializers? Example:

    int* p = new int[100](1);

    My inclination is to say yes. But GCC is not on my side this time.
    For int* p = new int(1), *p is initialized to 1. With operator new[]
    though, it did not.

    Q3. For a POD type, should the following two statements both perform
    zero initialization:

    int *p = new int[100];
    int *p = new int[100]();

    My understanding is the first one doesn't and the second one does, just
    like for operator new.

    We've consulted the standard but our interpretations are different.
    My basic stance is what's quoted for operator new should apply as well to
    operator new[].

    --Yu
    Yu Cao, Jun 27, 2003
    #7
  8. "Yu Cao" <> wrote...
    > "Victor Bazarov" <> writes:
    >
    > >"Wenjie" <> wrote...
    > >> "Victor Bazarov" <> wrote in message

    > >news:<>...
    > >> > "Wenjie" <> wrote...
    > >> > > Is there any difference between:
    > >> > > A* p = new A;
    > >> > > and
    > >> > > A* p = new A();
    > >> > > ?
    > >> >
    > >> > There is no difference for classes, there is a difference
    > >> > for POD: the former leaves the object uninitialised, the
    > >> > latter zero-initialises it.

    >
    > This is all fine. I just got into a heated debate with a colleague
    > about operator new[] though:
    >
    > Q1. Is operator new[] allowed to take a non-default constructor for
    > a user-defined type?


    If the user-defined type does not have a default constructor,
    the program that tries to dynamically allocate an array of such
    type is ill-formed. See Standard

    > Example:
    >
    > class A { public: A(int, int, int); };
    > A* p = new A[100](1, 2, 3);
    >
    > My position is yes.


    The Standard doesn't agree with you. See 5.3.4/15. Only default
    initialisation is allowed for arrays.

    > GCC 3.2 apparently agrees.


    Doesn't prove anything.

    >
    > Q2. For a POD type, is operator new[] allowed to take non-default
    > initializers?


    Nope.

    > Example:
    >
    > int* p = new int[100](1);
    >
    > My inclination is to say yes.


    Oh, well...

    > But GCC is not on my side this time.
    > For int* p = new int(1), *p is initialized to 1. With operator new[]
    > though, it did not.


    Right. PODs are left uninitialised.

    >
    > Q3. For a POD type, should the following two statements both perform
    > zero initialization:
    >
    > int *p = new int[100];
    > int *p = new int[100]();


    The first one does not perform any initialisation.

    >
    > My understanding is the first one doesn't and the second one does, just
    > like for operator new.
    >
    > We've consulted the standard but our interpretations are different.
    > My basic stance is what's quoted for operator new should apply as well to
    > operator new[].


    It doesn't. But if you'd like to make a suggestion, you should
    definitely do so in comp.std.c++.

    Victor
    Victor Bazarov, Jun 27, 2003
    #8
  9. Wenjie

    Ron Natalie Guest

    "Victor Bazarov" <> wrote in message news:...

    > > The default constructor (whether user defined or not) is
    > > called in both cases like for a definition "A a;" right?

    >
    > If it's not defined ("trivial"), how would it be called?
    >

    As far as the standard is concerend it behaves as if one was automatically
    inserted by the compiler.
    Ron Natalie, Jun 27, 2003
    #9
  10. Ron Natalie wrote:
    [...]
    > > int *p = new int[100];
    > > int *p = new int[100]();

    >
    > The latter is ILLEGAL.
    > The former provides no initialization. Another major defect of the C++ language.


    You just love things like

    struct IntGarbage { IntGarbage() {} int garbage; };

    I guess. ;-)

    regards,
    alexander.
    Alexander Terekhov, Jun 27, 2003
    #10
  11. Wenjie

    Greg Comeau Guest

    In article <>,
    Victor Bazarov <> wrote:
    >"Ron Natalie" <> wrote...
    >> "Yu Cao" <> wrote in message

    >news:bdfvj6$pgq$...
    >> > Q2. For a POD type, is operator new[] allowed to take non-default
    >> > initializers? Example:
    >> >
    >> > int* p = new int[100](1); // LINE AA

    >>
    >> What you wrote above is illegal for ANY type.
    >> >
    >> > Q3. For a POD type, should the following two statements both perform
    >> > zero initialization:
    >> >
    >> > int *p = new int[100]; // LINE BB
    >> > int *p = new int[100](); // LINE CC

    >>
    >> The latter is ILLEGAL.

    >
    >Really? How would you explain that it works in Comeau? Another
    >"fucked-up Comeau extension"?


    Just so there is no confusion, I labelled the above statements.
    LINE BB and LINE CC are both valid, legal, and well-defined,
    and usually mean different things. LINE AA is not allowed.
    --
    Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Jun 28, 2003
    #11
  12. "Ron Natalie" <> wrote...
    >
    > "Victor Bazarov" <> wrote in message

    news:...
    >
    > > > The default constructor (whether user defined or not) is
    > > > called in both cases like for a definition "A a;" right?

    > >
    > > If it's not defined ("trivial"), how would it be called?
    > >

    > As far as the standard is concerend it behaves as if one was automatically
    > inserted by the compiler.


    How can one verify that "as if"? There is no code to put
    the breakpoint or a debugging output statement, is there?
    That's what my question was about.
    Victor Bazarov, Jun 28, 2003
    #12
  13. "Greg Comeau" <> wrote in message
    news:bdir76$shh$...
    > In article <>,
    > Victor Bazarov <> wrote:
    > >"Ron Natalie" <> wrote...
    > >> "Yu Cao" <> wrote in message

    > >news:bdfvj6$pgq$...
    > >> > Q2. For a POD type, is operator new[] allowed to take non-default
    > >> > initializers? Example:
    > >> >
    > >> > int* p = new int[100](1); // LINE AA
    > >>
    > >> What you wrote above is illegal for ANY type.
    > >> >
    > >> > Q3. For a POD type, should the following two statements both perform
    > >> > zero initialization:
    > >> >
    > >> > int *p = new int[100]; // LINE BB
    > >> > int *p = new int[100](); // LINE CC
    > >>
    > >> The latter is ILLEGAL.

    > >
    > >Really? How would you explain that it works in Comeau? Another
    > >"fucked-up Comeau extension"?

    >
    > Just so there is no confusion, I labelled the above statements.
    > LINE BB and LINE CC are both valid, legal, and well-defined,
    > and usually mean different things. LINE AA is not allowed.


    The whole idea for my "Really" was to coerce Ron into looking it
    up in the Standard in an attempt to make him substantiate his
    claims. It has become customary here (in response to some newbies'
    "it works this way here so it's right" statements) to actually
    quote the Standard (not that it has the desired effect every time),
    so I would expect both Ron and you not to use "it's legal" or "it's
    ILLEGAL" without proof. And, mind you, *I* don't need that proof,
    most of the time. It's the other posters who read the forum I am
    concerned about. :)

    Victor
    Victor Bazarov, Jun 28, 2003
    #13
  14. Wenjie

    Ron Natalie Guest

    "Victor Bazarov" <> wrote in message news:VHhLa.41883$Fy6.12552@sccrnsc03...

    >
    > How can one verify that "as if"? There is no code to put
    > the breakpoint or a debugging output statement, is there?
    > That's what my question was about.
    >


    Debugging is not part of the language.
    The constructor is treated AS IF it exists. Whether the compiler
    emits code or calls it is an implementation detail. Read 12.1/5
    and 12.1/7
    Ron Natalie, Jun 30, 2003
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robb Smith
    Replies:
    2
    Views:
    554
    Saravana
    Jul 18, 2003
  2. =?Utf-8?B?UGF0cmljaw==?=

    Open new with new session

    =?Utf-8?B?UGF0cmljaw==?=, Dec 18, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,312
    tittlejoseph
    Sep 2, 2007
  3. Gawel
    Replies:
    1
    Views:
    384
    Alvin Bruney
    Dec 31, 2003
  4. =?Utf-8?B?QmVuamFtaW4=?=
    Replies:
    0
    Views:
    301
    =?Utf-8?B?QmVuamFtaW4=?=
    Feb 12, 2004
  5. Replies:
    2
    Views:
    407
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page