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

Result of operations on empty multimap?

In moving some code from VS6 to VS2008 (bear with me, this is not a VS
question, I'm just setting context), we find new crashes that weren't there
before and we think they're related to trying an operation on a multimap
that is empty - for example,

std::multimap<double, aStructWeHaveDefined>::iterator low;
low = c.begin() ; // c is empty (i.e. c.empty()==1) but we haven't
checked for it

In VS6 these crashed did not occur, in VS2008 they do. Can anyone tell me
what is supposed to happen? Is there an exception thrown, or does c.begin()
return some value indicating that the multimap is empty, or is it undefined?
I've had a root through Josuttis but he doesn't go into what happens when
you do something so silly as play around with empty multimaps.

Moschops
Oct 30 '08 #1
7 2299
On 2008-10-30 07:20:58 -0400, "Moschops" <mo******@notvalid.comsaid:
In moving some code from VS6 to VS2008 (bear with me, this is not a VS
question, I'm just setting context), we find new crashes that weren't there
before and we think they're related to trying an operation on a multimap
that is empty - for example,

std::multimap<double, aStructWeHaveDefined>::iterator low;
low = c.begin() ; // c is empty (i.e. c.empty()==1) but we haven't
checked for it

In VS6 these crashed did not occur, in VS2008 they do. Can anyone tell me
what is supposed to happen?
The behavior is undefined.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 30 '08 #2
In message <2008103007325843658-pete@versatilecodingcom>, Pete Becker
<pe**@versatilecoding.comwrites
>On 2008-10-30 07:20:58 -0400, "Moschops" <mo******@notvalid.comsaid:
>In moving some code from VS6 to VS2008 (bear with me, this is not a VS
question, I'm just setting context), we find new crashes that weren't there
before and we think they're related to trying an operation on a multimap
that is empty - for example,
std::multimap<double, aStructWeHaveDefined>::iterator low;
low = c.begin() ; // c is empty (i.e. c.empty()==1) but we haven't
checked for it
In VS6 these crashed did not occur, in VS2008 they do. Can anyone
tell me
what is supposed to happen?

The behavior is undefined.
???

Surely the result of calling begin() on an empty multimap (or any other
std:: container, for that matter) is an iterator which compares equal to
that returned by end().

Doing anything with that iterator other than comparing it may be
undefined, but that's another matter.

--
Richard Herring
Oct 30 '08 #3
On 2008-10-30 08:22:14 -0400, Richard Herring <ju**@[127.0.0.1]said:
In message <2008103007325843658-pete@versatilecodingcom>, Pete Becker
<pe**@versatilecoding.comwrites
>On 2008-10-30 07:20:58 -0400, "Moschops" <mo******@notvalid.comsaid:
>>In moving some code from VS6 to VS2008 (bear with me, this is not a VS
question, I'm just setting context), we find new crashes that weren't there
before and we think they're related to trying an operation on a multimap
that is empty - for example,
std::multimap<double, aStructWeHaveDefined>::iterator low;
low = c.begin() ; // c is empty (i.e. c.empty()==1) but we haven't
checked for it
In VS6 these crashed did not occur, in VS2008 they do. Can anyone tell me
what is supposed to happen?

The behavior is undefined.

???

Surely the result of calling begin() on an empty multimap (or any other
std:: container, for that matter) is an iterator which compares equal
to that returned by end().
Whoops, I read it as c.first(). You're absolutely right: c.begin() is
well defined.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 30 '08 #4
Pete Becker wrote:
On 2008-10-30 07:20:58 -0400, "Moschops" <mo******@notvalid.comsaid:
>In moving some code from VS6 to VS2008 (bear with me, this is not a VS
question, I'm just setting context), we find new crashes that weren't
there before and we think they're related to trying an operation on a
multimap that is empty - for example,

std::multimap<double, aStructWeHaveDefined>::iterator low;
low = c.begin() ; // c is empty (i.e. c.empty()==1) but we haven't
checked for it

In VS6 these crashed did not occur, in VS2008 they do. Can anyone tell me
what is supposed to happen?

The behavior is undefined.
I don't see any undefined behavior in the snippet. Shouldn't c.begin() just
return the same value as c.end()?

As far as I can see, undefined behavior enters the scene when low is
dereferenced (as in *low).
Best

Kai-Uwe Bux
Oct 30 '08 #5
Kai-Uwe Bux wrote:
As far as I can see, undefined behavior enters the scene when low is
dereferenced (as in *low).
It also enters the scene if you attempt to modify the iterator by
calling its operator++ or operator--.
Oct 30 '08 #6
"Moschops" <mo******@notvalid.comkirjutas:
In moving some code from VS6 to VS2008 (bear with me, this is not a VS
question, I'm just setting context), we find new crashes that weren't
there before and we think they're related to trying an operation on a
multimap that is empty - for example,

std::multimap<double, aStructWeHaveDefined>::iterator low;
low = c.begin() ; // c is empty (i.e. c.empty()==1) but we haven't
checked for it

In VS6 these crashed did not occur, in VS2008 they do. Can anyone tell
me what is supposed to happen? Is there an exception thrown, or does
c.begin() return some value indicating that the multimap is empty, or
is it undefined? I've had a root through Josuttis but he doesn't go
into what happens when you do something so silly as play around with
empty multimaps.
This is probably due to the "checked iterators" feature newer VC++
versions are sporting. These checks are presumably technically correct by
the letter of standard, even if the code would have not encountered any
problem without them (with the same VC++ compiler on the same hardware),
like taking an address of a nonexisting container element, which is not
used later however (in the flat memory model Windows and Linux are using
one can construct any address without a fear to generate an hardware
exception).

For code portability the errors should be fixed though. Any dereference
of the iterator will trigger the problem in the case of an empty
container.

Paavo

Nov 2 '08 #7
Moschops wrote:
In moving some code from VS6 to VS2008 (bear with me, this is not a VS
question, I'm just setting context), we find new crashes that weren't there
before and we think they're related to trying an operation on a multimap
that is empty - for example,

std::multimap<double, aStructWeHaveDefined>::iterator low;
low = c.begin() ; // c is empty (i.e. c.empty()==1) but we haven't
checked for it

In VS6 these crashed did not occur, in VS2008 they do. Can anyone tell me
what is supposed to happen? Is there an exception thrown, or does c.begin()
return some value indicating that the multimap is empty, or is it undefined?
I've had a root through Josuttis but he doesn't go into what happens when
you do something so silly as play around with empty multimaps.
You haven't said how such a crash manifested. Was it the
"helpful" iterator debugging stuff? If so, ask in a MS-
specific newsgroup how to turn it off.
Moschops
Schobi
Nov 4 '08 #8

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

Similar topics

12
by: Tanguy Fautré | last post by:
Hello, does std::multimap make any guarantee about the insertion order? for example: int main() { std::multimap<int, int> Map;
9
by: Dennis Jones | last post by:
Hi, Is there a way to iterate through a multimap in such a way as to encounter only the unique keys? In other words, since a multimap allows duplicate keys, I would like to iterate through the...
3
by: He Shiming | last post by:
Hi Folks, Happy holidays! I have a question regarding STL multimap. Basically, the current multimap<int,int> look like this: key=>value 1=>10, 1=>20, 1=>30,
9
by: R.Z. | last post by:
i was wondering whether it pays off in terms of memory use to maintain lots of empty deques (it would be convenient for my algorithms but memory use is more important). and does the size of a deque...
4
by: Nick Keighley | last post by:
Hi, I've checked out various documentation for multimap but can't find anywhere it explicitly stated that insert() invalidates multimap iterators. consider this pseudo code:- int...
4
by: sks | last post by:
I have a question regarding std::multimap/iterators. At the SGI website, it says "Erasing an element from a multimap also does not invalidate any iterators, except, of course, for iterators that...
1
by: Saile | last post by:
I want to give an array the values from the specific multimap's key's values. multimap<string,int> mymultimap; multimap<string,int>::iterator it;...
1
by: ambarish.mitra | last post by:
Hi all, I have a multimap, where key is an int and the value is a class. I can insert into the multimap, but finding it difficult to retrieve the value when keys match. I can do this with...
20
by: puzzlecracker | last post by:
I am using while loop for that but I am sure you can do it quicker and more syntactically clear with copy function. Here is what I do and would like to if someone has a cleaner solution: ...
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: 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
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.