Sign in to follow this  

Runtime Error with vector

Recommended Posts

This is my code:
//in Circuit class definition
vector<Node> nodes;

Circuit::Node* Circuit::CreateNode()
	Node temp( this );
	nodes.push_back( temp );
	return &nodes.back();

// in main()
Circuit circuit;
Circuit::Node *n1 = circuit.CreateNode(),
	*n2 = circuit.CreateNode(),
	*n3 = circuit.CreateNode();

The weird thing is that after checking the vector's elements' addresses, n2 is not equal to any of the addresses. Plus this problem was not existent when using a list instead of a vector. Does pushing elements on a vector change the addresses of the previous elements! Is this related to the resizing of the vector.. And if this was the case, how come only one element's address was changing..

Share this post

Link to post
Share on other sites
Any operation which changes the capacity of the vector will invalidate all references and iterators to elements in the vector. list is a node-based container and so does not have the same problem. As to why only one pointer was changing, it depends on the situation, but here is one possibility:
  1. vector size is zero, vector capacity is zero.

  2. push_back a node.

  3. Reallocate vector memory. Allocate memory at address x.

  4. vector size is one, vector capacity is one.

  5. vector contains one element at address x.

  6. Remember address of first element - x.

  7. push_back a node.

  8. Reallocate vector memory. Allocate memory at address y. Free memory at address x.

  9. vector size is two, vector capacity is two.

  10. vector contains two elements, one at address y and one at address y + 1.

  11. Remember address of second element - y + 1.

  12. push_back a node.

  13. Reallocate vector memory. Allocate memory at address x. Free memory at address y.

  14. vector size is three, vector capacity is four.

  15. vector contains three elements, one at address x, one at address x + 1 and one at address x + 2.

  16. Remember address of third element - x + 2.

Share this post

Link to post
Share on other sites
thnx a lot Σnigma, that's very thorough...

My project requires linking between a branch and a pair of nodes. I achieved that by creating a handle to a node (which is the pointer) and assigning that to variables in the branch. That worked initially since nodes were contained in a list. However I changed into a vector since I needed the indices of these nodes (can't actually see why did this help ie changing into vector).

Any way am stuck.. I have to redesign the whole paradigm of linking..
I guess iterators instead of pointers won't work as well, would they?

Share this post

Link to post
Share on other sites
No - iterators are invalidated at the same time as pointers (iterators into vectors are basically pointers wrapped in a small class). The only thing not invalidated is the indices into the vector. This looks like one of those times when what you really need is a garbage collector.

Share this post

Link to post
Share on other sites
Ok, what about iterators into lists, are they invalidated too (assuming everything is converted from vectors into lists)

Share this post

Link to post
Share on other sites
Reference, pointer and iterator invalidation rules for inserts into standard sequence containers:
  • vector: If size() == capacity() then any single element insert will result in all references, pointers and iterators being invalidated. For multiple-element inserts if size() + numberOfElementsToInsert > capacity() then all references, pointers and iterators will be invalidated. If the insert is not at the end then all references, pointers and iterators to elements after the insertion point are invalidated. Otherwise no references, pointers or iterators are invalidated.

  • deque: Inserts at either end invalidate iterators but will not invalidate references or pointers. Inserts in the middle will invalidate all references, pointers and iterators.

  • list: Insertions never invalidate references, pointers or iterators.


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