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 on other sites
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 on other sites
Quote:
 Original post by Anonymous PosterWhy 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 on other sites
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

Create an account

Register a new account

• Forum Statistics

• Total Topics
628387
• Total Posts
2982402

• 10
• 9
• 16
• 24
• 11