Which is the right way to allocate memory via new in the C++ constructor. First way in the argument list:
class Boda {
int *memory;
public:
Boda(int length) : memory(new int [length]) {}
~Boda() { delete [] memory; }
};
or in the body of constructor:
class Boda {
int *memory;
public:
Boda(int length) {
memory = new int [length];
}
~Boda() { delete [] memory; }
};
Thanks, Boda Cydo.
I think the simplest way to do this would be to use a boost scoped array and let someone else’s well tested library code handle it all for you.
So:
Moreover, scoped arrays cannot be copied – so you avoid the nasty copy constructor deallocation issue mentioned in another answer.