Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can I trust a vector to stay put if I don't add to it?

Reply
Thread Tools

Can I trust a vector to stay put if I don't add to it?

 
 
Steven T. Hatton
Guest
Posts: n/a
 
      05-07-2004
My understanding of the containers in the Standard Library is that they can
move out from under pointers and references if they are resized.
Stroustrup suggests I can reserve the capacity I will need, and thus avoid
having the vector (or other container) realocated with a different address
range. I have the sense this is not explicitly specified in the Standard.
I have not, however, read the entire ISO/IEC 14882:2003. Here's a link to
an oder version draft of the Standard:

http://www.kuzbass.ru:8086/docs/isoc...tml#lib.vector
// lib.vector.capacity capacity:
size_type size() const;
size_type max_size() const;
void resize(size_type sz, T c = T());
size_type capacity() const;
bool empty() const;
void reserve(size_type n);

Is it reasonable for me to assume my vector /will/ stay put if I don't force
a reallocation by adding to it?
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      05-07-2004
Steven T. Hatton wrote:
> ...
> Is it reasonable for me to assume my vector /will/ stay put if I don't force
> a reallocation by adding to it?


Yes. It is specified in the standard.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      05-07-2004
On Thu, 06 May 2004 20:58:29 -0400, "Steven T. Hatton"
<(E-Mail Removed)> wrote:

>My understanding of the containers in the Standard Library is that they can
>move out from under pointers and references if they are resized.
>Stroustrup suggests I can reserve the capacity I will need, and thus avoid
>having the vector (or other container) realocated with a different address
>range. I have the sense this is not explicitly specified in the Standard.
>I have not, however, read the entire ISO/IEC 14882:2003. Here's a link to
>an oder version draft of the Standard:
>
>http://www.kuzbass.ru:8086/docs/isoc...tml#lib.vector
> // lib.vector.capacity capacity:
> size_type size() const;
> size_type max_size() const;
> void resize(size_type sz, T c = T());
> size_type capacity() const;
> bool empty() const;
> void reserve(size_type n);
>
>Is it reasonable for me to assume my vector /will/ stay put if I don't force
>a reallocation by adding to it?


The word you're interested here is "invalidates". You wish to avoid
anything that invalidates references, pointers and iterators into the
vector. If you search the Standard for "invalidates", you'll find
23.2.4.2/5, which answers your direct question, and also a bit later the
section on vector::erase() explains what gets invalidated when you use
that.
-leor


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      05-07-2004
"Steven T. Hatton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> My understanding of the containers in the Standard Library is that they

can
> move out from under pointers and references if they are resized.
> Stroustrup suggests I can reserve the capacity I will need, and thus avoid
> having the vector (or other container) realocated with a different address
> range. I have the sense this is not explicitly specified in the Standard.
> I have not, however, read the entire ISO/IEC 14882:2003. Here's a link to
> an oder version draft of the Standard:
>
> http://www.kuzbass.ru:8086/docs/isoc...tml#lib.vector
> // lib.vector.capacity capacity:
> size_type size() const;
> size_type max_size() const;
> void resize(size_type sz, T c = T());
> size_type capacity() const;
> bool empty() const;
> void reserve(size_type n);
>
> Is it reasonable for me to assume my vector /will/ stay put if I don't

force
> a reallocation by adding to it?



Yes. However you can get the begin or end (one past the end element) of the
vector whenever you want, so you can do for example:


#include <vector>


int main()
{
using std::vector;

vector<int>a(10);

vector<int>::iterator p=a.begin()+3;

*p=7;

a.push_back(4);

// p continues to point at a[3]
p=a.begin()+3;
}






Regards,

Ioannis Vranos

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Full trust and medium trust in .net and websites Linda ASP .Net Security 1 08-31-2006 05:16 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
can not dynamically add element to vector in going over the vector John Black C++ 4 05-28-2004 07:56 PM



Advertisments