473,395 Members | 1,379 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,395 software developers and data experts.

pointer vectors

when using vectors with pointers of objects. is it best to create instances
then point to them or dynamic allocation upon creating a vector element.
I can do this, but is creating a vector with pointer to objects best done
like this...with dynamic allocation
vector<emp_class*> ptvector;
ptvector.push_back(new emp_class(4,"ee"));
cout<<ptvector->....
....this works but is it effeicient
Jul 23 '05 #1
7 1908
"john townsley" <jo**********@optusnet.com.au> wrote...
when using vectors with pointers of objects. is it best to create
instances then point to them or dynamic allocation upon creating a vector
element.
I can do this, but is creating a vector with pointer to objects best done
like this...with dynamic allocation
vector<emp_class*> ptvector;
ptvector.push_back(new emp_class(4,"ee"));
cout<<ptvector->....
...this works but is it effeicient


Why is the question of efficiency? Dynamic object creation is done not
for efficiency reasons but for the specifics of their lifetime (objects
created in the free store survive the scope they are created in). The
performance concerns should only be addressed if it's proven that they
are founded.

V
Jul 23 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Kv********************@comcast.com...
"john townsley" <jo**********@optusnet.com.au> wrote...
when using vectors with pointers of objects. is it best to create
instances then point to them or dynamic allocation upon creating a vector
element.
I can do this, but is creating a vector with pointer to objects best done
like this...with dynamic allocation
vector<emp_class*> ptvector;
ptvector.push_back(new emp_class(4,"ee"));
cout<<ptvector->....
...this works but is it effeicient


Why is the question of efficiency? Dynamic object creation is done not
for efficiency reasons but for the specifics of their lifetime (objects
created in the free store survive the scope they are created in). The
performance concerns should only be addressed if it's proven that they
are founded.

V


i wanted to know is this how you created vector of pointer to objects? is
there anything to be awate of or is there a better way?
Jul 23 '05 #3
"john townsley" <jo**********@optusnet.com.au> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Kv********************@comcast.com...
"john townsley" <jo**********@optusnet.com.au> wrote...
when using vectors with pointers of objects. is it best to create
instances then point to them or dynamic allocation upon creating a
vector element.
I can do this, but is creating a vector with pointer to objects best
done like this...with dynamic allocation
vector<emp_class*> ptvector;
ptvector.push_back(new emp_class(4,"ee"));
cout<<ptvector->....
...this works but is it effeicient


Why is the question of efficiency? Dynamic object creation is done not
for efficiency reasons but for the specifics of their lifetime (objects
created in the free store survive the scope they are created in). The
performance concerns should only be addressed if it's proven that they
are founded.

V


i wanted to know is this how you created vector of pointer to objects? is
there anything to be awate of or is there a better way?


The main thing to be aware of is the necessity to handle object[s] properly
when deleting the vector element[s]. The vector does not know that you got
your object from the free store and therefore will not attempt to delete
it for you. You have do to it yourself. An alternative is to create the
vector not of pointers but of objects of type 'smart pointer' or something
like that. Do not use 'std::auto_ptr' though, it is not suitable. Look for
a better implementation, like in the Boost library.

AIUI, there are two instances where you want to have a vector of some
pointer
type. One is when you want to store a pointer to a base class and maintain
polymorphic access to objects of [different] derived types, and the other
one
is when the lifetime of the objects is not necessarily controlled by the
object's visibility. Of course you can have both instances together, they
are not mutually exclusive, but rather orthogonal.

V
Jul 23 '05 #4
On Tue, 1 Mar 2005 00:03:12 -0500, Victor Bazarov wrote:
AIUI, there are two instances where you want to have a vector of some
pointer
type. One is when you want to store a pointer to a base class and maintain
polymorphic access to objects of [different] derived types, and the other
one
is when the lifetime of the objects is not necessarily controlled by the
object's visibility. Of course you can have both instances together, they
are not mutually exclusive, but rather orthogonal.

V


What if I have a vector of really BigObject? I mean, when vector gets
resized it must: allocate more memory, call copy constructors for all
elements and finally call destructors after copy is done. For BigObject
this results in both memory and speed performance loss. Wouldn't this be
the third type of situation when it is better to hold pointers in vector
than objects itself? Here, by pointers I mean some kind of smart objects
that are smaller than original objects and do creation/destruction of
BigObject automatically. Copying these pointer objects wouldn't touch
BigObjects on the heap, just pointers to them, which is probably faster,
and definitely uses less "temporary" memory.
Jul 23 '05 #5
SpOiLeR wrote:
On Tue, 1 Mar 2005 00:03:12 -0500, Victor Bazarov wrote:

AIUI, there are two instances where you want to have a vector of some
pointer
type. One is when you want to store a pointer to a base class and maintain
polymorphic access to objects of [different] derived types, and the other
one
is when the lifetime of the objects is not necessarily controlled by the
object's visibility. Of course you can have both instances together, they
are not mutually exclusive, but rather orthogonal.

V

What if I have a vector of really BigObject? I mean, when vector gets
resized it must: allocate more memory, call copy constructors for all
elements and finally call destructors after copy is done. For BigObject
this results in both memory and speed performance loss. Wouldn't this be
the third type of situation when it is better to hold pointers in vector
than objects itself? Here, by pointers I mean some kind of smart objects
that are smaller than original objects and do creation/destruction of
BigObject automatically. Copying these pointer objects wouldn't touch
BigObjects on the heap, just pointers to them, which is probably faster,
and definitely uses less "temporary" memory.


If you anticipate frequent resizing of your container, perhaps std::vector
is not the best choice? See other threads and publications to learn more
on how to pick the proper container for your needs.

