C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

question about std::vector<T>::erase

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Bronek Kozicki
Guest





PostPosted: Fri Oct 24, 2003 9:16 pm    Post subject: question about std::vector<T>::erase Reply with quote



C++ standard does not mention what happens with memory allocated inside
vector when whole vector is being invalidated by following operation
(clause 23.2.4.3/3) :

v.erase(v.begin(), v.end());

Quote:
From definition of function "reserve" (clause 23.2.4.2/2) it's assumed
that erase will not free memory allocated by vector. But it's not stated

explicitly in standard, thus this assumption is doubtful - especially
when whole vector is being erased. Is it omission in standard or it's
left unspecified on purpose (eg. to allow space optimization, but not
require it) ?


B.


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Dhruv
Guest





PostPosted: Sat Oct 25, 2003 5:57 pm    Post subject: Re: question about std::vector<T>::erase Reply with quote



On Fri, 24 Oct 2003 21:16:44 +0000, Bronek Kozicki wrote:

Quote:
C++ standard does not mention what happens with memory allocated inside
vector when whole vector is being invalidated by following operation
(clause 23.2.4.3/3) :

v.erase(v.begin(), v.end());

This statement will not free the memory occupied by the vector. Neither
will v.clear ();

You can use the swap hack to get that effect.

template <class Type>
void erase_vector (std::vector<Type>& temp)
{
std::vector<Type> useless;
useless.swap (temp);
}


Regards,
-Dhruv.








---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Bronek Kozicki
Guest





PostPosted: Sun Oct 26, 2003 7:28 pm    Post subject: Re: question about std::vector<T>::erase Reply with quote



On Sat, 25 Oct 2003 17:57:55 +0000 (UTC), "Dhruv" wrote:
Quote:
C++ standard does not mention what happens with memory allocated inside
vector when whole vector is being invalidated by following operation
(clause 23.2.4.3/3) :

v.erase(v.begin(), v.end());

This statement will not free the memory occupied by the vector. Neither
will v.clear ();

Well, this is what I have learned from books already (BTW: thanks for
reminding vector<T>::clear). However it does not seem to be mentioned
anywhere in the standard. Did I miss something (in C++ standard), is it
omission, or it's left unspecified on purpose? In other words: are
standard library implementors allowed to actually free memory (and lower
capacity) of vector in standard conforming implementation of
vector<T>::erase and vector<T>::clear ? If no, why ?


B.

PS. there is a reason why I ask this question here, not on comp.lang.c++ :)

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
John Potter
Guest





PostPosted: Mon Oct 27, 2003 3:21 am    Post subject: Re: question about std::vector<T>::erase Reply with quote

On Sun, 26 Oct 2003 19:28:48 +0000 (UTC), [email]brok (AT) rubikon (DOT) pl[/email] (Bronek
Kozicki) wrote:

Quote:
In other words: are
standard library implementors allowed to actually free memory (and lower
capacity) of vector in standard conforming implementation of
vector<T>::erase and vector<T>::clear ? If no, why ?

They may iff reserve has not been called. Do you want a
reserve_has_been called member of std::vector?

John

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Oct 29, 2003 11:46 pm    Post subject: Re: question about std::vector<T>::erase Reply with quote

[email]Brok (AT) rubikon (DOT) pl[/email] ("Bronek Kozicki") wrote in message
news:<e6e7e$3f98ef32$3e6fced2$30189 (AT) nf1 (DOT) news-service.com>...

Quote:
C++ standard does not mention what happens with memory allocated
inside vector when whole vector is being invalidated by following
operation (clause 23.2.4.3/3) :

v.erase(v.begin(), v.end());

Implementation defined.

Quote:
From definition of function "reserve" (clause 23.2.4.2/2) it's
assumed that erase will not free memory allocated by vector. But it's
not stated explicitly in standard, thus this assumption is doubtful -
especially when whole vector is being erased. Is it omission in
standard or it's left unspecified on purpose (eg. to allow space
optimization, but not require it) ?

The standard pretty explicitly guarantees that the following code will
work:

