Vector of double pointers

Hi, I would like to use push_back in order to fill up a vector, containing double pointers. However, when I delete the pointers after pushing them back into the vector, the vector gets emptied as well:

double * Vertex;
vector < double * > Vertices;
Vertex = new double[3];
Vertex[0] = 1.0;
Vertex[1] = 2.0;
Vertex[2] = 3.0;
Vertices.push_back(Vertex);
delete Vertex;

After deleting Vertex, the vector does not return the values anymore. What is the correct way of doing this?

Thank you!
Last edited on
If you delete the array the pointer that you just inserted will get invalid. It does no longer points to a valid array object.

Instead of storing pointers I think you should store actual objects. If you want something similar to a regular array you can use std::array, but you could also create your own struct or class to hold the values of each vertex.

1
2
3
vector<array<double, 3>> Vertices;
array<double, 3> Vertex = {1.0, 2.0, 3.0};
Vertices.push_back(Vertex);
Topic archived. No new replies allowed.