0

Does the following create x new objects, or simply allocate space for x objects?:

Vector3D* binArray = new Vector3D[size];

I need to build an array with space for x Vector3D objects on the heap. However, a Vector3D object can only be created when an "add" function is called - this will take the parameters, construct the object on the heap and add its address to the array of Vector3D pointers.

2
  • You've called new - this means you will be calling constructors. Commented Nov 14, 2017 at 19:00
  • 1
    Use std::vector! Commented Nov 14, 2017 at 20:17

2 Answers 2

4

This does create an array of Vector3D objects on the heap.

Each vector is created by calling the Vector3D constructor.

Put a little debugging print statement in the default constructor for Vector3D, and watch the constructor get called the same number of times as you have vectors in your array.

Example:

#include <iostream>
using namespace std;

class C {
public:
  C() { cout << "Hello default constructor here\n"; }
};

int main() {
  C* cs = new C[5];
}

Output is:

Hello default constructor here
Hello default constructor here
Hello default constructor here
Hello default constructor here
Hello default constructor here

If your class does not have a default constructor, you cannot allocate the array in one shot (thank you for the comment @Everyone), so in this case consider using a std::vector or a std::array and adding your Vector3D objects dynamically --- or even "statically"! Example:

#include <iostream>
#include <vector>
using namespace std;

class Vector3D {
  double i, j, k;
public:
  Vector3D(double i, double j, double k): i(i), j(j), k(k) {}
};

int main() {
  vector<Vector3D> v = {
    Vector3D(3, 4, 5),
    Vector3D(6, 8, 10),
    Vector3D(7, 24, 25)
  };
  v.push_back(Vector3D(1, 2, 3));
  cout << v.size() << '\n';
}

This outputs 4.

You can also make your vector contain pointers to Vector3D objects.

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

12 Comments

I think it's worth to note that if he doesn't have a default constructor this cannot work. Just for the sake of completion.
So, how can I have an array of Vector3D objects (of size x) as a private member and then construct and allocate each object dynamically individually.
You want an array as a member but you want each of the members of the array to be on the heap? I think the last edit I made should help, though to be honest, storing the Vector3D objects directly in an array, std::array, or std::vector I would think would be easy. Can you clarify (1) is the size of your array known in advance? (2) Must the Vector3D obejcts be on the heap or is it okay to be allocated by value?
@Edward use std::vector
@pm100, this is a learning exercise and we've been told not to use it.
|
1

Just to add based on the asker's comments on RayToal's excellent answer. If you don't know the size of the binArray prior to runtime then you must use std::vector. If you want to allocate each item alone, I would recommend using std::vector<Vector3D*>.

This way you can resize the std::vector at runtime and when you do, it will hold a bunch of nullptrs that are not allocated. Then you can allocate each one of them separately.

std::vector<Vector3D*> binArray;
binArray.resize(x);  // now you have binArray of size x and no allocated elements
binArray[0] = new Vector3D(...);

Please keep in mind that you need to delete them after you're not using them in order to not have a memory leak:

for(size_t i=0;i<binArray.size(); i++)
  if(binArray[i]!=nullptr) delete binArray[i];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.