Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The Dangers of Copy On Write (COW)
Forum Updated to NodeBB v4.3 + New Features

The Dangers of Copy On Write (COW)

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hmuelner
    wrote on last edited by
    #1

    I had the following function in my code:
    @ResultConstIterator getIterator(const ResultVector& checkArray, size_t argumentNumber)
    {
    if (argumentNumber < checkArray.size()) {
    return checkArray.begin()+argumentNumber;
    } else {
    return checkArray.end();
    }
    }
    @

    which was called like this:

    @
    ResultConstIterator entry = getIterator(checkArray, nr);
    if (entry != checkArray.end()) {
    doSomething(entry);
    }
    @

    When I changed the type of ResultVector from std::vector to QVector suddenly doSomething was called in every case. Fortunately I had a unit test that caught this. After some debugging and head scratching I found the cause of the changed behaviour.

    Do you also see it?

    Helmut Mülner

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rahul Das
      wrote on last edited by
      #2

      I am bit curious here.
      QVector::end () returns a pointer pointing to the imaginary item after the last item in the vector.
      And The vector::end returns iterator [Random Access Iterator] referring the last element .
      'entry' is of type ResultConstIterator .

      So when checkArray is QVector, @if (entry != checkArray.end()) @ means you are comparing a ResultConstIterator with an iterator?

      Just to make sure, if i am understanding correctly.

      And yea, Implicit sharing is the main difference between a std::vector and QVector.


      Declaration of (Platform) independence.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        You're mixing const iterators with non-const iterators. Don't :-)

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hmuelner
          wrote on last edited by
          #4

          The problem is indeed comparing an iterator with a const iterator, which is not easy to prevent in C++03. The correct code should be:

          @
          ResultConstIterator entry = getIterator(checkArray, nr);
          ResultConstIterator constEnd = checkArray.end();
          if (entry != constEnd) {
          doSomething(entry);
          }
          @

          QVector::end() does a detach() resulting in different end pointers.

          With QVector constEnd() can be used, and C++11 has cend().

          Helmut Mülner

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved