[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Result of operations on empty multimap?

Moschops

10/30/2008 11:21:00 AM

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


7 Answers

Pete Becker

10/30/2008 11:33:00 AM

0

On 2008-10-30 07:20:58 -0400, "Moschops" <moschops@notvalid.com> said:

> 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)

Richard Herring

10/30/2008 12:22:00 PM

0

In message <2008103007325843658-pete@versatilecodingcom>, Pete Becker
<pete@versatilecoding.com> writes
>On 2008-10-30 07:20:58 -0400, "Moschops" <moschops@notvalid.com> said:
>
>> 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

Pete Becker

10/30/2008 12:31:00 PM

0

On 2008-10-30 08:22:14 -0400, Richard Herring <junk@[127.0.0.1]> said:

> In message <2008103007325843658-pete@versatilecodingcom>, Pete Becker
> <pete@versatilecoding.com> writes
>> On 2008-10-30 07:20:58 -0400, "Moschops" <moschops@notvalid.com> said:
>>
>>> 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)

Kai-Uwe Bux

10/30/2008 12:51:00 PM

0

Pete Becker wrote:

> On 2008-10-30 07:20:58 -0400, "Moschops" <moschops@notvalid.com> said:
>
>> 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

Juha Nieminen

10/30/2008 7:28:00 PM

0

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--.

Paavo Helde

11/2/2008 11:29:00 PM

0

"Moschops" <moschops@notvalid.com> kirjutas:

> 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



Hendrik Schober

11/3/2008 5:18:00 PM

0

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