0

How do I create array of objects using placement new operator? I know how to do it for single object from other SO questions. But I couldn't find for array of objects.

To see the performance difference I am trying to create an array of objects and deleting after the subloop. But I am not able to find a way. How do I create multiple objects?

class Complex
{
  double r; // Real Part
  double c; // Complex Part

  public:
    Complex() : r(0), c(0) {}
    Complex (double a, double b): r (a), c (b) {}
    void *operator new( std::size_t size,void* buffer)
    {
        return buffer;
    }
};

char *buf  = new char[sizeof(Complex)*1000]; // pre-allocated buffer

int main(int argc, char* argv[])
{
  Complex* arr;
  for (int i = 0;i < 200000; i++) {
    //arr = new(buf) Complex(); This just create single object.
    for (int j = 0; j < 1000; j++) {
      //arr[j] = Now how do I assign values if only single obect is created?
    }
    arr->~Complex();
  }
return 0;
}
4
  • Did you try new(buff) Complex[1000]? As in, just add the size of the array to it? Note, however, that placement array new can require more space in a platform dependent way (ick), so a loop calling placement new on each element can be more portable sometiems. Second, why are you overloading operator new above? Is your goal to call a user defined operator new when doing placement new? Commented Mar 13, 2017 at 19:19
  • This I was not aware. I will try this. Also I won't be able to create it with parameterized constructor right? Commented Mar 13, 2017 at 19:22
  • sourcetricks.com/2008/05/c-placement-new.html#.WMbfiyGGPak I was following this link. I thought I need to overload operator new for placement new to work. Commented Mar 13, 2017 at 19:23
  • @InQusitive they redefined it to print message, but technically they should have been defining it through existing new, you can't supply parameters if you create array, but you can do if you calculate storage address and create one by one Commented Mar 13, 2017 at 20:33

1 Answer 1

2

What's the purpose of overriding a standard-defined new operator to rather useless function? And how you suppose to store pointers if you create one by one

#include <iostream>

class Complex
{
  double r; // Real Part
  double c; // Complex Part

  public:
    Complex() : r(0), c(0) {}
    Complex (double a, double b): r (a), c (b) {}
};

char *buf  = new char[sizeof(Complex)*1000]; // pre-allocated buffer

int main(int argc, char* argv[])
{ 
    // When doing this, it's _your_ problem 
    // that supplied storage  is aligned proeperly and got 
    // enough storage space

    // Create them all
    // Complex* arr = new(buf) Complex[1000];

    Complex** arr = new Complex*[1000];
    for (int j = 0; j < 1000; j++)      
        arr[j] = new (buf + j*sizeof(Complex)) Complex;

    for (int j = 0; j < 1000; j++) 
        arr[j]->~Complex();

    delete[] buf;
    return 0;
}

If you going to design any infrastructure based on placement new , you most likely need to create a factory class to construct and store obejcts in and to control the buffer, using RAII. Such classes\patterns are generally called memory pools. The only practical pool I ever saw implemented was storing arrays and classes of different size and type, using special class to store a reference to such object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.