pointer to a vector

I see many many usage as pointer to a vector in some third party library. I never use containers that way and wondering what is the rational behind it? Save memory?
Thanks

No idea. Can you show us an example of what you mean?
so many if u google it. one simple example

std::vector<MyClass*>* pList;
closed account (S6k9GNh0)
In C++, there's really not much advantage of a reference to a vector and a pointer to a vector. A reference to a vector is used more often than not here. Also, there is no point in allocating memory on the heap for a vector if that's what your using the pointer for.
Last edited on
saving pointers in vector or in any container instead of full object will give better performance.
item 3, effective stl, scott meyers.
It also sucks because:

1) your application now has to manage the memory (objects are probably dynamically allocated)
2) any good programmer will check for NULL before dereferencing any of the pointers contained in the vector,
meaning extra (unneeded) error legs
3) the extra dereference needed to access the real value makes using some boost libraries (such as lambda)
with STL algorithms nastier.
saving pointers in vector or in any container instead of full object will give better performance. item 3, effective stl, scott meyers.


You need to qualify why here, since it's not always the case. At other times storing the full object can be better for performance. There's never a silver bullet in C++ or other languages for that matter, it's always trade offs.
Why use a pointer if you could use an iterator? Iterators are pointerlike anyway.
so many if u google it. one simple example

std::vector<MyClass*>* pList;


Google what? Why should I have to google anything when you asked the question? I was asking for some code from a particular 3rd party library where you said that you saw a vector being used in that way. How would I know what to search for?
closed account (S6k9GNh0)
http://lmgtfy.com/?q=std%3A%3Avector+pointer
That's a pretty dumb response computerquip. That's obviously not what I was asking.
closed account (S6k9GNh0)
It was a joke, lighten up. :)
Last edited on
Topic archived. No new replies allowed.