Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ dynamic allocation of class array

Assume a class X with a constructor function X(int a, int b)

I create a pointer to X as X *ptr; to allocate memory dynamically for the class.

Now to create an array of object of class X

 ptr = new X[sizeOfArray];

until now everything is fine. But what I want to do is creation of the above array of objects should invoke the constructor function X(int a, int b). I tried as follows:

ptr = new X(1,2)[sizeOfArray]; 

As expected It gave me compile time error

error: expected ';' before '[' token|

How can I create an array of objects to invoke the constructor?

SizeOfArray is entered by the user at runtime.

EDIT: What I wanted to achieve in not possible as answered by zenith or will be too complex . So how can I use std::vector for the same?

like image 726
Pushkar Avatar asked Oct 22 '15 14:10

Pushkar


2 Answers

This seems like a job for placement new...

Here's a basic example:

Run It Online !

#include <iostream>
#include <cstddef>  // size_t
#include <new>      // placement new

using std::cout;
using std::endl;

struct X
{
    X(int a_, int b_) : a{a_}, b{b_} {}
    int a;
    int b;
};

int main()
{
    const size_t element_size   = sizeof(X);
    const size_t element_count  = 10;

    // memory where new objects are going to be placed
    char* memory = new char[element_count * element_size];

    // next insertion index
    size_t insertion_index = 0;

    // construct a new X in the address (place + insertion_index)
    void* place = memory + insertion_index;
    X* x = new(place) X(1, 2);
    // advance the insertion index
    insertion_index += element_size;

    // check out the new object
    cout << "x(" << x->a << ", " << x->b << ")" << endl;

    // explicit object destruction
    x->~X();

    // free the memory
    delete[] memory;
}

EDIT: If I've understood your edit, you want to do something like this:

Run It Online !

#include <vector>
// init a vector of `element_count x X(1, 2)`
std::vector<X> vec(element_count, X(1, 2));

// you can still get a raw pointer to the array as such
X* ptr1 = &vec[0];
X* ptr2 = vec.data();  // C++11
like image 64
maddouri Avatar answered Sep 30 '22 01:09

maddouri


This is not possible in the current C++ standard, unless:

  • you provide an initializer for each element, or
  • you use a vector.

See:

  • Object array initialization without default constructor
  • How do I declare an array of objects whose class has no default constructor?
like image 42
emlai Avatar answered Sep 30 '22 01:09

emlai