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

iterators as members

I'm wondering whether the following code construct is allowed according
to the C++ standard.

The following code fragment illustrates the situation:

#include <vector>
using namespace std;

// Class A has a member v of type std::vector<int>
class A
{
public :
std::vector<intv;
};
// class B stores an iterator (i) to such a vector:
class B
{
public :
std::vector<int>::iterator i;
};
int main()
{
A *a = new A();

B b;
b.i = a->v.begin();

// make sure a is deleted before b goes out of scope
delete a;
}

We only found this to be a problem when compiling with the Microsoft
compiler using cl /EHsc /MTd /D_HAS_ITERATOR_DEBUGGING#0. In that case,
it results in a crash inside the destructor of B::i.

Now my question: does the standard allow storing iterators like that?

thx,
jeroen

Dec 28 '06 #1
4 1252
jeroen.haegeba...@gmail.com wrote:
I'm wondering whether the following code construct is allowed according
to the C++ standard.

The following code fragment illustrates the situation:

#include <vector>
using namespace std;

// Class A has a member v of type std::vector<int>
class A
{
public :
std::vector<intv;
};
// class B stores an iterator (i) to such a vector:
class B
{
public :
std::vector<int>::iterator i;
};
int main()
{
A *a = new A();

B b;
b.i = a->v.begin();

// make sure a is deleted before b goes out of scope
delete a;
}

We only found this to be a problem when compiling with the Microsoft
compiler using cl /EHsc /MTd /D_HAS_ITERATOR_DEBUGGING#0. In that case,
it results in a crash inside the destructor of B::i.

Now my question: does the standard allow storing iterators like that?
This code is legal albeit dangerous. B::i is certainly invalidated by
the destruction of A. If you have dereferenced it after the destruction
of A (e.g. in the destructor of B), you have undefined behavior.
Destroying an invalidated iterator should not cause a problem.

Does this exact code reproduce the problem for you, and if so, on which
version of the compiler?

Cheers! --M

Dec 28 '06 #2

mlimber schreef:
jeroen.haegeba...@gmail.com wrote:
I'm wondering whether the following code construct is allowed according
to the C++ standard.

This code is legal albeit dangerous. B::i is certainly invalidated by
the destruction of A. If you have dereferenced it after the destruction
of A (e.g. in the destructor of B), you have undefined behavior.
Destroying an invalidated iterator should not cause a problem.

Does this exact code reproduce the problem for you, and if so, on which
version of the compiler?
It's that exact code construct that reproduces the problem, but I
forgot to mention: we detected this problem after installing Service
Pack 1 for Visual Studio 2005 ...

compiler version:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86

jeroen

Dec 28 '06 #3
je***************@gmail.com wrote:
mlimber schreef:
jeroen.haegeba...@gmail.com wrote:
I'm wondering whether the following code construct is allowed according
to the C++ standard.
>
This code is legal albeit dangerous. B::i is certainly invalidated by
the destruction of A. If you have dereferenced it after the destruction
of A (e.g. in the destructor of B), you have undefined behavior.
Destroying an invalidated iterator should not cause a problem.

Does this exact code reproduce the problem for you, and if so, on which
version of the compiler?

It's that exact code construct that reproduces the problem, but I
forgot to mention: we detected this problem after installing Service
Pack 1 for Visual Studio 2005 ...

compiler version:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86
I compiled it with those exact options with v. 14.00.50727.42, and it
works fine (well, it least it doesn't give any message about a crash).
You'll probably have more luck asking in a Microsoft-specific group
since this doesn't appear to be a standard C++ question any more.

Cheers! --M

Dec 28 '06 #4

mlimber schreef:
I compiled it with those exact options with v. 14.00.50727.42, and it
works fine (well, it least it doesn't give any message about a crash).
You'll probably have more luck asking in a Microsoft-specific group
since this doesn't appear to be a standard C++ question any more.
Indeed. I just first wanted to get some confirmation about whether or
not the code
construct really was legal C++.

thx,
jeroen

Dec 28 '06 #5

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

Similar topics

7
by: sks_cpp | last post by:
Consider the following: list<int> a; // assume a contains some number of integers list<int>::iterator iter = a.find(10); for (iter; iter < a.end(); ++iter) { std::cout << "found one\n"; }...
2
by: Azumanga | last post by:
Hello. A small question with regards input iterators. Table 72 of the standard says with regards ++r on an input iterator r: "any copies of the previous value of r are no longer required to...
2
by: dgront | last post by:
Greetings! I believe someone can spare me a minute and give me some ideas. Once upon a time I had a C program: double propert; char symb; char* descript; But now I want to make it in C++....
8
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a...
2
by: brutus | last post by:
Hi, I'm trying to define a map-like interface to a container. My basic problem is that the container is associative, but values are not stored in pairs internally. (and as we know, the map...
19
by: fungus | last post by:
I mentioned earlier to day that I was moving some code from VC++6 to VC++2005 and having trouble with the new iterators. There's all sorts of problems cropping up in the code thanks to this...
2
by: desktop | last post by:
Are there any case where iterators in a std::list gets invalidated besides from the iterator pointing to an element thats deleted? It seems that its only the std::vector that invalidates...
3
by: Jess | last post by:
Hello, Iterators are typically put into five different categories, namely input iterator, output iterator, forward iterator, bidirectional iterator and random iterator. The differences come...
5
by: jgscott | last post by:
I've been trawling around for an answer to this question and thought I'd try here. I have a class Graph, which has a std::list<Nodeas a class member. Node it itself a class that makes extensive...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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.