Search code examples
c++placement-newnew-operator

operator new for array of class without default constructor


For a class without default constructor, operator new and placement new can be used to declare an array of such class.

When I read the code in More Effective C++, I found the code as below(I modified some part).....

My question is, why [] after the operator new is needed?

I test it without it, it still works. Can any body explain that?

class A {
    public:
    int i;

    A(int i):i(i) {}
};

int main()
{
      void *rawMemory = operator new[] (10 * sizeof(A));   // Why [] needed here?
      A *p = static_cast<A*>(rawMemory);

      for(int i = 0 ; i < 10 ; i++ ) {

            new(&p[i])A(i); 

      }

      for(int i = 0 ; i < 10 ; i++ ) {

            cout<<p[i].i<<endl;

      }

      for(int i = 0 ; i < 10 ; i++ ) {

            p[i].~A();

      }

    return 0;
}

Solution

  • I'm surprised that Effective C++ would be advising you to use something as hackish as a void*.

    new[] does a very specific thing: it allocates a dynamically sized array. An array allocated with it should be passed to delete[]. delete[] then reads a hidden number to find how many elements are in the array, and destroys the objects as you have done with p[i].~A();.

    However, this usage is incompatible with that. The array is statically sized, and there's no way to get that hidden number or dynamic-size destruction without properly using new[] (no operator), in turn requiring a default constructor. A genuine weakness of C++.

    If you called delete[] at the end of main as others have suggested, your code could crash. Instead you need to use operator delete[], which looks like a typo and is just an accident waiting to happen.

    Use non-array operator new and operator delete and ample comments if you must use this trick. But I wouldn't consider this particularly effective C++.