Sign in to follow this  
Jedite

Access Violation when pushing to a list

Recommended Posts

list <WorkStation*> pStations;
WorkStation *temp = new WorkStation;
temp->SetStation(StationData);
pStations.push_back(temp);

Setstation is a function to fill up all the values in station.. Stationdata is just a char* with the data. For some reason im getting an access violation when trying to push temp into the list. I dont understand why.. any help is welcomed

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why are you using pointers? Why not just use list instead? The list automatically stores a copy of the data that is separate from the one that is passed in.

For example:
list intVec;
intVec.push_back(1);
intVec.push_back(2);
intVec.push_back(3);
intVec.push_back(4);
intVec.push_back(5);

When the elements were pushed on the back of the list, no pointers at all were used, and this list will have values 1, 2, 3, 4, 5 after all of the push_backs are finished.

Is the crash happening specifically at the push_back call? What happens if you add another simple, non-intrusive line after it? Also, does it happen if you switch to a different STL container, say vector?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Why not just use list instead? The list automatically stores a copy of the data that is separate from the one that is passed in.


One reason NOT to do this is that enforcing a copy on a user defined type can be VERY costly.

I vaguely recall reading something that suggested using push_back on an empty list since was bad since the list doesn't have an end (not every implementation of the STL is 'standard')... but I am probably wrong. Can you push the first item to the front of the list? If this works, can you then push something onto the back of the list?

Cheers,

Timkin

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this