Small String Cleanup?

Hey All,

Beginner question. I am writing an app that will need a lot of short strings. It would be inconvenient to save a pointer to each to allow for them to be deallocated. Will they be deallocated automatically when the program ends or will this cause a memory leak?

Thanks,

Lars
If the pointer is declared as part of a new statement then it has to be deleted.

When the program ends completely all memory of it including un-deleted pointers is trashed. Just imagine what would happen if that didn't happen - new computer on each run - a little expensive.

The discipline of delete followed by a new every time is still best adhered to, and cleaning up before the program ends is good programming discipline.

(The pointer police will tell you to use smart pointers anyway - they are designed to overcome associated problems. Best to be across both raw and smart pointers.)
Hmmm, I suppose I could allocate a larger block and then sub-allocate, so as to need only one pointer...
Modern general-purpose operating systems are fairly robust when it comes to userspace code. They will clean most application resources when the program terminates, memory included.

Not deleting pointers (dropping resources on the floor) is potentially a valid strategy, even if the C++ community might frown upon it as a matter of principle. In particular, allowing the system to clean resources in bulk can occasionally produce a measurably faster program.

This is not a viable strategy for all programs. A program is not leak-free merely because all resources are freed eventually, but because they are freed in a timely manner: problems occur when resources are held idle.

Please give us an overview of your particular situation. There is most likely a better alternative.

Beginner question
Are you familar with std::string?
Last edited on
> Beginner question. I am writing an app that will need a lot of short strings
Beginner answer - just use std::string and forget about the length of the strings.

You will spend more time fiddling around with (and screwing up) all this string micro-management, than you spend on the whole of the rest of the program.

Get something working first.

Then evaluate if you need to do something 'special' for all your strings.
Topic archived. No new replies allowed.