Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem storing tvmet vector objects in an stl vector container

Reply
Thread Tools

Problem storing tvmet vector objects in an stl vector container

 
 
alexjcollins@gmail.com
Guest
Posts: n/a
 
      09-08-2008
The following program demonstrates the problem:

#include <vector>
#include <iostream>
#include <tvmet/Vector.h>

typedef tvmet::Vector<double, 3> Vector3d;

class Mesh
{
public:
Vector3d* addVertex(const Vector3d& tVertex);
private:
std::vector<Vector3d> m_tVertices;
};

Vector3d* Mesh::addVertex(const Vector3d& tVertex)
{ m_tVertices.push_back(tVertex);
return &m_tVertices.back();
}

int main()
{
Mesh tMesh;
Vector3d* pVertex0 = tMesh.addVertex(Vector3d(-1, 1, 1));
std::cout << "Vertex0: " << (*pVertex0)(0) << ", " << (*pVertex0)
(1) << ", " << (*pVertex0)(2) << std::endl;
Vector3d* pVertex1 = tMesh.addVertex(Vector3d( 1, 1, 1));
std::cout << "Vertex0: " << (*pVertex0)(0) << ", " << (*pVertex0)
(1) << ", " << (*pVertex0)(2) << std::endl;
std::cout << "Vertex1: " << (*pVertex1)(0) << ", " << (*pVertex1)
(1) << ", " << (*pVertex1)(2) << std::endl;
return 0;
}

Which gives the following output:

Vertex0: -1, 1, 1
Vertex0: 0, 1, 1
Vertex1: 1, 1, 1

Instead of the expected output:

Vertex0: -1, 1, 1
Vertex0: -1, 1, 1
Vertex1: 1, 1, 1

It is interesting to note that the code behaves correctly if an stl
list is used instead. I can't work out why this is not working, and it
seems like such a trivial program! Could anyone explain to me what is
wrong?
 
Reply With Quote
 
 
 
 
alexjcollins@gmail.com
Guest
Posts: n/a
 
      09-08-2008
On Sep 8, 9:17*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > The following program demonstrates the problem:

>
> > #include <vector>
> > #include <iostream>
> > #include <tvmet/Vector.h>

>
> > typedef tvmet::Vector<double, 3> Vector3d;

>
> > class Mesh
> > {
> > public:
> > * * Vector3d* addVertex(const Vector3d& tVertex);
> > private:
> > * * std::vector<Vector3d> m_tVertices;
> > };

>
> > Vector3d* Mesh::addVertex(const Vector3d& tVertex)
> > { * m_tVertices.push_back(tVertex);
> > * * return &m_tVertices.back();
> > }

>
> > int main()
> > {
> > * * Mesh tMesh;
> > * * Vector3d* pVertex0 = tMesh.addVertex(Vector3d(-1, 1, 1));
> > * * std::cout << "Vertex0: " << (*pVertex0)(0) << ", " << (*pVertex0)
> > (1) << ", " << (*pVertex0)(2) << std::endl;
> > * * Vector3d* pVertex1 = tMesh.addVertex(Vector3d( 1, 1, 1));
> > * * std::cout << "Vertex0: " << (*pVertex0)(0) << ", " << (*pVertex0)
> > (1) << ", " << (*pVertex0)(2) << std::endl;
> > * * std::cout << "Vertex1: " << (*pVertex1)(0) << ", " << (*pVertex1)
> > (1) << ", " << (*pVertex1)(2) << std::endl;
> > * * return 0;
> > }

>
> > Which gives the following output:

>
> > Vertex0: -1, 1, 1
> > Vertex0: 0, 1, 1
> > Vertex1: 1, 1, 1

>
> > Instead of the expected output:

>
> > Vertex0: -1, 1, 1
> > Vertex0: -1, 1, 1
> > Vertex1: 1, 1, 1

>
> > It is interesting to note that the code behaves correctly if an stl
> > list is used instead. I can't work out why this is not working, and it
> > seems like such a trivial program! Could anyone explain to me what is
> > wrong?

>
> 'push_back' *is allowed* to invalidate all pointers and iterators to any
> of the vector's elements. *So, storing 'pVertex0' is probably a bad idea
> if you intend to grow the vector (matrix).
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Thanks, that makes sense.
Is push_back allowed to invalidate pointers when using a list?
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      09-08-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Is push_back allowed to invalidate pointers when using a list?


AFAIK std::list is guaranteed to preserve pointers to existing
elements valid even if new elements are added.

If you need random access (and more efficient memory usage) you might
want to consider using std::deque instead of std::vector. The former
behaves mostly like std::vector, but pointers are not invalidated.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any Blitz++ and TVMET experts out there? dss C++ 2 03-10-2007 02:02 AM
which STL container do I need for storing a 2D-array? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C++ 20 10-25-2006 11:16 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM



Advertisments