Skip to main content
corrected spelling
Source Link
Dmitry
  • 6.8k
  • 2
  • 28
  • 19

You should instead do:

 for ( int = i; i < 3; i++ ) { delete arrayPtr[i]; } 

And you shouldn't do delete[] arrayPtr; as you're trying to free/delete a stack allocated arraPtrarrayPtr.

Another thing to consider is using a std::vector of pointers instead of an array. And if you're using a compiler that implements TR1, you could also use a std::vector of std::tr1::shared_ptr instead of raw pointers, and than you wouldn't need to worry about deleting those objects yourself.

Example:

{ std::vector< std::tr1::shared_ptr<Base> > objects; for (int i=0; i < 3; ++i) { objects.push_back(std::tr1::shared_ptr<Base>(new Derived())); } } // here, once "objects" exit scope, all of your Derived objects are nicely deleted 

You should instead do:

 for ( int = i; i < 3; i++ ) { delete arrayPtr[i]; } 

And you shouldn't do delete[] arrayPtr; as you're trying to free/delete a stack allocated arraPtr.

Another thing to consider is using a std::vector of pointers instead of an array. And if you're using a compiler that implements TR1, you could also use a std::vector of std::tr1::shared_ptr instead of raw pointers, and than you wouldn't need to worry about deleting those objects yourself.

You should instead do:

 for ( int = i; i < 3; i++ ) { delete arrayPtr[i]; } 

And you shouldn't do delete[] arrayPtr; as you're trying to free/delete a stack allocated arrayPtr.

Another thing to consider is using a std::vector of pointers instead of an array. And if you're using a compiler that implements TR1, you could also use a std::vector of std::tr1::shared_ptr instead of raw pointers, and than you wouldn't need to worry about deleting those objects yourself.

Example:

{ std::vector< std::tr1::shared_ptr<Base> > objects; for (int i=0; i < 3; ++i) { objects.push_back(std::tr1::shared_ptr<Base>(new Derived())); } } // here, once "objects" exit scope, all of your Derived objects are nicely deleted 
Source Link
Dmitry
  • 6.8k
  • 2
  • 28
  • 19

You should instead do:

 for ( int = i; i < 3; i++ ) { delete arrayPtr[i]; } 

And you shouldn't do delete[] arrayPtr; as you're trying to free/delete a stack allocated arraPtr.

Another thing to consider is using a std::vector of pointers instead of an array. And if you're using a compiler that implements TR1, you could also use a std::vector of std::tr1::shared_ptr instead of raw pointers, and than you wouldn't need to worry about deleting those objects yourself.