You should, of course, think about possible performance implications of
any decision you're making in the process of creating your software. Just
don't be making wrong ones based on incomplete analysis of the problem
domain requirements.

V
Jul 23 '05 #6
On Fri, 04 Mar 2005 09:09:36 -0500, Victor Bazarov wrote:
SpOiLeR wrote:
On Tue, 1 Mar 2005 00:03:12 -0500, Victor Bazarov wrote:

AIUI, there are two instances where you want to have a vector of some
pointer
type. One is when you want to store a pointer to a base class and maintain
polymorphic access to objects of [different] derived types, and the other
one
is when the lifetime of the objects is not necessarily controlled by the
object's visibility. Of course you can have both instances together, they
are not mutually exclusive, but rather orthogonal.

V

What if I have a vector of really BigObject? I mean, when vector gets
resized it must: allocate more memory, call copy constructors for all
elements and finally call destructors after copy is done. For BigObject
this results in both memory and speed performance loss. Wouldn't this be
the third type of situation when it is better to hold pointers in vector
than objects itself? Here, by pointers I mean some kind of smart objects
that are smaller than original objects and do creation/destruction of
BigObject automatically. Copying these pointer objects wouldn't touch
BigObjects on the heap, just pointers to them, which is probably faster,
and definitely uses less "temporary" memory.


If you anticipate frequent resizing of your container, perhaps std::vector
is not the best choice? See other threads and publications to learn more
on how to pick the proper container for your needs.


Hm, but let's say I need random access to container elements. This is
allowed only by vector container (I simulated it with std::list using
iterator that always started from begin() and moved through list until
specified member reached, but of course it was slower than vector). If I
expect from 100 to few 1000s BigObjects to be stored in container, and if I
need random access, but also expect objects will be freely inserted/removed
than what's better: list or vector. Using vector, reallocation takes quite
a time (and reserving vector memory for, let's say 10000, BigObjects each
time seems quite a waste), but using list access takes longer time. The
only thing that seems to solve this problem is vector of pointers. The
question is, how to do it safely? I've been googling around and found boost
smart_ptr library which could do the trick.
Anyway, all I wanted to say, is that maybe this is another situation where
pointers in vector (and vector specifically, not STL containers generally)
may be needed when performance is considered.
Jul 23 '05 #7
SpOiLeR wrote:
On Fri, 04 Mar 2005 09:09:36 -0500, Victor Bazarov wrote:

SpOiLeR wrote:
On Tue, 1 Mar 2005 00:03:12 -0500, Victor Bazarov wrote:

AIUI, there are two instances where you want to have a vector of some
pointer
type. One is when you want to store a pointer to a base class and maintain
polymorphic access to objects of [different] derived types, and the other
one
is when the lifetime of the objects is not necessarily controlled by the
object's visibility. Of course you can have both instances together, they
are not mutually exclusive, but rather orthogonal.

V
What if I have a vector of really BigObject? I mean, when vector gets
resized it must: allocate more memory, call copy constructors for all
elements and finally call destructors after copy is done. For BigObject
this results in both memory and speed performance loss. Wouldn't this be
the third type of situation when it is better to hold pointers in vector
than objects itself? Here, by pointers I mean some kind of smart objects
that are smaller than original objects and do creation/destruction of
BigObject automatically. Copying these pointer objects wouldn't touch
BigObjects on the heap, just pointers to them, which is probably faster,
and definitely uses less "temporary" memory.
If you anticipate frequent resizing of your container, perhaps std::vector
is not the best choice? See other threads and publications to learn more
on how to pick the proper container for your needs.

Hm, but let's say I need random access to container elements. This is
allowed only by vector container (I simulated it with std::list using
iterator that always started from begin() and moved through list until
specified member reached, but of course it was slower than vector). If I
expect from 100 to few 1000s BigObjects to be stored in container, and if I
need random access, but also expect objects will be freely inserted/removed
than what's better: list or vector.


deque.
Using vector, reallocation takes quite
a time (and reserving vector memory for, let's say 10000, BigObjects each
time seems quite a waste), but using list access takes longer time. The
only thing that seems to solve this problem is vector of pointers. The
question is, how to do it safely? I've been googling around and found boost
smart_ptr library which could do the trick.
Probably.
Anyway, all I wanted to say, is that maybe this is another situation where
pointers in vector (and vector specifically, not STL containers generally)
may be needed when performance is considered.


Maybe. What I wanted to say, don't just consider performance when
choosing your design.

V
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sean Dettrick | last post by:
Hi, Can anyone please tell me if the following is possible, and if so, what is the correct syntax? I'd like to resize a vector (and access other vector member functions) via a pointer to that...
6
by: Ash Lux | last post by:
Does the C++ vector template class use pointer-based link lists? Could I get a link explaining how it works, regardless of what it uses? Thank You, Ash Lux
4
by: Venn Syii | last post by:
I've searched all the forums but cannot find an answer to this question. I do the following: vector<MyClass*> myClassList; Later in the program I try to add to myClassList with a...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: techie | last post by:
I'm creating a class BookType that will store information about books. Each book type can have up to 4 authors. I defined a new type for an array of char: typedef char array4_t; This will...
3
by: Si | last post by:
How do I access the contents of an std:vector pointer (I mean access contents of the vector)? What is the correct notation for a pointer ( doesn't seem to work)?
8
by: Bo Peng | last post by:
Dear list, I am using std::vector<bool> (bit_vector) to store my bit sequence. To access the same sequence from C (to expose to a python module), I need to know the pointer and offset of...
18
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct...
156
by: Lame Duck | last post by:
Hi Group! I have a vector<floatvariable that I need to pass to a function, but the function takes a float * arguement. That's OK, I can convert by doing &MyVector.front(), but when I get back a...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.