Jump to content
  • Advertisement
Sign in to follow this  
Grosch

Problems with vectors and pointers

This topic is 4604 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two problems concerning vectors and pointers at their elements. I have a vector of Events (std::vector<CColEvent>Events;), and the function to add an element to this vector is addEvent CColEvent* CColList::addEvent() { CColEvent fEvent; fEvent.ID = Events.size(); fEvent.init();//this changes the boolean values to FALSE Events.push_back(fEvent); return &Events[fEvent.ID]; } the first element i add to this vector is BaseEvent colList.BaseEvent = colList.addEvent(); the first problem is, that some boolean values of BaseEvent change from FALSE to TRUE when i add the second Event to the vector. CColEvent* fpNewEvent = addEvent(); The values change at "Events.push_back(fEvent);" the second problem with this code is, that i get the programm shuts down with the error "Unhandled exception in Pong.exe(MSVCRTD.DLL): 0x0000005: Access Violation." This also happens in addEvent at "Events.push_back(fEvent);", but only at the the 5th or 6th time i call it. this is the part of the code where the error occurs: CColEvent* fpETSI; fpETSI = addEvent(); I know that this error means that the programm wants to read something out of a pointer which doesnt point to an object but i dont know why this error doesnt occur every time when i call addEvent, and why it occurs here at all. I hope that i have described my problems in an understandable way and that my englisch isnt too bad. Many thanks in advance, Grosch

Share this post


Link to post
Share on other sites
Advertisement
when you use a vector, the additions you make are copied across. thus the copy constructor is called. what does your copy constructor look like?

Share this post


Link to post
Share on other sites
If push_back (or any vector operation that inserts objects into the vector) forces the vector to reallocate its internal storage then all pointer, references and iterators to existing elements are invalidated. One way around this is to reserve sufficient space beforehand in order to prevent reallocation.

Enigma

Share this post


Link to post
Share on other sites
here is the copy constructor:

CColEvent::CColEvent(const CColEvent &Event)
{
colTime = Event.colTime;
NextEvent = Event.NextEvent;
scndEvent = Event.scndEvent;
objA = Event.objA;
objB = Event.objB;
direction = Event.direction;
ID = Event.ID;
isCol = Event.isCol;
isInput = Event.isInput;
Parent = Event.Parent;
isGabel = Event.isGabel;
}

of course, here are some pointers which do not point to an object yet, i.e. NextEvent or scndEvent, but i dont understand why it sometimes works and sometimes not...

Share this post


Link to post
Share on other sites
Quote:
Original post by Grosch
here is the copy constructor:

CColEvent::CColEvent(const CColEvent &Event)
{
colTime = Event.colTime;
NextEvent = Event.NextEvent;
scndEvent = Event.scndEvent;
objA = Event.objA;
objB = Event.objB;
direction = Event.direction;
ID = Event.ID;
isCol = Event.isCol;
isInput = Event.isInput;
Parent = Event.Parent;
isGabel = Event.isGabel;
}

of course, here are some pointers which do not point to an object yet, i.e. NextEvent or scndEvent, but i dont understand why it sometimes works and sometimes not...


enigma is right, if you keep adding elements to a vector it will move all of its elements to a bigger array. this means that pointers to the original array are invalid.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!