std::vector< int > v ;
v.reserve( 100 ) ;
v.erase( v.begin(), v.end() ) ;
v.push_back( 1 ) ;
int& ri = v[ 0 ] ;
v.push_back( 2 ) ;
ri = 100 ;

The standard doesn't ever speak in terms of when memory will be freed,
etc. It gives required semantics. If you can find a way to make the
above work, and also free memory in erase, then it is legal to do so.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Andrey Tarasevich
Guest





PostPosted: Thu Oct 30, 2003 10:47 pm    Post subject: Re: question about std::vector<T>::erase Reply with quote

Bronek Kozicki wrote:
Quote:
C++ standard does not mention what happens with memory allocated inside
vector when whole vector is being invalidated by following operation
(clause 23.2.4.3/3) :

v.erase(v.begin(), v.end());

From definition of function "reserve" (clause 23.2.4.2/2) it's assumed
that erase will not free memory allocated by vector. But it's not stated
explicitly in standard, thus this assumption is doubtful - especially
when whole vector is being erased. Is it omission in standard or it's
left unspecified on purpose (eg. to allow space optimization, but not
require it) ?
...

If you read the specification of 'std::vector<>' carefully, you'll
notice that it appears that an attempt was made to explicitly declare
the situations when reallocation might occur (see, for example, the
description of 'std::vector<>::insert'). Since the specification of
'std::vector<>::erase' doesn't say that reallocation might take place,
it seems to be pretty logical to assume that it doesn't.

Another thing to pay attention to is that the specification guarantees
that no reallocation takes place on an "partial" erase, since all
references and iterators before the point of the erase are guaranteed to
remain valid. You seem to separate the "full" erase into some kind of
special case, while the standard doesn't do that. (Yes, I understand
that "full" erase will invalidate all iterators anyway and, therefore,
deallocation of the memory won't violate anything in this respect).

I'd agree if you said that both of the above arguments are rather
inconclusive. I'd also prefer the standard to be more explicit in this
respect.

--
Best regards,
Andrey Tarasevich

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Sean Kelly
Guest





PostPosted: Tue Nov 04, 2003 5:23 pm    Post subject: Re: question about std::vector<T>::erase Reply with quote

Andrey Tarasevich wrote:

Quote:
Bronek Kozicki wrote:

If you read the specification of 'std::vector<>' carefully, you'll
notice that it appears that an attempt was made to explicitly declare
the situations when reallocation might occur (see, for example, the
description of 'std::vector<>::insert'). Since the specification of
'std::vector<>::erase' doesn't say that reallocation might take place,
it seems to be pretty logical to assume that it doesn't.

All of the container specifications do this to define which operations
could invalidate iterators and if so, which iterators. I don't think
the spec should be read into any more than that.

Quote:
Another thing to pay attention to is that the specification guarantees
that no reallocation takes place on an "partial" erase, since all
references and iterators before the point of the erase are guaranteed to
remain valid. You seem to separate the "full" erase into some kind of
special case, while the standard doesn't do that. (Yes, I understand
that "full" erase will invalidate all iterators anyway and, therefore,
deallocation of the memory won't violate anything in this respect).

And since the point of the erase when calling clear() is the first
element, no gurantees are made about what's done with the any of
element-memory used by the container. So what actually happens is, of
course, implementation-defined.


Sean

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Vishal
Guest





PostPosted: Thu Nov 06, 2003 6:52 pm    Post subject: Re: question about std::vector<T>::erase Reply with quote

Of the four implementations I checked, one implementation deallocated memory
on vec.clear() (even after a call to reserve). But, it did not do so for
vec.erase(vec.begin(), vec.end()), possibly because its capacity was more
than the size.

Vishal

Bronek Kozicki wrote:

Quote:
C++ standard does not mention what happens with memory allocated inside
vector when whole vector is being invalidated by following operation
(clause 23.2.4.3/3) :

v.erase(v.begin(), v.end());

From definition of function "reserve" (clause 23.2.4.2/2) it's assumed
that erase will not free memory allocated by vector. But it's not stated
explicitly in standard, thus this assumption is doubtful - especially
when whole vector is being erased. Is it omission in standard or it's
left unspecified on purpose (eg. to allow space optimization, but not
require it) ?

B.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.