Why this runtime error?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Why this runtime error?

Post by ImLazy » Wed Jan 18, 2006 2:11 pm

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);".
I stay home. Don't call me out.

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Wed Jan 18, 2006 3:47 pm

You should never dereference end(). end() is an iterator that points *after* the last element.

User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy » Wed Jan 18, 2006 4:06 pm

Oh, I see. Thank you.
I'm sorry I'm not very familiar with STL.
I stay home. Don't call me out.

Post Reply

Return to “C++”