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

iterator pointing to invalid container


int main(int argc, char* argv[])
{
std::map<int, int>::iterator footest;
{
std::map<int, intfoo;
foo[0] = 0;
footest = foo.begin();
}
return 0;
}

Is this undefined behavior? Please note that footest is going out of
scope before std::map. In theory an implementation could have a
iterator destructor that could still access the std::map ?

Thanks
Raj

Jan 4 '07 #1
2 1739
Parapura Rajkumar wrote:
int main(int argc, char* argv[])
{
std::map<int, int>::iterator footest;
{
std::map<int, intfoo;
foo[0] = 0;
footest = foo.begin();
}
return 0;
}

Is this undefined behavior? Please note that footest is going out of
scope before std::map.
You mean, "after 'foo'", don't you?
In theory an implementation could have a
iterator destructor that could still access the std::map ?
No. The iterator is allowed to be invalid, that does not affect
its destruction.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 4 '07 #2
On Thu, 4 Jan 2007 00:47:13 -0500, "Victor Bazarov" wrote:
>Parapura Rajkumar wrote:
>int main(int argc, char* argv[])
{
std::map<int, int>::iterator footest;
{
std::map<int, intfoo;
foo[0] = 0;
footest = foo.begin();
}
return 0;
}

Is this undefined behavior? Please note that footest is going out of
scope before std::map.

You mean, "after 'foo'", don't you?
>In theory an implementation could have a
iterator destructor that could still access the std::map ?

No. The iterator is allowed to be invalid, that does not affect
its destruction.
Different kinds of invalid iterators are described here:
http://www.angelikalanger.com/Confer...tions-2002.pdf

Best wishes,
Roland Pibinger
Jan 4 '07 #3

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

Similar topics

4
by: Rex_chaos | last post by:
Hi there, I am writing an iterator for a container. Just like a typical iterator, template <typename ADT> class MyIter { ... MyContainer<ADT>& refc; public:
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
4
by: matthurne | last post by:
I am working through exercise 8-2 in Accelerated C++...I am implementing the function equal(b, e, b2) where b is an iterator for the first element in a container, e is an iterator pointing to one...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
5
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want...
18
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct...
16
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
11
by: barcaroller | last post by:
I've noticed that neither container.begin() nor container.end() return an error when the container is empty, so I get a nasty segfault when I dereference the iterator. Do I have to check if the...
4
by: thinktwice | last post by:
std::vector<CStringvec; vec::iterator iter; i can initiaze the iterator in vc6 like this $B!'(B iter = NULL, but it compile failed in vc2005, it tells me there is no acceptable conversion.
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
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
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
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.