Result of operations on empty multimap?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Moschops

    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<d ouble, aStructWeHaveDe fined>::iterato r 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


  • Pete Becker

    #2
    Re: Result of operations on empty multimap?

    On 2008-10-30 07:20:58 -0400, "Moschops" <moschops@notva lid.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<d ouble, aStructWeHaveDe fined>::iterato r 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)

    Comment

    • Richard Herring

      #3
      Re: Result of operations on empty multimap?

      In message <20081030073258 43658-pete@versatilec odingcom>, Pete Becker
      <pete@versatile coding.comwrite s
      >On 2008-10-30 07:20:58 -0400, "Moschops" <moschops@notva lid.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<d ouble, aStructWeHaveDe fined>::iterato r 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

      Comment

      • Pete Becker

        #4
        Re: Result of operations on empty multimap?

        On 2008-10-30 08:22:14 -0400, Richard Herring <junk@[127.0.0.1]said:
        In message <20081030073258 43658-pete@versatilec odingcom>, Pete Becker
        <pete@versatile coding.comwrite s
        >On 2008-10-30 07:20:58 -0400, "Moschops" <moschops@notva lid.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<d ouble, aStructWeHaveDe fined>::iterato r 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)

        Comment

        • Kai-Uwe Bux

          #5
          Re: Result of operations on empty multimap?

          Pete Becker wrote:
          On 2008-10-30 07:20:58 -0400, "Moschops" <moschops@notva lid.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, aStructWeHaveDe fined>::iterato r 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

          Comment

          • Juha Nieminen

            #6
            Re: Result of operations on empty multimap?

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

            Comment

            • Paavo Helde

              #7
              Re: Result of operations on empty multimap?

              "Moschops" <moschops@notva lid.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<d ouble, aStructWeHaveDe fined>::iterato r 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



              Comment

              • Hendrik Schober

                #8
                Re: Result of operations on empty multimap?

                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<d ouble, aStructWeHaveDe fined>::iterato r 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

                Comment

                Working...