casting a void ** to object **

Started by
16 comments, last by Zahlman 16 years, 1 month ago
Quote:Original post by saeedm
Q1:Wouldn't an array of pointers to objects always perform better, since the actual object would remain in the same place in memory and the only data moved around when operations are performed on the array would be the actual 4byte pointers?
Sure, if you were actually moving it around a lot. In your case, you won't be. Again, this is a situation where you need to understand amortized complexity analysis in order to have an informed opinion.
Quote:Q2:I need to dynamically allocate my Vertex's and Edges, is the following the only way of doing it, while storing them in a Vector? It seems like data is being copied around unnecessarily...

Your code has a memory leak and allocates memory for no good reason. Generally you would do:
myVector.push_back(Edge(2,3,10));

Advertisement
SiCrane and Sneftel: first thing that comes to mind, me being a noobie coder, is where's the 'new' keyword? But I'm assuming Vector will take care of dynamically allocating the Edge or Vertex, correct?
Quote:Original post by saeedm
SiCrane and Sneftel: first thing that comes to mind, me being a noobie coder, is where's the 'new' keyword?

Nowhere. There's no allocation happening outside vector. There's probably no allocation happening inside vector (since the memory's probably already been allocated).
Quote:But I'm assuming Vector will take care of dynamically allocating the Edge or Vertex, correct?
The vector will take care of deciding where to put the object, including allocating more space if necessary. Even if you did "new" yourself, it would still do the same thing.
jyk: I know the max number of Edges and Vertex's from the start. I can't simply use an array because I will gradually add elements, so I need to keep an index or the current number of elements inserted.
Quote:But I'm assuming Vector will take care of dynamically allocating the Edge or Vertex, correct?
Yes (as far as you're concerned, at least).

The details are a little more complicated, but all you really need to know is that:

1. The vector will make room for a new object of the specified type (if necessary)
2. The vector will add a new object that is an exact copy of the input object (under normal circumstances, at least)

So (you might ask), does this mean that vector allocates memory every single time I add a new object? The answer is 'no' (this is where Sneftel's 'amortized complexity analysis' comes into play).
Quote:Original post by saeedm
jyk: I know the max number of Edges and Vertex's from the start. I can't simply use an array because I will gradually add elements, so I need to keep an index or the current number of elements inserted.
As mentioned previously, you can use reserve(), e.g.:
edges.reserve(anticipated_max_edges);verts.reserve(anticipated_max_verts);
And then just use push_back() from there.

Or you could just not worry about it, add the new elements as needed, and see if you run into any performance problems. My guess is you won't, but if you do, then you can try (e.g.) reserve().
got it! thanks to everyone for helping me out

[Edited by - saeedm on March 2, 2008 5:18:45 PM]
Why would you want the same variable to refer to either a set of Vertices or a set of Edges? Could we see more context - what are you doing with the data?

This topic is closed to new replies.

Advertisement