Daniel
Daniel

Reputation: 2726

Is using pointer as c++ class member a more efficient way if the array is large?

Here are a few questions concerning pointers.

class object
{
private:
    vector<point> points_;
}

Say I am writing a class as above, if I have lots of points, a long array, what would you suggest? Should I use vector<point> points_ or vector<point>* points_?

Is using pointers more efficient for a c++ code, so that I should use it as possible as I can? Note: I need the final program to be as fast as possible.

Does an experienced programmer always love to use pointers? If efficiency is not a issue, or is not a concern for me, I found NOT using pointer is clearer to me, and I found using pointers always has a lot of lifetime issues. Thanks

Upvotes: 2

Views: 219

Answers (4)

SinisterRainbow
SinisterRainbow

Reputation: 145

To help solve future issues when speed is important, think about memory management in terms of cache misses and not just look-up times. The above answers I agree, no need for a pointer, unless your class is larger than the size of a pagefile. In which case, you may want to think about breaking down your class 50 or 100 times and still not use pointers.

When considering speed, in game engines for instance, you put as much memory together as possible, and make sure it's loaded before it's needed, and unloaded when it's not needed. You want to keep memory from getting fragged and taking more hits on cache penalties. When ure trying to maintain a certain framerate, say 30 fps, you really do not want to have this vary much (which will happen with a lot of cache misses or memory movement). With std::vector, you can do this as someone suggested, allocate ahead of time, try to allocate enough, or a bit more than you need, a resizing is painful later on, during runtime of a game, it's likely a huge no-no.

Anyway, sometimes it's more efficient to have a large array/vector of objects, even if you have to traverse them all to find the one you need if it means not taking a cache miss. If the memory is loaded, it's quite fast to jump from object to object when sequentially stored. Furthermore, on things that have say (int) ids for instance, doing a vectorObject.at(id_location) is constant time, and lightning fast if no cache problems.

On the other hand, sometimes it's better to have a loaded std::map for instance containing pointers to a very large vector that would likely produce a lot of cache misses (like database size). Traversing a map is lg(n) time compared with with traversing a vector (n) time, and then jumping to the record location through a pointer is more efficient, even if it's one cache miss. It beats traversing the entire harddisk, producing tear-jerking caches.

This is the eternal question of course, deciding what's right depending on what you need. Knowing to avoid cache misses can help you make a better decision, because you can be sure this isn't the last time you'll have to think about it.

Upvotes: 1

billz
billz

Reputation: 45410

The question actually is to choose right STL container instead of pointer to a container. quote below words from book Effective STL

vector, list, and deque offer the programmer different complexity trade-offs and should be used accordingly, vector is the type of sequence that should be used by default, list should be used when there are frequent insertions and deletions from the middle of the sequence, deque is the data structure of choice when most insertions and deletions take place at the beginning or at the end of the sequence.

Does an experienced programmer always love to use pointers?

smart pointers come to very handy, in practice we mostly use smart pointers instead of new/delete operate on raw pointers

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490058

There's no reason to use a pointer here. A vector object will have the same (small) size regardless of the number of elements in the vector.

The vector already contains a pointer that refers to the space in which the data is stored. That space is dynamically allocated (from the free store, assuming you don't take steps to change that).

Upvotes: 5

ForEveR
ForEveR

Reputation: 55887

Vector is dynamic array. It's sizeof is constant, regardless of its size. There is no need to use pointer anyway.

Upvotes: 3

Related Questions