vector pointers

J

jagguy

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?
 
A

Abdo Haji-Ali

jagguy said:
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 said:
//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
 
E

Earl Purple

jagguy said:
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.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

jagguy said:
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.
 
M

Marcus Kwok

jagguy said:
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;
}
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.
 
R

Roland Pibinger

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;
}


In this case yes, in general no. The pointed-to objects may reside on
the heap, the stack or in the global space.
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
 
M

Marcus Kwok

Roland Pibinger said:
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;
}


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:
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.
 
M

Michiel.Salters

Marcus said:
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
 
R

Roland Pibinger

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top