Originally Posted by
iMalc
No you've got it all wrong. I told you this wasn't doing what you thought it was doing.
If this were truly an array of pointers to things allocated by new then "delete arrayOfPointers[i];" as per your first guess is 100% correct.
The issue here is that what you've got is not pointers to things allocated by new. What you've got are pointers to *something* that is internal to a string object. You've essentially tried to rip some of the guts out of a string, leak the string itself, and improperly free pieces of its internals.
If you were a doctor trying to empty a patient's stomach contents, this would be like cutting the stomach out of the patient, burning it, and calling it a job well done (if you'll excuse the pun).
In other words you have no business trying to rip the data() out of the string, and pretending that it is akin to the string itself - it isn't! It is povided to view the contents of a string, and that is all. It may be an internal allocation that is shared with other strings, or part of a string pool maintained by the string class, or a pointer to somewhere inside a special global chunk of memory used specifically for providing a read-only copy when data is called, or who knows what else. It in no way corresponds to something you have and responsiblity, or even the ability, to mess with it in any other way whatsoever besides reading it, and even then only until the next call to a non-constant member function of the string object. It is not something you can hold onto beyond the lifetime of the string object itself and certainly not something you can delete. It's more of a quick peek into something that you probably shouldn't actually be peeking at anyway.
If you delete the "anotherStr" then that string object cleans up after itself, and this can mean that the buffer you were pointing to is sometimes gone. Depending on where it chose to put the buffer, and how the particular runtime library you're using works, the buffer may or may not actually happen to sometimes look like the data is still there. No matter what though, once the string is deleted, that buffer is not allowed to be accessed at all.
Hopefully soon you'll finally see the light that using pointers to string objects in your program really was a ridiculous thing to do to begin with. Forget doing it with pointers, as I said in the beginning, declaring a pointer to a string object as a class member is almost guaranteed to be the wrong thing to do. Just declare a vector of strings and return that. If you're going to learn C++, you'd be best to learn about vectors and strings sooner rather than later.
Some of us have been using the language for about a decade, and in very large projects, and some of us have even taught programming at some point. You'd do better off to pay closer attention