Access Violation when pushing to a list

Started by
2 comments, last by JohnBolton 19 years, 7 months ago

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
Advertisement
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?
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

I see nothing wrong with the code you posted. The problem is probably somewhere else. Perhaps SetStation or the constructor is overflowing an array or something
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement