You only need to delete something if
- You created the object using new (or you used some other library or function which created it with new)
- You own the object (i.e. it is not owned by a smart pointer or another class from another library)
- There are no other pointers anywhere in your program which are referring to the object
In the example you've given; assuming you're talking about deleting your iterator 'p' - it's an object with automatic storage duration - you haven't created it with new, therefore it will be cleared up as soon as it drops out of scope.
In fact, calling delete on it would be invalid (undefined behaviour) and could cause your program to malfunction or crash.
If you think you're getting memory leaks, have a look at anywhere that you're calling new with 'raw' pointers, then investigate whether there's anything you can do to reduce your usage of them (e.g. replacing raw pointers with std::shared_ptr)
e.g. - the following needs a delete because there's a raw pointer:
MyClass* bob = new MyClass;
but this uses a smart pointer which does the cleanup for you:
std::shared_ptr<MyClass> bob( new MyClass );
auto fred = std::make_shared( new MyClass );
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...