0

Is there a halfway elegant way to upgrade to following code snipped by the use of boost's scoped_ptr or scoped_array?

MyClass** dataPtr = NULL;
dataPtr = new MyClass*[num];
memset(dataPtr, 0, sizeof(MyClass*));
allocateData(dataPtr); // allocates objects under all the pointers

// have fun with the data objects

// now I'm bored and want to get rid of them
for(uint i = 0; i < num; ++i)
  delete dataPtr[i];
delete[] dataPtr;
user1709708
  • 1,557
  • 2
  • 14
  • 27

1 Answers1

0

I did it the following way now:

boost::scoped_array<MyClass*> dataPtr(new MyClass*[num]);
memset(dataPtr.get(), 0, num * sizeof(MyClass*));
allocateData(dataPtr.get());

Seems to work fine.

user1709708
  • 1,557
  • 2
  • 14
  • 27
  • You will still need to deallocate all elements in the array manually. – pmr Jul 18 '14 at 10:05
  • Yeah, I just recognized with Visual Leak Detector that the inner pointers won't be deleted. I guess I'd have to nest scoped_ptrs to do so. I guess I'll just stay with a manual delete then. – user1709708 Jul 18 '14 at 16:16
  • Just rewrite `allocateData`. The function is a perfect case of horrible design. – pmr Jul 18 '14 at 18:21