Yes, your first example is actually a memory leak. Each call to new must have a consistent delete call (unless new succeeds).
In C ++, the usual way to do what you are trying to do is simply declare bool locally:
bool b = truel b = false;
If for some reason you really need dynamic allocation, there are smart pointers that manage memory, so you donβt have to worry about calling delete . You can look at scoped_ptr , unique_ptr and shared_ptr .
Finally, C ++ has an excellent standard library that handles many possible containers and algorithms, preventing you from reinventing them and eliminating the need to deal with dynamic allocation in a variety of cases.
If you are serious about learning C ++, I would choose one of the books from the list of SO C ++ books and learn from scratch, rather than trying to translate Java idioms into C ++ (this just won't work).
source share