Default initialization with dynamic allocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcus Kwok

    #1

    Default initialization with dynamic allocation

    Given a user defined class with a default constructor (let's call the
    class C), is there any semantic difference between

    C* c = new C;

    and

    C* c = new C();

    ? I am thinking they are equivalent, as demonstrated by the program
    listing below. If not, which style is more commonly preferred?

    I know with primitive types, e.g. int, there is a difference.


    #include <iostream>

    class C {
    public:
    C() { std::cout << "C()\n"; }
    };

    int main()
    {
    C* a = new C;
    C* b = new C();

    delete a;
    delete b;

    int* c = new int;
    int* d = new int();

    std::cout << *c << '\n' << *d << '\n';

    delete c;
    delete d;

    return 0;
    }


    Output I get is:
    C()
    C()
    3277336
    0

    --
    Marcus Kwok
  • Victor Bazarov

    #2
    Re: Default initialization with dynamic allocation

    Marcus Kwok wrote:[color=blue]
    > Given a user defined class with a default constructor (let's call the
    > class C), is there any semantic difference between
    >
    > C* c = new C;
    >
    > and
    >
    > C* c = new C();[/color]

    No.
    [color=blue]
    > ? I am thinking they are equivalent, as demonstrated by the program
    > listing below. If not, which style is more commonly preferred?[/color]

    I prefer the latter when I need to ensure zero-initialisation for PODs and
    the former for POD when I need to leave it uninitialised.
    [color=blue]
    > I know with primitive types, e.g. int, there is a difference.[/color]

    The difference is not only with primitive types. The difference is with
    all POD types.
    [color=blue]
    > [..][/color]

    V
    --
    Please remove capital As from my address when replying by mail

    Comment

    • Marcus Kwok

      #3
      Re: Default initialization with dynamic allocation

      Victor Bazarov <v.Abazarov@com acast.net> wrote:[color=blue]
      > Marcus Kwok wrote:[color=green]
      >> Given a user defined class with a default constructor (let's call the
      >> class C), is there any semantic difference between
      >>
      >> C* c = new C;
      >>
      >> and
      >>
      >> C* c = new C();[/color]
      >
      > No.[/color]

      Thanks, that's what I thought.
      [color=blue][color=green]
      >> I know with primitive types, e.g. int, there is a difference.[/color]
      >
      > The difference is not only with primitive types. The difference is with
      > all POD types.[/color]

      Thanks, sorry for my imprecise language.

      --
      Marcus Kwok

      Comment

      • davidrubin@warpmail.net

        #4
        Re: Default initialization with dynamic allocation


        Victor Bazarov wrote:[color=blue]
        > Marcus Kwok wrote:[color=green]
        > > Given a user defined class with a default constructor (let's call the
        > > class C), is there any semantic difference between
        > >
        > > C* c = new C;
        > >
        > > and
        > >
        > > C* c = new C();[/color]
        >
        > No.
        >[color=green]
        > > ? I am thinking they are equivalent, as demonstrated by the program
        > > listing below. If not, which style is more commonly preferred?[/color]
        >
        > I prefer the latter when I need to ensure zero-initialisation for PODs and
        > the former for POD when I need to leave it uninitialised.[/color]

        This makes it sound like there *is* a semantic difference: in the first
        case, zero initialization of PODs in not guaranteed, while in the
        second case, it is. Can you please elaborate?

        Comment

        • David Lindauer

          #5
          Re: Default initialization with dynamic allocation



          Victor Bazarov wrote:
          [color=blue]
          > Marcus Kwok wrote:[color=green]
          > > Given a user defined class with a default constructor (let's call the
          > > class C), is there any semantic difference between
          > >
          > > C* c = new C;
          > >
          > > and
          > >
          > > C* c = new C();[/color]
          >
          > No.
          >[color=green]
          > > ? I am thinking they are equivalent, as demonstrated by the program
          > > listing below. If not, which style is more commonly preferred?[/color]
          >
          > I prefer the latter when I need to ensure zero-initialisation for PODs and
          > the former for POD when I need to leave it uninitialised.
          >[color=green]
          > > I know with primitive types, e.g. int, there is a difference.[/color]
          >
          > The difference is not only with primitive types. The difference is with
          > all POD types.[/color]

          can you clarify what a POD type is?


          Comment

          • Peter_Julian

            #6
            Re: Default initialization with dynamic allocation


            "David Lindauer" <camille@bluegr ass.net> wrote in message
            news:43EDFAE5.2 419B1A6@bluegra ss.net...
            |
            |
            | Victor Bazarov wrote:
            |
            | > Marcus Kwok wrote:
            | > > Given a user defined class with a default constructor (let's call
            the
            | > > class C), is there any semantic difference between
            | > >
            | > > C* c = new C;
            | > >
            | > > and
            | > >
            | > > C* c = new C();
            | >
            | > No.
            | >
            | > > ? I am thinking they are equivalent, as demonstrated by the
            program
            | > > listing below. If not, which style is more commonly preferred?
            | >
            | > I prefer the latter when I need to ensure zero-initialisation for
            PODs and
            | > the former for POD when I need to leave it uninitialised.
            | >
            | > > I know with primitive types, e.g. int, there is a difference.
            | >
            | > The difference is not only with primitive types. The difference is
            with
            | > all POD types.
            |
            | can you clarify what a POD type is?
            |
            |

            Plain Old Data (section 26.7):



            Comment

            • David Lindauer

              #7
              Re: Default initialization with dynamic allocation



              Peter_Julian wrote:
              [color=blue]
              > "David Lindauer" <camille@bluegr ass.net> wrote in message
              > news:43EDFAE5.2 419B1A6@bluegra ss.net...
              > |
              > |
              > | Victor Bazarov wrote:
              > |
              > | > Marcus Kwok wrote:
              > | > > Given a user defined class with a default constructor (let's call
              > the
              > | > > class C), is there any semantic difference between
              > | > >
              > | > > C* c = new C;
              > | > >
              > | > > and
              > | > >
              > | > > C* c = new C();
              > | >
              > | > No.
              > | >
              > | > > ? I am thinking they are equivalent, as demonstrated by the
              > program
              > | > > listing below. If not, which style is more commonly preferred?
              > | >
              > | > I prefer the latter when I need to ensure zero-initialisation for
              > PODs and
              > | > the former for POD when I need to leave it uninitialised.
              > | >
              > | > > I know with primitive types, e.g. int, there is a difference.
              > | >
              > | > The difference is not only with primitive types. The difference is
              > with
              > | > all POD types.
              > |
              > | can you clarify what a POD type is?
              > |
              > |
              >
              > Plain Old Data (section 26.7):
              > http://www.parashift.com/c++-faq-lit...sic-types.html[/color]

              ok thanks. Now I know that classes get default constructors & assignment
              operator if you don't create any, but if I make a POD like this:

              struct aa
              {
              int a;
              int b;
              } ;

              I assume it wouldn't get default constructors? But if I do anything to
              make it a non-POD it would?

              Thanks,

              David

              Comment

              • Victor Bazarov

                #8
                Re: Default initialization with dynamic allocation

                davidrubin@warp mail.net wrote:[color=blue]
                > Victor Bazarov wrote:[color=green]
                >> Marcus Kwok wrote:[color=darkred]
                >>> Given a user defined class with a default constructor (let's call
                >>> the class C), is there any semantic difference between
                >>>
                >>> C* c = new C;
                >>>
                >>> and
                >>>
                >>> C* c = new C();[/color]
                >>
                >> No.
                >>[color=darkred]
                >>> ? I am thinking they are equivalent, as demonstrated by the program
                >>> listing below. If not, which style is more commonly preferred?[/color]
                >>
                >> I prefer the latter when I need to ensure zero-initialisation for
                >> PODs and the former for POD when I need to leave it uninitialised.[/color]
                >
                > This makes it sound like there *is* a semantic difference: in the
                > first case, zero initialization of PODs in not guaranteed, while in
                > the second case, it is. Can you please elaborate?[/color]

                Paragraph 5.3.4/15 of the Standard elaborates on that. What else do
                you expect me to add?

                V
                --
                Please remove capital As from my address when replying by mail


                Comment

                • davidrubin@warpmail.net

                  #9
                  Re: Default initialization with dynamic allocation

                  Do you feel like adding more? Otherwise, that is fine for me. Thanks.

                  Comment

                  Working...