views:

148

answers:

3

I'm dealing with a file with a linked list of lines with each node looking like this:

struct TextLine{
    //The actual text
    string text;
    //The line number of the document
    int line_num;
    //A pointer to the next line
    TextLine * next;
};

and I'm writing a function that adds spaces at the beginning of the lines found in the variable text, by calling functions like linelist_ptr->text.insert(0,1,'\t');

The program compiles, but when I run it I get this error:

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at
Aborted

Any ideas?

A: 

Check that linelist_ptr has a legitimate value, i.e. that you have newed it (and it hasn't been deleted prior to you using it)

hamishmcn
Neither of these will throw std::out_of_range, at all, ever. All are undefined behavior and are likely to simply result in a segmentation fault.
iconiK
What I was thinking was that if the pointer was pointing at a garbage location which was then interpreted as a std::string when the call was made.
hamishmcn
He is dereferencing the pointer. How could that possibly result in at() being called?
iconiK
Pavel Minaev
A: 

You are using the std::string::at() somewhere in your code but you are passing it an incorrect index, hence it throws. Since you don't catch any exceptions it propagates out of main() and terminate() is called, killing the process.

The code you've shown can't fail in that way, as std::string::insert() doesn't call std::string::at(), nor are the parameters. Please add exception handling to your code and if you still can't find the bug please post a larger section of your code (or the whole file, to http://codepad.org preferably).

iconiK
This is wrong. ISO C++03 specifies that `insert()` should throw `std::out_of_range` if index is out of range (21.3.5.4[lib.string::insert]/3: "Throws: out_of_range if pos1 > size() or pos2 > str.size()."). It is perfectly legal for it to implement this requirement by calling `at()`.
Pavel Minaev
@Pavel, alright. However, that is not the cause as the index is 0 - which is in range of any valid std::string.
iconiK
A: 

I think the most likely problem from what you've described is that insert() is being invoked with an invalid position off the end of the string (i.e. > size()). You say this example is like the functions you're calling, so check the ones you may have written where you might be passing position differently than the sample code above and make sure the value is what you expect.

The terminate message is because you're not handling the out_of_range exception (via try/catch blocks), so it escapes up to the C++ language runtime, which unceremoniously shuts your program down.

Owen S.