Skip to main content
GameDev.net gamedev.net
🔒 Locked

Vector elements corrupted after push_back.

Started by Mussi Jun 23, 2010 at 10:47 AM 4 replies 1.5k views
Original Post
Mussi
Mussi
Hi there,

My vector elements get corrupted after I call push_back() to another vector. The following code shows the problem:

{	vector<Waypoint*> vecWaypoints;	for(UINT j = 0; j < vecPaths.at(i).vecWaypoints.size(); j++)	{		Waypoint waypoint;		waypoint.vPos = vecPaths.at(i).vecWaypoints.at(j).vPos;		waypoint.vecG = vecPaths.at(i).vecWaypoints.at(j).vecG;		pPath->vecWaypoints.push_back(waypoint); //after this call vecWaypoints' elements get corrupted		Waypoint *pWaypoint = &pPath->vecWaypoints.back();		vecWaypoints.push_back( pWaypoint ); //first element is fine until the call to pPath->vecWaypoints.push_back(waypoint)	}	pPath->vecWaypointGroups.push_back(vecWaypoints);}


Any ideas what might be causing this? and more importantly how to solve this?

[Edit] I use Visual Studio 2008, I've determined the corruption trough the debugger, also the corruption causes problems in other areas of my code.
SiCrane
SiCrane
What does your Waypoint class look like? In particular, does it allocate any memory and does it have a proper copy constructor and assignment operator?
Evil Steve
Evil Steve
This line takes the address of an element in the vector (the last element):
Waypoint *pWaypoint = &pPath->vecWaypoints.back();
But the elements in a vector aren't guaranteed to be at the same address if an element is added to or removed from the vector.

When you add a second element, the vector resizes, which re-allocates its memory, which causes the address of the element to change.

As for how to solve it: Don't take the address of elements in a vector - store the objects by value instead.
Mussi
Mussi
Thanks for the quick replies. Waypoint is a structure that contains pointers to adjacent waypoints and some basic data types like floats, vecG is a vector of floats. I have a vector of waypoints by value, however not all waypoints are connected so I have different groups. The groups are vectors of pointers to the waypoints, I could simply store them by value in the groups and not have a vector for all the waypoints but I'm unsure how/if that fits in the design. However this part has to do with loading the waypoints so the amount of waypoints is fixed, I could reserve the amount needed to avoid this problem. Thanks again.
Zahlman
Zahlman
Why do you need for another container to point at the Waypoints when another one already stores them? Right now, it looks like each "waypoint group" stores pointers to every waypoint of the same, single path. Why not just keep track of paths instead of inventing this apparently redundant "waypoint group" concept?
Mussi
Mussi
Quote:
Original post by Zahlman
Why do you need for another container to point at the Waypoints when another one already stores them? Right now, it looks like each "waypoint group" stores pointers to every waypoint of the same, single path. Why not just keep track of paths instead of inventing this apparently redundant "waypoint group" concept?


Thanks for the comment. I'm not sure if understand you correctly, so let me clarify what I was doing.

I was working on my editor to export some path finding data for maps, so at first I generated a sort of grid of nodes(waypoints). I then worked on generating the reachable adjacent node data for each node. After I had done that I realised I could have a set of nodes that were (indirectly)connected with each other but not connected with other nodes creating an island(group), which could lead to a long search for a path that's not available. So I decided to make a vector of pointers for the groups instead of reorganizing all the nodes into different vectors.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.