473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

vector pointer vs vector object

57
Hello,
Whats the difference between std::vector<Object> obj; and std::vector<Object *> obj;. And which is recommended, preferred?

Thanks,
Kasya
Sep 14 '08 #1
7 7725
weaknessforcats
9,208 Expert Mod 8TB
Whats the difference between std::vector<Object> obj; and std::vector<Object *> obj;. And which is recommended, preferred?
std::vector<Object> obj creates obj on the stack and it will contain Object variables. Where is actual vector is located is up to your implementation of vector but it is almost certainly on the heap.

std::vector<Object *> obj creates obj on the stack and it will contain pointers to Object variables. The disdadvantage here is that you must insure that all the pointers in the vector are valid for the lifeof the vector. Another disadvantage is that vector will move things around as the array expands an contracts and this may result in a delete of one of those pointers which will also delete the Object. You should use a vector of handles to Object (see the Bridge design pattern) rather than naked pointers.

You must also ask yourself if the Objects or the Object* are unique. If not, then to change an Object in a vector<Object> you will have to iterate the entire vector to find it. If ouy had a vector<Object*> you jusy change the Object and not worry about the vector.

I could go on but this should give you the idea.

You might get a copy of Effective STL by Scott Meyers.
Sep 14 '08 #2
newb16
687 512MB
Another disadvantage is that vector will move things around as the array expands an contracts and this may result in a delete of one of those pointers which will also delete the Object.
I find it doubtful. If vector<T> uses some pointer T* t in its internal representation and calls t=new T; and delete t; for T=Object, it allocates and deletes object. If T=Object* t itself is Object**, and these operations create/delete pointer but not Object itself.
Sep 14 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
I find it doubtful. If vector<T> uses some pointer T* t in its internal representation and calls t=new T; and delete t; for T=Object, it allocates and deletes object. If T=Object* t itself is Object**, and these operations create/delete pointer but not Object itself.
No need to wonder. Just look in your vector template and see how it is implemented.
Sep 15 '08 #4
newb16
687 512MB
No need to wonder. Just look in your vector template and see how it is implemented.
It is implemented in a bunch of header files with no comments whatsoever, so it is no good.
Is such behavior ( vector<T*> is free to delete T whenever it finds it feasible) documented in the standard? Or maybe some another stl reference states it?
Sep 16 '08 #5
Banfa
9,065 Expert Mod 8TB
Another disadvantage is that vector will move things around as the array expands an contracts and this may result in a delete of one of those pointers which will also delete the Object.
My understanding of the dangers of vectors is opposite to this, if you have a vector of pointers, vector<TYPE *> as you resize (reduce in size) the vector the pointers are destroyed but the object they point to is not. You have to handle the memory pointed to yourself (hence your suggestion to use a handle template).

A disadvantage of vector<TYPE> that you have not mentioned is that because of the vectors need to hold its memory contiguously as it would be held if it were an actual array if you insert data anywhere but at the end of the vector or if inserting at the end of an vector causes the array to need a larger capacity, or any other operation that causes the vector to need a larger capacity the contents of the array need to be copied around the array. If TYPE is large this can have an effect on performance. If TYPE has pointers to allocated memory in it and no copy constructor/assignment operator this can be disastrous.

And as an aside you quite often see it said that you can use a vector of vectors as a 2 dimensional array. However where as vectors holds its memory in the same layout as a 1 dimensional array a vector of vectors does not hold its memory in the same layout as a 2 dimensional array. This point would only be relevant if you where interfacing with some legacy code that still used arrays rather than vectors and I am not saying you couldn't program you way round it.

My point is that it is good to be aware of how the various containers hold their memory and what you can rely on and what you shouldn't rely on.
Sep 16 '08 #6
pootle
68
My understanding of the dangers of vectors is opposite to this, if you have a vector of pointers, vector<TYPE *> as you resize (reduce in size) the vector the pointers are destroyed but the object they point to is not. You have to handle the memory pointed to yourself (hence your suggestion to use a handle template).
Exactly. You are responsible for all de-allocation when the template is a pointer.

BTW, there is also another reason to use pointers in a container - to utilise polymorphic types.

My point is that it is good to be aware of how the various containers hold their memory and what you can rely on and what you shouldn't rely on.
This is somehow true, but you cannot make some assumption about how memory is held, except in the most general way (such as vector being contiguous) - it is implementation dependant - you can only rely on what is specified by standards. You can also control allocation because every standard library container takes a custom allocator as a template arugment, and this permits you control / tune the allocation / deallocation of the container.
Sep 16 '08 #7
Banfa
9,065 Expert Mod 8TB
This is somehow true, but you cannot make some assumption about how memory is held, except in the most general way (such as vector being contiguous) - it is implementation dependant - you can only rely on what is specified by standards.
Exactly you should only rely on the behaviour that the standard says a conforming implementation exhibits and not on anything else.

I have noticed a tendency in programmers (new and old in some cases) to assume that all platforms perform in the same way the one they are working on performs rather than knowing the standards and programming to that.
Sep 16 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: The Directive | last post by:
I can't understand why in this folling example the message "Base.Draw()" is printed. Shouldn't "Derive.Draw" be printed because of polymorphism? How can I rewrite this example using a vector to...
1
by: cylin | last post by:
Dear all, Here is my code. ------------------------------ #include <iostream> #include <vector> using namespace std; class A { public:
13
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================...
10
by: gogogo_1001 | last post by:
Dear all, I don't understand why "delete" works well on destructing a object, but fails to destruct a vector of it. Any of your comment is highly appreciated! Following is the program...
12
by: Alfonso Morra | last post by:
I have the ff code for testing the concept of storing objects: #include <vector> #include <iostream> using namespace std ; class MyClass { public: MyClass(){
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
10
by: Jess | last post by:
Hello, I have a program that stores dynamically created objects into a vector. #include<iostream> #include<vector> using namespace std;
13
by: smp | last post by:
Does anyone know why making a vector of pointers is so much less efficient than a vector of objects? For a simple example: int num = 20; vector<int*v_int_ptr; v_int_ptr.reserve(num); ...
3
by: JackC | last post by:
Hi, I am trying to find if its at all possible to create a pointer to an object inside a vector, based upon a vector iterator that will remain a valid pointer once the iterator is invalid. ...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.