In the first code snippet, you have to make sure that after you're done using the pointer returned by copio
you delete it, otherwise you end up with a memory leak. But as the others said, unless this is an exercise, use std::string
for string manipulation, or, if not, at least declare your own class and do the delete
in the destructor (or use a std::unique_ptr
or std::shared_ptr
, i.e. a smart pointer).
I don't understand your second code snippet, it looks like there are no allocations there so don't know how you can end up with a memory leak.