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

vector pointers

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
8 3596

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

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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
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================================...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
5
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.