Is `new classname' the same as `new classname()' ?

Discussion in 'C++' started by Hongzheng Wang, Dec 4, 2003.

  1. Hi,
    I saw some codes use `new classname' to get a point to
    a new object of classname, but some codes, instead, use
    `new classname()'.
    Is these two usage equivalent?

    Thank you.

    --
    Hongzheng Wang
    Department of Electronics Engineering
    Tsinghua University
     
    Hongzheng Wang, Dec 4, 2003
    #1
    1. Advertising

  2. "Hongzheng Wang" <> wrote...
    > I saw some codes use `new classname' to get a point to
    > a new object of classname, but some codes, instead, use
    > `new classname()'.
    > Is these two usage equivalent?


    For POD they are not. The former leaves it uninitialised,
    the latter makes it default-initialised, IIRC.

    Victor
     
    Victor Bazarov, Dec 4, 2003
    #2
    1. Advertising

  3. Hongzheng Wang

    Xenos Guest

    "Victor Bazarov" <> wrote in message
    news:akyzb.416658$Fm2.425058@attbi_s04...
    > "Hongzheng Wang" <> wrote...
    > > I saw some codes use `new classname' to get a point to
    > > a new object of classname, but some codes, instead, use
    > > `new classname()'.
    > > Is these two usage equivalent?

    >
    > For POD they are not. The former leaves it uninitialised,
    > the latter makes it default-initialised, IIRC.
    >
    > Victor
    >
    >

    No, both would cause default initialization. You cannot create an
    uninitialized class object without a kludge.

    DrX
     
    Xenos, Dec 4, 2003
    #3
  4. Xenos wrote:
    >
    > "Victor Bazarov" <> wrote in message
    > news:akyzb.416658$Fm2.425058@attbi_s04...
    > > "Hongzheng Wang" <> wrote...
    > > > I saw some codes use `new classname' to get a point to
    > > > a new object of classname, but some codes, instead, use
    > > > `new classname()'.
    > > > Is these two usage equivalent?

    > >
    > > For POD they are not. The former leaves it uninitialised,
    > > the latter makes it default-initialised, IIRC.
    > >
    > > Victor
    > >
    > >

    > No, both would cause default initialization.


    They don't

    > You cannot create an
    > uninitialized class object without a kludge.
    >


    you can:

    #include <iostream>
    using namespace std;

    class A
    {
    public:
    int m_n;
    };

    int main()
    {
    A* pA = new A;
    A* pB = new A();


    Here pA->m_n is left uninitialized while
    pB->m_n should be default initialized.

    VC++ 6.0 however gets this wrong and I suspect that
    most compiler get this wrong. In other words: don't
    depend on it.

    Oh, by the way: Yes in the above, class A is a POD.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Dec 4, 2003
    #4
  5. Hongzheng Wang

    Xenos Guest

    "Karl Heinz Buchegger" <> wrote in message
    news:...

    > you can:
    >
    > #include <iostream>
    > using namespace std;
    >
    > class A
    > {
    > public:
    > int m_n;
    > };
    >
    > int main()
    > {
    > A* pA = new A;
    > A* pB = new A();
    >
    >
    > Here pA->m_n is left uninitialized while
    > pB->m_n should be default initialized.
    >
    > VC++ 6.0 however gets this wrong and I suspect that
    > most compiler get this wrong. In other words: don't
    > depend on it.
    >
    > Oh, by the way: Yes in the above, class A is a POD.
    >

    I don't care what you call it, both uses of the new operator will call the
    default constructor.
     
    Xenos, Dec 4, 2003
    #5
  6. "Xenos" <> wrote...
    >
    > "Karl Heinz Buchegger" <> wrote in message
    > news:...
    >
    > > you can:
    > >
    > > #include <iostream>
    > > using namespace std;
    > >
    > > class A
    > > {
    > > public:
    > > int m_n;
    > > };
    > >
    > > int main()
    > > {
    > > A* pA = new A;
    > > A* pB = new A();
    > >
    > >
    > > Here pA->m_n is left uninitialized while
    > > pB->m_n should be default initialized.
    > >
    > > VC++ 6.0 however gets this wrong and I suspect that
    > > most compiler get this wrong. In other words: don't
    > > depend on it.
    > >
    > > Oh, by the way: Yes in the above, class A is a POD.
    > >

    > I don't care what you call it, both uses of the new operator will call the
    > default constructor.


    There is no default constructor for an int.

    Now, are these two forms the same

    int a;

    and

    int a = int();

    ? If not, what's the difference? What value does 'a' get in each
    case?

    And if you don't know that POD is (or what it stands for), perhaps
    you need to learn first, then begin arguing...

    Victor
     
    Victor Bazarov, Dec 4, 2003
    #6
  7. "Victor Bazarov" <> wrote...
    > "Xenos" <> wrote...
    > >
    > > "Karl Heinz Buchegger" <> wrote in message
    > > news:...
    > >
    > > > you can:
    > > >
    > > > #include <iostream>
    > > > using namespace std;
    > > >
    > > > class A
    > > > {
    > > > public:
    > > > int m_n;
    > > > };
    > > >
    > > > int main()
    > > > {
    > > > A* pA = new A;
    > > > A* pB = new A();
    > > >
    > > >
    > > > Here pA->m_n is left uninitialized while
    > > > pB->m_n should be default initialized.
    > > >
    > > > VC++ 6.0 however gets this wrong and I suspect that
    > > > most compiler get this wrong. In other words: don't
    > > > depend on it.
    > > >
    > > > Oh, by the way: Yes in the above, class A is a POD.
    > > >

    > > I don't care what you call it, both uses of the new operator will call

    the
    > > default constructor.



    Oh, I meant to refer you to 5.3.4/15, but you wouldn't know where
    to look, would you?

    > There is no default constructor for an int.
    >
    > Now, are these two forms the same
    >
    > int a;
    >
    > and
    >
    > int a = int();
    >
    > ? If not, what's the difference? What value does 'a' get in each
    > case?
    >
    > And if you don't know that POD is (or what it stands for), perhaps
    > you need to learn first, then begin arguing...
     
    Victor Bazarov, Dec 4, 2003
    #7
  8. Hongzheng Wang

    Xenos Guest

    "Victor Bazarov" <> wrote in message
    news:jMKzb.226707$Dw6.803084@attbi_s02...
    > "Xenos" <> wrote...
    > >
    > > "Karl Heinz Buchegger" <> wrote in message
    > > news:...
    > >
    > > > you can:
    > > >
    > > > #include <iostream>
    > > > using namespace std;
    > > >
    > > > class A
    > > > {
    > > > public:
    > > > int m_n;
    > > > };
    > > >
    > > > int main()
    > > > {
    > > > A* pA = new A;
    > > > A* pB = new A();
    > > >
    > > >
    > > > Here pA->m_n is left uninitialized while
    > > > pB->m_n should be default initialized.
    > > >
    > > > VC++ 6.0 however gets this wrong and I suspect that
    > > > most compiler get this wrong. In other words: don't
    > > > depend on it.
    > > >
    > > > Oh, by the way: Yes in the above, class A is a POD.
    > > >

    > > I don't care what you call it, both uses of the new operator will call

    the
    > > default constructor.

    >
    > There is no default constructor for an int.
    >
    > Now, are these two forms the same
    >
    > int a;
    >
    > and
    >
    > int a = int();
    >
    > ? If not, what's the difference? What value does 'a' get in each
    > case?
    >
    > And if you don't know that POD is (or what it stands for), perhaps
    > you need to learn first, then begin arguing...
    >
    > Victor
    >
    >


    If you refer back to my statement, I was talking about classes, not
    primative types. The OP was asking about a struct, not an int.

    And if you can't even read someone's statement, maybe you should learn
    English before arguing.

    DrX
     
    Xenos, Dec 4, 2003
    #8
  9. Hongzheng Wang

    Pete Becker Guest

    Xenos wrote:
    >
    > "Victor Bazarov" <> wrote in message
    > news:jMKzb.226707$Dw6.803084@attbi_s02...
    > > "Xenos" <> wrote...
    > > >
    > > > "Karl Heinz Buchegger" <> wrote in message
    > > > news:...
    > > >
    > > > > you can:
    > > > >
    > > > > #include <iostream>
    > > > > using namespace std;
    > > > >
    > > > > class A
    > > > > {
    > > > > public:
    > > > > int m_n;
    > > > > };
    > > > >
    > > > > int main()
    > > > > {
    > > > > A* pA = new A;
    > > > > A* pB = new A();
    > > > >
    > > > >
    > > > > Here pA->m_n is left uninitialized while
    > > > > pB->m_n should be default initialized.
    > > > >
    > > > > VC++ 6.0 however gets this wrong and I suspect that
    > > > > most compiler get this wrong. In other words: don't
    > > > > depend on it.
    > > > >
    > > > > Oh, by the way: Yes in the above, class A is a POD.
    > > > >
    > > > I don't care what you call it, both uses of the new operator will call

    > the
    > > > default constructor.

    > >
    > > There is no default constructor for an int.
    > >
    > > Now, are these two forms the same
    > >
    > > int a;
    > >
    > > and
    > >
    > > int a = int();
    > >
    > > ? If not, what's the difference? What value does 'a' get in each
    > > case?
    > >
    > > And if you don't know that POD is (or what it stands for), perhaps
    > > you need to learn first, then begin arguing...
    > >
    > > Victor
    > >
    > >

    >
    > If you refer back to my statement, I was talking about classes, not
    > primative types. The OP was asking about a struct, not an int.
    >


    What Victor said is absolutely correct: the two forms are different for
    a POD. And a struct or a class can be a POD.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Dec 4, 2003
    #9
  10. Hongzheng Wang

    Xenos Guest

    "Pete Becker" <> wrote in message
    news:...
    > Xenos wrote:
    > >
    > > "Victor Bazarov" <> wrote in message
    > > news:jMKzb.226707$Dw6.803084@attbi_s02...
    > > > "Xenos" <> wrote...
    > > > >
    > > > > "Karl Heinz Buchegger" <> wrote in message
    > > > > news:...
    > > > >
    > > > > > you can:
    > > > > >
    > > > > > #include <iostream>
    > > > > > using namespace std;
    > > > > >
    > > > > > class A
    > > > > > {
    > > > > > public:
    > > > > > int m_n;
    > > > > > };
    > > > > >
    > > > > > int main()
    > > > > > {
    > > > > > A* pA = new A;
    > > > > > A* pB = new A();
    > > > > >
    > > > > >
    > > > > > Here pA->m_n is left uninitialized while
    > > > > > pB->m_n should be default initialized.
    > > > > >
    > > > > > VC++ 6.0 however gets this wrong and I suspect that
    > > > > > most compiler get this wrong. In other words: don't
    > > > > > depend on it.
    > > > > >
    > > > > > Oh, by the way: Yes in the above, class A is a POD.
    > > > > >
    > > > > I don't care what you call it, both uses of the new operator will

    call
    > > the
    > > > > default constructor.
    > > >
    > > > There is no default constructor for an int.
    > > >
    > > > Now, are these two forms the same
    > > >
    > > > int a;
    > > >
    > > > and
    > > >
    > > > int a = int();
    > > >
    > > > ? If not, what's the difference? What value does 'a' get in each
    > > > case?
    > > >
    > > > And if you don't know that POD is (or what it stands for), perhaps
    > > > you need to learn first, then begin arguing...
    > > >
    > > > Victor
    > > >
    > > >

    > >
    > > If you refer back to my statement, I was talking about classes, not
    > > primative types. The OP was asking about a struct, not an int.
    > >

    >
    > What Victor said is absolutely correct: the two forms are different for
    > a POD. And a struct or a class can be a POD.
    >
    > --
    >
    > Pete Becker
    > Dinkumware, Ltd. (http://www.dinkumware.com)


    I didn't say a struct couldn't be. But the different between the two forms
    is largely syntactical. In reality, there is no difference in code
    generation. If you want to think of them as meaning different things, fine.
    It not worth arguing over.

    DrX
     
    Xenos, Dec 4, 2003
    #10
  11. Hongzheng Wang

    Ron Natalie Guest

    "Xenos" <> wrote in message news:bqoc3m$...

    >
    > I didn't say a struct couldn't be. But the different between the two forms
    > is largely syntactical. In reality, there is no difference in code
    > generation. If you want to think of them as meaning different things, fine.
    > It not worth arguing over.
    >

    There's a substantial difference in code generation. It's not a "fine meaning."

    class pod {
    public:
    int x;
    };

    new pod; // yields an uninitialized pod (x has indeterminate value).
    new pod(); // yields a default initialized pod (x has value 0).
     
    Ron Natalie, Dec 4, 2003
    #11
  12. Hongzheng Wang wrote:

    > I saw some codes use `new classname'
    > to get a point to a new object of classname
    > but some codes use `new classname()'instead.
    > Is these two usage equivalent?


    Yes.
     
    E. Robert Tisdale, Dec 4, 2003
    #12
  13. "E. Robert Tisdale" <> wrote...
    > Hongzheng Wang wrote:
    >
    > > I saw some codes use `new classname'
    > > to get a point to a new object of classname
    > > but some codes use `new classname()'instead.
    > > Is these two usage equivalent?

    >
    > Yes.


    'nuther wun... Read 5.3.4/15 before continuing.
     
    Victor Bazarov, Dec 4, 2003
    #13
  14. Hongzheng Wang

    Pete Becker Guest

    Xenos wrote:
    >
    >
    > I didn't say a struct couldn't be. But the different between the two forms
    > is largely syntactical. In reality, there is no difference in code
    > generation. If you want to think of them as meaning different things, fine.


    There is also a difference in semantics. There is a difference in code
    generation, because they mean different things. If you would stop being
    arrogant long enough to read what everyone is saying you might learn
    something.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Dec 5, 2003
    #14
  15. Victor Bazarov wrote:

    > Hongzheng Wang wrote:
    >
    >>I saw some codes use `new classname'
    >>to get a pointer to a new object of classname
    >>but some codes use`new classname()' instead.
    >>Are these two usages equivalent?

    >
    > For Plain Old Data (POD) they are not.


    See the C++ FAQ [26.7] What is a "POD type"?
    http://www.parashift.com/c -faq-lite/intrinsic-types.html#faq-26.7

    > The former leaves it uninitialized,
    > the latter makes it default-initialized, IIRC.


    I don't think so.
    I believe it is left up to the implementation
    to determine whether the object is "initialized" or not.
     
    E. Robert Tisdale, Dec 5, 2003
    #15
  16. Hongzheng Wang

    Ali Cehreli Guest

    Karl Heinz Buchegger <> wrote in message news:<>...
    > Xenos wrote:
    > >
    > > "Victor Bazarov" <> wrote in message
    > > news:akyzb.416658$Fm2.425058@attbi_s04...
    > > > "Hongzheng Wang" <> wrote...
    > > > > I saw some codes use `new classname' to get a point to
    > > > > a new object of classname, but some codes, instead, use
    > > > > `new classname()'.
    > > > > Is these two usage equivalent?
    > > >
    > > > For POD they are not. The former leaves it uninitialised,
    > > > the latter makes it default-initialised, IIRC.
    > > >
    > > > Victor
    > > >
    > > >

    > > No, both would cause default initialization.

    >
    > They don't
    >
    > > You cannot create an
    > > uninitialized class object without a kludge.
    > >

    >
    > you can:
    >
    > #include <iostream>
    > using namespace std;
    >
    > class A
    > {
    > public:
    > int m_n;
    > };


    A does not have a user-defined constructor. The compiler generated one
    will be similar to the following:

    A::A() {}

    In other words, the compiler will not write the constructor like this:

    A::A() : m_n() {}

    which would be the equivalent of

    A::A() : m_n(0) {}

    The built-in types are never default constructed.

    > int main()
    > {
    > A* pA = new A;
    > A* pB = new A();


    Both call the default constructor.

    > Here pA->m_n is left uninitialized while
    > pB->m_n should be default initialized.


    *pA and *pB are both default constructed, but since the compiler
    generated constructor does not initialize the built-in types, m_n
    members are both left uninitialized.

    > VC++ 6.0 however gets this wrong and I suspect that
    > most compiler get this wrong. In other words: don't
    > depend on it.


    None of those compilers are wrong if they don't initialize the
    built-in members. They are not required to default-initialize built-in
    types.

    Ali
     
    Ali Cehreli, Dec 5, 2003
    #16
  17. Xenos wrote:
    >> you can:
    >>
    >> #include <iostream>
    >> using namespace std;
    >>
    >> class A
    >> {
    >> public:
    >> int m_n;
    >> };
    >>
    >> int main()
    >> {
    >> A* pA = new A;
    >> A* pB = new A();
    >>
    >>
    >> Here pA->m_n is left uninitialized while
    >> pB->m_n should be default initialized.
    >>
    >> VC++ 6.0 however gets this wrong and I suspect that
    >> most compiler get this wrong. In other words: don't
    >> depend on it.
    >>
    >> Oh, by the way: Yes in the above, class A is a POD.
    >>

    > I don't care what you call it, both uses of the new operator will call the
    > default constructor.


    Wrong. For POD types _neither_ form will call default constructor. The
    first form will left object uninitialized. The second form will
    defualt-initialize it (which means zero-initialization, not a
    constructor call).

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Dec 5, 2003
    #17
  18. Xenos wrote:
    > ...
    > I didn't say a struct couldn't be. But the different between the two forms
    > is largely syntactical. In reality, there is no difference in code
    > generation. If you want to think of them as meaning different things, fine.
    > It not worth arguing over.
    > ...


    Wrong again. The difference in code generation is extremely hard to
    miss. '()' form causes default-initialization, which boils down to
    zero-initialization for POD types. Zero-initialization requires
    additional code. '()'-less form leaves the object non-initialized, which
    means that no initialization-related code is generated.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Dec 5, 2003
    #18
  19. E. Robert Tisdale wrote:

    > Victor Bazarov wrote:
    >
    >> Hongzheng Wang wrote:
    >>
    >>>I saw some codes use `new classname'
    >>>to get a pointer to a new object of classname
    >>>but some codes use`new classname()' instead.
    >>>Are these two usages equivalent?

    >>
    >> For Plain Old Data (POD) they are not.

    >
    > See the C++ FAQ [26.7] What is a "POD type"?
    > http://www.parashift.com/c -faq-lite/intrinsic-types.html#faq-26.7
    > ...


    So? Once again, the effect of '()' initializer on different kinds of
    data types in new-expression is very clearly described in the standard.
    See 5.3.4/15.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Dec 5, 2003
    #19
  20. Hongzheng Wang

    Pete Becker Guest

    Ali Cehreli wrote:
    >
    > Karl Heinz Buchegger <> wrote in message news:<>...
    > > Xenos wrote:
    > > >
    > > > "Victor Bazarov" <> wrote in message
    > > > news:akyzb.416658$Fm2.425058@attbi_s04...
    > > > > "Hongzheng Wang" <> wrote...
    > > > > > I saw some codes use `new classname' to get a point to
    > > > > > a new object of classname, but some codes, instead, use
    > > > > > `new classname()'.
    > > > > > Is these two usage equivalent?
    > > > >
    > > > > For POD they are not. The former leaves it uninitialised,
    > > > > the latter makes it default-initialised, IIRC.
    > > > >
    > > > > Victor
    > > > >
    > > > >
    > > > No, both would cause default initialization.

    > >
    > > They don't
    > >
    > > > You cannot create an
    > > > uninitialized class object without a kludge.
    > > >

    > >
    > > you can:
    > >
    > > #include <iostream>
    > > using namespace std;
    > >
    > > class A
    > > {
    > > public:
    > > int m_n;
    > > };

    >
    > A does not have a user-defined constructor. The compiler generated one
    > will be similar to the following:
    >
    > A::A() {}
    >
    > In other words, the compiler will not write the constructor like this:
    >
    > A::A() : m_n() {}
    >
    > which would be the equivalent of
    >
    > A::A() : m_n(0) {}
    >
    > The built-in types are never default constructed.
    >


    Read paragraph 5.3.4/15 of the C++ Standard. Especially the part where
    it says "if the new-initializer is of the form (), the item is
    value-initialized." Then see 8.5/5 for the definition of
    'value-initialized'. It says, in effect, that for built-in types
    value-initializing means zero-initializing.

    int *ip = new int(); // value-initialized (i.e. zero-initialized)
    struct S
    {
    int i;
    };

    S *sp = new S; // not initialized
    S *sp1 = new S(); // value-initialized (S::i gets zero-initialized)

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Dec 5, 2003
    #20
    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. Charles A. Lackman

    className Attribute (UserControls)

    Charles A. Lackman, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    419
    Charles A. Lackman
    Jul 8, 2003
  2. noname

    global.asax classname

    noname, Jul 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    378
    Graham
    Jul 16, 2003
  3. Oliver S.

    classname::classname a type ?

    Oliver S., Sep 11, 2003, in forum: C++
    Replies:
    1
    Views:
    507
    White Wolf
    Sep 11, 2003
  4. Luca Scaljery
    Replies:
    5
    Views:
    104
    Patrick Spence
    Feb 14, 2007
  5. Ante Perkovic

    CSS: "tagname.classname" or ".classname"

    Ante Perkovic, Dec 22, 2003, in forum: Javascript
    Replies:
    2
    Views:
    107
Loading...

Share This Page