pointer to a vector

Apr 7, 2010 at 2:48pm
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

Apr 7, 2010 at 4:07pm
No idea. Can you show us an example of what you mean?
Apr 7, 2010 at 4:12pm
so many if u google it. one simple example

std::vector<MyClass*>* pList;
Apr 7, 2010 at 5:11pm
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 Apr 7, 2010 at 5:17pm
Apr 7, 2010 at 6:24pm
saving pointers in vector or in any container instead of full object will give better performance.
item 3, effective stl, scott meyers.
Apr 7, 2010 at 6:58pm
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.
Apr 7, 2010 at 7:20pm
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.
Apr 7, 2010 at 8:17pm
Why use a pointer if you could use an iterator? Iterators are pointerlike anyway.
Apr 7, 2010 at 10:51pm
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?
Apr 8, 2010 at 4:18pm
closed account (S6k9GNh0)
http://lmgtfy.com/?q=std%3A%3Avector+pointer
Apr 8, 2010 at 4:26pm
That's a pretty dumb response computerquip. That's obviously not what I was asking.
Apr 8, 2010 at 4:28pm
closed account (S6k9GNh0)
It was a joke, lighten up. :)
Last edited on Apr 8, 2010 at 4:28pm
Topic archived. No new replies allowed.