By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,723 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,723 IT Pros & Developers. It's quick & easy.

vector pointers

P: n/a
I am a little confused with the basic concept of vector of pointers.
The vector is easy enough.

Say you want a vector of pointers to int. The integers are not created
outside the vector so all we get is this and it doesn't work

vector<int*> ones;
ones->push_back(7);
//this failed to *ones.push_back(7)
ones->push_back(8);
ones->push_back(3);
cout << ones[0] << endl; //just print 1 out

this fails from 2nd line

q)do vectors automaticall dynamically create the variables?

May 11 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

jagguy wrote:
I am a little confused with the basic concept of vector of pointers.
The vector is easy enough.

Say you want a vector of pointers to int. The integers are not created
outside the vector so all we get is this and it doesn't work

vector<int*> ones;
ones->push_back(7); This should be:
ones.push_back(7);
'ones' isn't a pointer so it shouldn't be used as one, same for
below...

Also you declared a "vector of pointers to int" not a "vector of
int"... Unless you know what you are doing, change that declaration to:
vector<int> ones;
//this failed to *ones.push_back(7)
ones->push_back(8);
ones->push_back(3);
cout << ones[0] << endl; //just print 1 out

this fails from 2nd line

q)do vectors automaticall dynamically create the variables?

Vectors allocate thier items (elements) dynamicly, however the vector
itself is created either way... So while the following creates it
dynamically:
vector<int> *pOnes = new vector<int>;

The following doesn't:
vector<int> ones;

Abdo Haji-Ali
Programmer
In|Framez

May 11 '06 #2

P: n/a

jagguy wrote:
I am a little confused with the basic concept of vector of pointers.
The vector is easy enough.

Say you want a vector of pointers to int. The integers are not created
outside the vector so all we get is this and it doesn't work

vector<int*> ones;
ones->push_back(7);
//this failed to *ones.push_back(7)
ones->push_back(8);
ones->push_back(3);
cout << ones[0] << endl; //just print 1 out

this fails from 2nd line
Of course it fails, you have a vector of pointers to int, so you must
put pointers to int onto it. And be certain to handle the memory
correctly of the pointers you put on it.
q)do vectors automaticall dynamically create the variables?

They dynamically create the memory to store the variables, but if the
variables are pointers, they do allocate any memory for them to point
to, nor do they call delete on them.

May 11 '06 #3

P: n/a
jagguy wrote:
I am a little confused with the basic concept of vector of pointers.
The vector is easy enough.
Say you want a vector of pointers to int. The integers are not created
outside the vector so all we get is this and it doesn't work


A vector of pointers is just that, a vector of pointers. You seem to ask for
a vector of "things pointed to by pointers". It's your work to create,
delete or anything that needs to be done with the pointers you put in that
vector.

--
Salu2

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 11 '06 #4

P: n/a
jagguy <jo**********@optusnet.com.au> wrote:
I am a little confused with the basic concept of vector of pointers.
The vector is easy enough.

Say you want a vector of pointers to int. The integers are not created
outside the vector so all we get is this and it doesn't work

vector<int*> ones;
ones->push_back(7);
Instead, you need to do:

ones.push_back(new int(7));

but now that you are working with pointers, you must also handle
deleting the things in the vector:

for (vector<int*>::size_type i = 0; i != ones.size(); ++i) {
delete ones[i];
}
cout << ones[0] << endl; //just print 1 out
If you want to print them you need:

cout << *ones[0] << endl;
this fails from 2nd line

q)do vectors automaticall dynamically create the variables?


In a sense, yes. However, in a vector<int*>, it will dynamically
create/destroy the pointers for you, but not what the pointers are
pointing to. This is why you must manually manage the memory for the
contents of the vector, but not the vector itself.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 11 '06 #5

P: n/a
On Thu, 11 May 2006 15:26:39 +0000 (UTC), ri******@gehennom.invalid
(Marcus Kwok) wrote:
Instead, you need to do:
ones.push_back(new int(7));

but now that you are working with pointers, you must also handle
deleting the things in the vector:

for (vector<int*>::size_type i = 0; i != ones.size(); ++i) {
delete ones[i];
}


In this case yes, in general no. The pointed-to objects may reside on
the heap, the stack or in the global space.
q)do vectors automaticall dynamically create the variables?


In a sense, yes. However, in a vector<int*>, it will dynamically
create/destroy the pointers for you, but not what the pointers are
pointing to. This is why you must manually manage the memory for the
contents of the vector, but not the vector itself.


In general Standard C++ containers are designed with 'values
semantics' as design principle. They are apropriate for values only
not for pointers.

Best regards,
Roland Pibinger
May 11 '06 #6

P: n/a
Roland Pibinger <rp*****@yahoo.com> wrote:
On Thu, 11 May 2006 15:26:39 +0000 (UTC), ri******@gehennom.invalid
(Marcus Kwok) wrote:
Instead, you need to do:
ones.push_back(new int(7));

but now that you are working with pointers, you must also handle
deleting the things in the vector:

for (vector<int*>::size_type i = 0; i != ones.size(); ++i) {
delete ones[i];
}


In this case yes, in general no. The pointed-to objects may reside on
the heap, the stack or in the global space.


Very true. However, my post was given with the assumption that the OP
wanted a vector of pointers to dynamically-allocated variables, since he
asked this following question:
q)do vectors automaticall dynamically create the variables?


In a sense, yes. However, in a vector<int*>, it will dynamically
create/destroy the pointers for you, but not what the pointers are
pointing to. This is why you must manually manage the memory for the
contents of the vector, but not the vector itself.


In general Standard C++ containers are designed with 'values
semantics' as design principle. They are apropriate for values only
not for pointers.


Yeah, I forgot to mention that in order to be exception-safe the
pointers should be wrapped in an appropriate smart pointer (not
std::auto_ptr<T> since it doesn't have the appropriate semantics) or a
container designed to hold pointers.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 11 '06 #7

P: n/a
Marcus Kwok wrote:
Yeah, I forgot to mention that in order to be exception-safe the
pointers should be wrapped in an appropriate smart pointer (not
std::auto_ptr<T> since it doesn't have the appropriate semantics) or a
container designed to hold pointers.


Assuming the pointers are owning pointers. But if you have
{
std::list<Widget> myWidgets;
std::vector<Widget*> mySelectedWidgets; // All point to myWidgets.
}
the raw pointers are already exception-safe. The Widgets will be
destroyed
when the list is destructed. In the example, that's after the vector is
destructed.

HTH,
Michiel Salters

May 12 '06 #8

P: n/a
On Thu, 11 May 2006 20:18:03 +0000 (UTC), ri******@gehennom.invalid
(Marcus Kwok) wrote:
Roland Pibinger <rp*****@yahoo.com> wrote:
In general Standard C++ containers are designed with 'values
semantics' as design principle. They are apropriate for values only
not for pointers.


Yeah, I forgot to mention that in order to be exception-safe the
pointers should be wrapped in an appropriate smart pointer (not
std::auto_ptr<T> since it doesn't have the appropriate semantics) or a
container designed to hold pointers.


Exception safety is not related to 'value semantics'. "Smart" pointers
just imitate real pointers. They are as inappropriate as real pointers
for std::containers. The only 'pointers' allowed in the STL world are
the iterators. Everything else is, well, a value and copied by value
but never referenced by a pointer (even C++ references contradict
value semantics).

Best wishes,
Roland Pibinger
May 12 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.