Page 1 of 1

Why this runtime error?

Posted: Wed Jan 18, 2006 2:11 pm
by ImLazy

Code: Select all

#include <list>
using namespace std;

int main(){
    list<list<int>*> g;
    g.push_back(new list<int>);
    list<list<int>*>::iterator p = g.end();
    list<int> v = *(*p);
    return 0;
}
It seems the program breaks at "list<int> v = *(*p);".

Posted: Wed Jan 18, 2006 3:47 pm
by Krzysztof Duleba
You should never dereference end(). end() is an iterator that points *after* the last element.

Posted: Wed Jan 18, 2006 4:06 pm
by ImLazy
Oh, I see. Thank you.
I'm sorry I'm not very familiar with STL.