0

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

Community
  • 1
  • 1
Ethan
  • 1,206
  • 3
  • 21
  • 39
  • 2
    So, going from the other question, what type of crash? – chris Sep 19 '12 at 01:53
  • honestly I have no idea...good old windows just crashes with no information.... I linked to that page just because there was no response so I felt like it was possible that maybe there was similar underlying feature of ostringstream that wasn't being used.... – Ethan Sep 19 '12 at 01:54
  • 1
    STL implementations vary so...which compiler and version and which platform? – Kevin Grant Sep 19 '12 at 01:56
  • What are `array` and `size`? We have no idea from the code. – Jesse Good Sep 19 '12 at 02:05
  • array is the backing of the data stRucture, it's of type T. Size is the int that is..the size. They're irrelevant however because my test code runs fine with them. It's just sometimes that that line I designated above doesn't work.... – Ethan Sep 19 '12 at 02:23
  • @Ethan Even if the crash occurs at that line, the cause may be somewhere else. You must check whether your array is correctly allocated and size reflects the true (allocated size). If the array is handled incorrectly, it is absolutely conceivable that your stack or elements on the heap get corrupted, damaging the `ostringstream` object as well. – jogojapan Sep 19 '12 at 02:29

1 Answers1

2

I think it crashed because somewhere before this method incorrectly free the memory.

slggamer
  • 217
  • 1
  • 2
  • but I can't image "streamOut << "(";" will cause a crash. I suggest you should check the code called before it. – slggamer Sep 19 '12 at 02:51
  • shit I went through every call to new....and my merge sort is leaking memory...Commenting out the call to mergeSort = no more crash! thanks!!! Good call, I thought I had locked it down... – Ethan Sep 19 '12 at 03:39