Using a Constant Character Pointer in a std::set Container: Memory Consumption?

The STL containers always calk the default destructors for elements they contain when they are cleaned up. For a set holding raw ‘char *‘ pointers, this will do nothing and the memory will be leaked. You are responsible for cleaning ip this sort of memory yourself For this reason, it's generally considered bad practice to store raw pointers in STL containers If you really must use an STL set with raw C strings in it, that's fine, but do be aware that you'll have to reclaim the memory yourself.

You will also need to provide a custom comparator to the set so that e stored strings are compared by value rather than by pointer (the default ordering on ‘char *‘s just compares the pointers, not the strings).

2 If the OP really means "string literals", then no memory cleanup is required. – Oli Charlesworth Feb 19 at 23:59 @Oli Charlesworth- Agreed, but since this wasn't mentioned explicitly I'm not going to assume it. But you're right - if it's just string literals, no cleanup is necessary.

– templatetypedef Feb 20 at 0:12.

In short, nothing special will happen. Deleting a structure that contains a pointer to some memory will only free the memory used by the pointer itself; it will not cause anything to happen to the pointed-to memory. Unless, of course, you explicitly call free/delete on it.

Which is not a good idea in the case of string literals!

Thank you for the quick response! :) Do you have any suggestions for alternate solutions? Is there a way to create a set of character arrays?

– Wheatevo Feb 19 at 23:58.

The STL containers always calk the default destructors for elements they contain when they are cleaned up. For a set holding raw ‘char *‘ pointers, this will do nothing and the memory will be leaked. You are responsible for cleaning ip this sort of memory yourself.

For this reason, it's generally considered bad practice to store raw pointers in STL containers. If you really must use an STL set with raw C strings in it, that's fine, but do be aware that you'll have to reclaim the memory yourself. You will also need to provide a custom comparator to the set so that e stored strings are compared by value rather than by pointer (the default ordering on ‘char *‘s just compares the pointers, not the strings).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions