Dynamic array of objects - initialization

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

    Dynamic array of objects - initialization

    Hello everyone,

    I want to create an array of objects at run-time.

    AFAIU, operator new[] will call the default constructor for each object
    in the array. In other words, the following program will print INSIDE
    DEFAULT CTOR five times.

    #include <vector>
    #include <cstdio>

    struct Foo
    {
    Foo(int u) {
    puts("INSIDE CTOR"); p = new char[u];
    }
    Foo() {
    puts("INSIDE DEFAULT CTOR"); p = new char[666];
    }
    ~Foo() {
    puts("INSIDE DTOR"); delete[] p;
    }
    char *p;
    };

    int main()
    {
    Foo *ww = new Foo[5];
    return 0;
    }

    $ g++ -Wall -g3 vectest.cxx
    vectest.cxx: In function `int main()':
    vectest.cxx:20: warning: unused variable 'ww'
    $ ./a.out
    INSIDE DEFAULT CTOR
    INSIDE DEFAULT CTOR
    INSIDE DEFAULT CTOR
    INSIDE DEFAULT CTOR
    INSIDE DEFAULT CTOR

    What if I want to use a different constructor?

    For example, how can I get the program to create an array of 5 objects
    that hold a 123-byte buffer?

    Foo *ww = new Foo(123)[5];

    is a syntax error. Am I missing something obvious?

    I suppose I could add a static variable to class Foo and have the
    default constructor use the value of that variable...

    struct Foo
    {
    Foo(int u) {
    puts("INSIDE CTOR"); p = new char[u];
    }
    Foo() {
    puts("INSIDE DEFAULT CTOR"); p = new char[666];
    }
    ~Foo() {
    puts("INSIDE DTOR"); delete[] p;
    }
    char *p;
    static int defaultsize;
    };

    int Foo::defaultsiz e = 0;

    int main()
    {
    Foo::defaultsiz e = 123;
    Foo *ww = new Foo[5];
    return 0;
    }

    But that feels like a kludge. Is there a better solution?

    On a related note, would a vector help in this situation?

    I could write something along the lines of

    std::vector < Foo v;
    v.reserve(N);
    for (int i=0; i < N; ++i)
    {
    Foo *curr = new Foo(size)
    v.push_back(*cu rr);
    }

    But that feels somewhat like a kludge too.

    Regards.
  • Victor Bazarov

    #2
    Re: Dynamic array of objects - initialization

    Spoon wrote:
    I want to create an array of objects at run-time.
    >
    AFAIU, operator new[] will call the default constructor for each
    object in the array. In other words, the following program will print
    INSIDE DEFAULT CTOR five times.
    >
    #include <vector>
    #include <cstdio>
    >
    struct Foo
    {
    Foo(int u) {
    puts("INSIDE CTOR"); p = new char[u];
    }
    Foo() {
    puts("INSIDE DEFAULT CTOR"); p = new char[666];
    }
    ~Foo() {
    puts("INSIDE DTOR"); delete[] p;
    }
    char *p;
    };
    >
    int main()
    {
    Foo *ww = new Foo[5];
    return 0;
    }
    >
    $ g++ -Wall -g3 vectest.cxx
    vectest.cxx: In function `int main()':
    vectest.cxx:20: warning: unused variable 'ww'
    $ ./a.out
    INSIDE DEFAULT CTOR
    INSIDE DEFAULT CTOR
    INSIDE DEFAULT CTOR
    INSIDE DEFAULT CTOR
    INSIDE DEFAULT CTOR
    >
    What if I want to use a different constructor?
    You're SOL.
    For example, how can I get the program to create an array of 5 objects
    that hold a 123-byte buffer?
    >
    Foo *ww = new Foo(123)[5];
    >
    is a syntax error. Am I missing something obvious?
    Not really. There is no way to do what you want in a single statement.
    I suppose I could add a static variable to class Foo and have the
    default constructor use the value of that variable...
    >
    struct Foo
    {
    Foo(int u) {
    puts("INSIDE CTOR"); p = new char[u];
    }
    Foo() {
    puts("INSIDE DEFAULT CTOR"); p = new char[666];
    }
    ~Foo() {
    puts("INSIDE DTOR"); delete[] p;
    }
    char *p;
    static int defaultsize;
    };
    >
    int Foo::defaultsiz e = 0;
    >
    int main()
    {
    Foo::defaultsiz e = 123;
    Foo *ww = new Foo[5];
    return 0;
    }
    >
    But that feels like a kludge. Is there a better solution?
    Use std::vector.
    On a related note, would a vector help in this situation?
    Yep.
    I could write something along the lines of
    >
    std::vector < Foo v;
    v.reserve(N);
    for (int i=0; i < N; ++i)
    {
    Foo *curr = new Foo(size)
    v.push_back(*cu rr);
    }
    >
    But that feels somewhat like a kludge too.
    Whatever you choose to call it. I call it "a work-around".

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • =?iso-8859-1?q?Erik_Wikstr=F6m?=

      #3
      Re: Dynamic array of objects - initialization

      On 19 Mar, 15:07, Spoon <devn...@localh ost.comwrote:
      Hello everyone,
      >
      I want to create an array of objects at run-time.
      >
      AFAIU, operator new[] will call the default constructor for each object
      in the array. In other words, the following program will print INSIDE
      DEFAULT CTOR five times.
      >
      #include <vector>
      #include <cstdio>
      >
      struct Foo
      {
      Foo(int u) {
      puts("INSIDE CTOR"); p = new char[u];
      }
      Foo() {
      puts("INSIDE DEFAULT CTOR"); p = new char[666];
      }
      ~Foo() {
      puts("INSIDE DTOR"); delete[] p;
      }
      char *p;
      >
      };
      >
      int main()
      {
      Foo *ww = new Foo[5];
      return 0;
      >
      }
      >
      $ g++ -Wall -g3 vectest.cxx
      vectest.cxx: In function `int main()':
      vectest.cxx:20: warning: unused variable 'ww'
      $ ./a.out
      INSIDE DEFAULT CTOR
      INSIDE DEFAULT CTOR
      INSIDE DEFAULT CTOR
      INSIDE DEFAULT CTOR
      INSIDE DEFAULT CTOR
      >
      What if I want to use a different constructor?
      >
      For example, how can I get the program to create an array of 5 objects
      that hold a 123-byte buffer?
      >
      Foo *ww = new Foo(123)[5];
      >
      is a syntax error. Am I missing something obvious?
      >
      I suppose I could add a static variable to class Foo and have the
      default constructor use the value of that variable...
      >
      struct Foo
      {
      Foo(int u) {
      puts("INSIDE CTOR"); p = new char[u];
      }
      Foo() {
      puts("INSIDE DEFAULT CTOR"); p = new char[666];
      }
      ~Foo() {
      puts("INSIDE DTOR"); delete[] p;
      }
      char *p;
      static int defaultsize;
      >
      };
      >
      int Foo::defaultsiz e = 0;
      >
      int main()
      {
      Foo::defaultsiz e = 123;
      Foo *ww = new Foo[5];
      return 0;
      >
      }
      >
      But that feels like a kludge. Is there a better solution?
      >
      On a related note, would a vector help in this situation?
      >
      I could write something along the lines of
      >
      std::vector < Foo v;
      v.reserve(N);
      for (int i=0; i < N; ++i)
      {
      Foo *curr = new Foo(size)
      v.push_back(*cu rr);
      >
      }
      std::vector<Foo v(5, Foo(4));

      --
      Erik Wikström

      Comment

      • Victor Bazarov

        #4
        Re: Dynamic array of objects - initialization

        Erik Wikström wrote:
        On 19 Mar, 15:07, Spoon <devn...@localh ost.comwrote:
        >[..]
        >
        std::vector<Foo v(5, Foo(4));
        ... along with a proper implementation of the copy c-tor.

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask


        Comment

        Working...