473,406 Members | 2,273 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,406 software developers and data experts.

Weird crasher with std::remove, std::erase

This small piece of code is troubling me.. is there anything wrong
with it??
After calling this method the contents ot the input vector are
completely screwed..
(CCountedCIG_CountZero() applies only to a few elements in the vector)

void UElementVector::ClearZeroCountElements(vector<CCou ntedCIG*>
&ioItemVec)
{
vector<CCountedCIG*>::iterator new_end = remove_if(ioItemVec.begin(),
ioItemVec.end(), CCountedCIG_CountZero());

for (vector<CCountedCIG*>::iterator it = new_end; it !=
ioItemVec.end(); it++)
{
delete *it;
*it = NULL;
}

ioItemVec.erase(new_end, ioItemVec.end());
}
Any help is much apreciated!

regards,
jan

Mar 24 '07 #1
3 2841
In article <11**********************@l75g2000hse.googlegroups .com>,
gr****@barnholt.net wrote:
This small piece of code is troubling me.. is there anything wrong
with it??
After calling this method the contents ot the input vector are
completely screwed..
(CCountedCIG_CountZero() applies only to a few elements in the vector)

void UElementVector::ClearZeroCountElements(vector<CCou ntedCIG*>
&ioItemVec)
{
vector<CCountedCIG*>::iterator new_end = remove_if(ioItemVec.begin(),
ioItemVec.end(), CCountedCIG_CountZero());

for (vector<CCountedCIG*>::iterator it = new_end; it !=
ioItemVec.end(); it++)
{
delete *it;
*it = NULL;
}

ioItemVec.erase(new_end, ioItemVec.end());
}
<nodThe problem is that remove_if works just by overwriting elements
that no longer belong in the sequence. It doesn't swap them to the end.
What is most likely happening is that you are overwriting the pointers
you want to delete. And then you are deleting copies of the pointers
you want to keep. A subsequent dereference leads to a crash.

You could do a two-pass operation where the first pass did:

1. Check if you want to delete, and if so:
2. delete
3. null pointer

transform could possibly be employed for the first pass.

And then the second pass could call remove (if null pointer).

Or you could write your own algorithm which combines these passes into a
single pass.

-Howard
Mar 24 '07 #2
On Mar 24, 9:41 pm, Howard Hinnant <howard.hinn...@gmail.comwrote:
<nodThe problem is that remove_if works just by overwriting elements
that no longer belong in the sequence. It doesn't swap them to the end.
What is most likely happening is that you are overwriting the pointers
you want to delete. And then you are deleting copies of the pointers
you want to keep. A subsequent dereference leads to a crash.

D'oh! Thanks for the hint -- carefully re-reading the docs it's all
there..

| Remove_if removes from the range [first, last) every element x such
that pred(x) is true.
| That is, remove_if returns an iterator new_last such that the range
[first, new_last)
| contains no elements for which pred is true. [1] The iterators in
the range [new_last, last)
| are all still dereferenceable, but the elements that they point to
are unspecified.
| ..
cheers,
jan

Mar 25 '07 #3
On Mar 25, 11:44 am, gro...@barnholt.net wrote:
On Mar 24, 9:41 pm, Howard Hinnant <howard.hinn...@gmail.comwrote:
<nodThe problem is that remove_if works just by overwriting elements
that no longer belong in the sequence. It doesn't swap them to the end.
What is most likely happening is that you are overwriting the pointers
you want to delete. And then you are deleting copies of the pointers
you want to keep. A subsequent dereference leads to a crash.

D'oh! Thanks for the hint -- carefully re-reading the docs it's all
there..

| Remove_if removes from the range [first, last) every element x such
that pred(x) is true.
| That is, remove_if returns an iterator new_last such that the range
[first, new_last)
| contains no elements for which pred is true. [1] The iterators in
the range [new_last, last)
| are all still dereferenceable, but the elements that they point to
are unspecified.
| ..

cheers,
jan
This behavior surprises me.I have read the source code for remove
family but I do not understand how they can be useful!!!????
remembering that they won`t work with map whose elements are none-
assignable 'pair's.

Mar 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christopher Armstrong | last post by:
Hello! I'm trying to write a part of a program that will remove all files in its directory. I have tried the std::remove feature of the standard library, but I don't know its syntax. Also, what's...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
12
by: Ross Boylan | last post by:
I am trying to understand under what circumstances destructors get called with std::vector. I have an application in which I will put real objects, not just pointers, in the vector. 1. The...
2
by: Clement RAMBACH | last post by:
Hi, here is my problem: I have a std::vector< A* >. This vector contains pointers to the objects A i create (lets say about 4000 items). I do this several times in my application, and at the...
7
by: jose luis fernandez diaz | last post by:
Hi, Is this right any stl container (vector, deque, list, . . .)? typedef vector container; int main() { container<int> c1;
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
5
by: Christopher | last post by:
The situation is that a std::list<std::set<std::string is being iterated through. Upon certain criteria some sets become empty. I need to remove the empty sets from the list. Is it safe to...
13
by: thomas | last post by:
suppose I will delete an element pointed to by "iter". like this: vector<ints; for(vector<int>::iterator iter=s.begin(); iter!=s.end(); iter++){ if(*iter==3) s.erase(iter); ...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.