Hello,
I am facing a starnge problem while erasing the last member in a
vector. I am using VC++ .NET 2002 complier. I have vector of
CComPtr<..> (irrelevant here), and then I iterate over the vector. If
it is the iterator, then I remove the element from the vector using
vecObjects.erase(it). It works fine till the last element. While
removing the last element it throws exception and fails. But the same
vecObject.clear() works with out any problem. Can somebody there,
please help me to indentify this problem and solution to this
Thanks in advance.
EK 11 4314
In article <11**********************@z14g2000cwz.googlegroups .com>,
<ee****@gmail.com> wrote: I am facing a starnge problem while erasing the last member in a vector. I am using VC++ .NET 2002 complier. I have vector of CComPtr<..> (irrelevant here), and then I iterate over the vector. If it is the iterator, then I remove the element from the vector using vecObjects.erase(it). It works fine till the last element. While removing the last element it throws exception and fails. But the same vecObject.clear() works with out any problem. Can somebody there, please help me to indentify this problem and solution to this
You are most likely going off the end of the vector and erasing an
invalid iterator. Please post a code snippet that can compile and
you'll get much better responses.
--
Mark Ping em****@soda.CSUA.Berkeley.EDU
Yes, the 'last' position in a vector is one beyond the last element.
If you are progressing through your vectory array, check if the address
of the iterator has reached the address of the last position. See the
code example below.
vector<int>::iterator iter = 0;
for( iter = vecList.begin(); iter != vecList.end(); iter++ )
{
delete *iter;
}
"Jordan" <jo***********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com... Yes, the 'last' position in a vector is one beyond the last element.
If you are progressing through your vectory array, check if the address of the iterator has reached the address of the last position. See the code example below.
vector<int>::iterator iter = 0; for( iter = vecList.begin(); iter != vecList.end(); iter++ ) { delete *iter; }
That's how to delete dynamically allocated objects from a vector, but the
iterator you've declared refers to a vector that does not have pointers, it
has just int's. You don't want to delete an int, or anything that was not
created via new. (I know, it's just an example, but it's a bad example.
:-))
The OP asked about using erase() to remove items from a vector. That's
different from deleting the dynamically allocated data pointed to by
pointers stored in the vector. And without seeing his code, we can't say
_for_sure_ what the problem is. My "guess" is that clear() will work fine,
but it's just a guess at this point. Only if he also wants to delete
dynamically allocated objects will he first want to have a loop like the one
you've shown.
-Howard ee****@gmail.com wrote in message
<11**********************@z14g2000cwz.googlegroups .com>... Hello,
I am facing a starnge problem while erasing the last member in a vector. I am using VC++ .NET 2002 complier. I have vector of CComPtr<..> (irrelevant here), and then I iterate over the vector. If it is the iterator, then I remove the element from the vector using vecObjects.erase(it). It works fine till the last element. While removing the last element it throws exception and fails. But the same vecObject.clear() works with out any problem. Can somebody there, please help me to indentify this problem and solution to this Thanks in advance. EK
You forgot to show the code! Are you trying to do this in a loop?
If you do:
vecObjects.erase(it);
....then iterator 'it' may no longer be valid [1]. Reset it.
... It works fine till the last element.
Use:
vecObjects.pop_back(); // Removes the last element.
[1] " A vector's iterators are invalidated when its memory is reallocated.
Additionally, inserting or deleting an element in the middle of a vector
invalidates all iterators that point to elements following the insertion or
deletion point. ..."
--
Bob R
POVrookie
<ee****@gmail.com> schrieb im Newsbeitrag
news:11**********************@z14g2000cwz.googlegr oups.com... Hello,
I am facing a starnge problem while erasing the last member in a vector. I am using VC++ .NET 2002 complier. I have vector of CComPtr<..> (irrelevant here),
Perhaps it is irrelevant for your problem, but you should not put instances
of CComPtr into C++ containers. Some of these containers (or some
implementations of them) do not work well with objects overloading
operator&.
and then I iterate over the vector. If it is the iterator, then I remove the element from the vector using vecObjects.erase(it).
When you iterate through a container and try to erase its last element, make
sure not to increment the iterator after erasing. Actually you should never
increment an iterator after erasing the element it refers to. For all
containers, all iterators refering to the erased element become invalid. So
don't do something like this
for (SomeContainer::iterator it = myContainer.begin(); it !=
myContainer.end(); ++it)
{
if (RemoveThisElement(*it)) myContainer.erase(it);
}
If SomeContainer is an std::vector, this seems to work until you try to
erase the last element, but actually it will not examine those elements
imediately following an element, that will be removed. Now if you erase the
last element, end() will change (in some implementations end() will become
equal to the iterator for the erased elemet) and due to the final increment
the test for equality to end() will fail and the loop will continue until it
reaches the end of allocated memory. (Or it will behave entirely different,
after all that's the problem with undefined behaviour.)
HTH
Heinz
In article <43**********************@newsread2.arcor-online.net>,
Heinz Ozwirk <ho**********@arcor.de> wrote: for (SomeContainer::iterator it = myContainer.begin(); it != myContainer.end(); ++it) { if (RemoveThisElement(*it)) myContainer.erase(it); }
If SomeContainer is an std::vector, this seems to work until you try to erase the last element, but actually it will not examine those elements imediately following an element, that will be removed.
This is actually worse, because as you remove the elements you
invalidate your iterator, so your "++it" is a very bad thing.
Better is:
vector<int> container;
vector<int>::iterator iter;
for (iter=container.begin(); iter != container.end(); )
{
if (RemoveThisElement(*iter)) {
iter = container.erase(iter);
} else {
++iter
}
}
Note the assignment of "iter" from erase, and the lack of ++iter in
the for loop. Either the iter is manually incremented or the erase
operation leaves it pointing to the next position.
--
Mark Ping em****@soda.CSUA.Berkeley.EDU
Hello All,
Thanks for the huge response, I have got. I am yet to try the
suggestions. In the mean time I am adding the code snippet here for
better clarity of the question I have asked.
Though I cannot reproduce the code, it looks similar to this.
I have a vector vecObjects with CComPtr<Interfaces I..>. Now I have to
check the contents for this vector for some conditions. Hence I iterate
the container as follows.
std::vector< CComPtr<I..> >::iterator it = vecObjects.begin();
std::vector< CComPtr<I..> >::iterator itEnd = vecObjects.end()
for(;it != itEnd; ++it); // I tried post increment also
{
// Check the iterator for some conditions
if (! IsValid(*it))
{
vecObjects.erase(it); // This causes the crash with the last
element
}
++it;
}
// At the same time the following code works
for(;it != itEnd; ++it); // I tried post increment also
{
// Check the iterator for some conditions
if (! IsValid(*it))
{
if(vecObjects.size() == 1)
vecObjects.clear();
else
vecObjects.erase(it);
}
++it;
}
Thank you for all the help.
EK
In article <11*********************@o13g2000cwo.googlegroups. com>,
<ee****@gmail.com> wrote: std::vector< CComPtr<I..> >::iterator it = vecObjects.begin(); std::vector< CComPtr<I..> >::iterator itEnd = vecObjects.end()
for(;it != itEnd; ++it); // I tried post increment also { // Check the iterator for some conditions if (! IsValid(*it)) { vecObjects.erase(it); // This causes the crash with the last element } ++it; }
Yeah, this is broken.
// At the same time the following code works
No it doesn't.
for(;it != itEnd; ++it); // I tried post increment also { // Check the iterator for some conditions if (! IsValid(*it)) { if(vecObjects.size() == 1) vecObjects.clear(); else vecObjects.erase(it); } ++it; }
the 'erase' invalidates all the iterators. You can't cache the 'end'
iterator. Furthermore, you need to use the return value of erase,
since you just invalidated 'it' when you erased it (and hence
incrementing it is unsafe).
--
Mark Ping em****@soda.CSUA.Berkeley.EDU
On 21 Dec 2005 17:01:17 -0800, ee****@gmail.com wrote: Hello All,
(...) std::vector< CComPtr<I..> >::iterator it = vecObjects.begin(); std::vector< CComPtr<I..> >::iterator itEnd = vecObjects.end()
for(;it != itEnd; ++it); // I tried post increment also { // Check the iterator for some conditions if (! IsValid(*it)) { vecObjects.erase(it); // This causes the crash with the last element } ++it; }
(...)
From the code you show us, I think the best soultion is to use the
algorithm std::remove_if using as predicte a functor written around
IsValid.
std::vector<...>::iterator lastPosition=
std::remove_if(vecObjectsbegin(),vecObjects.end(), wrapIsValid());
vecObjects.erase(lastPosition,vecObjects.end());
Or something similar.
Best regards,
Zara ee****@gmail.com writes: Hello,
I am facing a starnge problem while erasing the last member in a vector. I am using VC++ .NET 2002 complier. I have vector of CComPtr<..> (irrelevant here), and then I iterate over the vector. If it is the iterator, then I remove the element from the vector using vecObjects.erase(it). It works fine till the last element. While removing the last element it throws exception and fails. But the same vecObject.clear() works with out any problem. Can somebody there, please help me to indentify this problem and solution to this
Difficult to say without seeing your code. One possibility is that
you somewhere use an iterator which was earlier invalidated by
the call to vector<CComPtr>::erase. When an element in a vector
is erased, every iterator to that element and all elements following
it are invalidated.
If that is your problem, it could be possible that you could
get around it by iterating from end towards begin instead.
/Niklas Norrthon ee****@gmail.com writes: Hello All,
Thanks for the huge response, I have got. I am yet to try the suggestions. In the mean time I am adding the code snippet here for better clarity of the question I have asked.
Though I cannot reproduce the code, it looks similar to this.
I have a vector vecObjects with CComPtr<Interfaces I..>. Now I have to check the contents for this vector for some conditions. Hence I iterate the container as follows.
std::vector< CComPtr<I..> >::iterator it = vecObjects.begin(); std::vector< CComPtr<I..> >::iterator itEnd = vecObjects.end()
for(;it != itEnd; ++it); // I tried post increment also { // Check the iterator for some conditions if (! IsValid(*it)) { vecObjects.erase(it); // This causes the crash with the last element } ++it; }
Consider including <algorithm> and <functional> and then do this
instead:
using std::not1;
using std::ptr_fun;
using std::remove_if;
typedef std::vector<CComPtr<I...> >::iterator Iter;
Iter it = remove_if(vecObjects.begin(), vecObjects.end(),
not1(ptr_fun(IsValid)));
vecObjects.erase(it, vecObjects.end());
/Niklas Norrthon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Generic Usenet Account |
last post by:
To settle the dispute regarding what happens when an "erase" method is
invoked on an STL container (i.e. whether the element is merely
removed from the container or whether it also gets deleted in...
|
by: dalbosco |
last post by:
Hello,
I am new to STL and I've written the following code
which crash.
Could anyone tell me why this code crash?
Thanks by advance.
--
J-F
#include <iostream>
|
by: Active8 |
last post by:
I put the bare essentials in a console app.
http://home.earthlink.net/~mcolasono/tmp/degub.zip
Opening output.fft and loading it into a vector<float> screws
up, but input1.dat doesn't. It does...
|
by: laniik |
last post by:
Hi. For some reason I am getting a crash on pop_back() and Im not sure
why.
sorry I cant post the whole code because the vector is used in a bunch
of places.
i have a
vector<bool> complete;
|
by: ma740988 |
last post by:
For starters,
Happy New Year to all!!
I created a vector of pairs where pair first is a primitive and pair
second is a vector of ints.
So now:
# include <iostream>
# include <vector>
|
by: mahajan.vibhor |
last post by:
I have a list of pointers. e.g
A* a = new A(); // A is a class
stl::list<A*list_a;
I am inserting object of class in the after allocating memeory thru new
operator.
But when i want to...
|
by: cbbibleboy |
last post by:
Hey, I've been getting some very strange results with what seems to be very simple code. All I'm doing is trying to use an STL vector of "cSprite"s -- a class I wrote. The problem arises when I try...
|
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= |
last post by:
I have some code where there's this vector of pointers to objects and
I need to delete and erase some of them, the problem is that to know
which I need to iterate through the vector and I'm trying...
|
by: Rakesh Kumar |
last post by:
Hi All -
In a project of mine - I was trying to scale down the actual issue
to the following piece of code. I need to allocate an array of strings
and reserve the individual string to a particular...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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: 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...
| |