ostringstream SOMETIMES causes a crash
-
02-07-2021 - |
Question
So I wrote an implementation of an ArrayList over the summer, and I have a toString method within which I use an ostringstream object to tack on strings and then output them.
the method is below:
template<class T>
std::string ArrayList<T>::toString() {
std::ostringstream streamOut;
streamOut << "(";
for (int i = 0; i < size; i++) {
streamOut << array[i];
if (i != (size - 1)) {
streamOut << ", ";
}
}
streamOut << ")\n";
std::string returnString = streamOut.str();
return returnString;
}
The problem is that when I run this program it sometimes crashes on the line in the above method:
streamOut << "(";
I tried adding a flush statement at the end but that didn't do the trick...I really have no idea what could be wrong here.
I think this issue may be related but I can't be sure....
https://stackoverflow.com/questions/8250851/big-ostringstream-causes-application-crash
EDIT:
I forgot to mention, that I am using eclipse for development, and I have been unable to get a crash of the program to occur when I run it in eclipse. It's only when I run the exe generated via windows or the command line that it crashes
La solution
I think it crashed because somewhere before this method incorrectly free the memory.