Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Qt6.2.1 QList index out of bound
Forum Updated to NodeBB v4.3 + New Features

Qt6.2.1 QList index out of bound

Scheduled Pinned Locked Moved Solved Qt 6
12 Posts 5 Posters 1.9k 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.
  • sbelaS Offline
    sbelaS Offline
    sbela
    wrote on last edited by sbela
    #1

    This throws an index out of bounds for me ! But I can not figure out why!
    Maybe this:
    Q_ASSERT_X(size_t(i) + size_t(n) <= size_t(d->size), "QList::remove", "index out of range");
    shoud be this
    Q_ASSERT_X(size_t(i) + size_t(n) < size_t(d->size), "QList::remove", "index out of range");

    #include <QCoreApplication>
    #include <iostream>
    
    class A
    {
    public:
        explicit A(QString name) : m_name(name) { }
    
        bool operator==(const A &other) const
        {
            return other.m_name == m_name;
        }
    private:
        QString m_name;
    };
    
    int main(int argc, char *argv[])
    {
        QCoreApplication app(argc, argv);
        A a("ONE");
        QList<A> a_list;
    
        a_list.append(a);
    
        std::cout << "Item count:" << a_list.count() << std::endl;
    
        if (auto index = a_list.indexOf(A("ONE")) > -1)
        {
            std::cout << "Item index:" << index << std::endl;
            a_list.removeAt(index);
        }
    
        return app.exec();
    }
    

    I would like!

    jsulmJ KroMignonK JonBJ 3 Replies Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #10

      If you want to use c++17 you should learn the syntax...

      if (auto index = a_list.indexOf("ONE"); index > -1)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      sbelaS 1 Reply Last reply
      1
      • sbelaS sbela

        This throws an index out of bounds for me ! But I can not figure out why!
        Maybe this:
        Q_ASSERT_X(size_t(i) + size_t(n) <= size_t(d->size), "QList::remove", "index out of range");
        shoud be this
        Q_ASSERT_X(size_t(i) + size_t(n) < size_t(d->size), "QList::remove", "index out of range");

        #include <QCoreApplication>
        #include <iostream>
        
        class A
        {
        public:
            explicit A(QString name) : m_name(name) { }
        
            bool operator==(const A &other) const
            {
                return other.m_name == m_name;
            }
        private:
            QString m_name;
        };
        
        int main(int argc, char *argv[])
        {
            QCoreApplication app(argc, argv);
            A a("ONE");
            QList<A> a_list;
        
            a_list.append(a);
        
            std::cout << "Item count:" << a_list.count() << std::endl;
        
            if (auto index = a_list.indexOf(A("ONE")) > -1)
            {
                std::cout << "Item index:" << index << std::endl;
                a_list.removeAt(index);
            }
        
            return app.exec();
        }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @sbela said in Qt6.2.1 QList index out of bound:

        std::cout << "Item index:" << index << std::endl;

        What does this print out?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • sbelaS Offline
          sbelaS Offline
          sbela
          wrote on last edited by
          #3

          This is the output:

          16:33:31: Starting /home/.../work/projects/build-qlistqt6test-Desktop_Qt_6_2_1_GCC_64bit-Debug/qlistqt6test...
          Item count:1
          Item index:1
          ASSERT failure in QList::remove: "index out of range", file /opt/Qt5.15.0/6.2.1/gcc_64/include/QtCore/qlist.h, line 741
          16:33:31: /home/.../work/projects/build-qlistqt6test-Desktop_Qt_6_2_1_GCC_64bit-Debug/qlistqt6test crashed.

          I would like!

          jsulmJ 1 Reply Last reply
          0
          • sbelaS sbela

            This is the output:

            16:33:31: Starting /home/.../work/projects/build-qlistqt6test-Desktop_Qt_6_2_1_GCC_64bit-Debug/qlistqt6test...
            Item count:1
            Item index:1
            ASSERT failure in QList::remove: "index out of range", file /opt/Qt5.15.0/6.2.1/gcc_64/include/QtCore/qlist.h, line 741
            16:33:31: /home/.../work/projects/build-qlistqt6test-Desktop_Qt_6_2_1_GCC_64bit-Debug/qlistqt6test crashed.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @sbela said in Qt6.2.1 QList index out of bound:

            Item index:1

            Then you're indeed out of bounds.
            Just not sure why indexOf returns 1 instead of 0

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • sbelaS sbela

              This throws an index out of bounds for me ! But I can not figure out why!
              Maybe this:
              Q_ASSERT_X(size_t(i) + size_t(n) <= size_t(d->size), "QList::remove", "index out of range");
              shoud be this
              Q_ASSERT_X(size_t(i) + size_t(n) < size_t(d->size), "QList::remove", "index out of range");

              #include <QCoreApplication>
              #include <iostream>
              
              class A
              {
              public:
                  explicit A(QString name) : m_name(name) { }
              
                  bool operator==(const A &other) const
                  {
                      return other.m_name == m_name;
                  }
              private:
                  QString m_name;
              };
              
              int main(int argc, char *argv[])
              {
                  QCoreApplication app(argc, argv);
                  A a("ONE");
                  QList<A> a_list;
              
                  a_list.append(a);
              
                  std::cout << "Item count:" << a_list.count() << std::endl;
              
                  if (auto index = a_list.indexOf(A("ONE")) > -1)
                  {
                      std::cout << "Item index:" << index << std::endl;
                      a_list.removeAt(index);
                  }
              
                  return app.exec();
              }
              
              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #5

              @sbela I have many problems with your code. Does this really build?

              AFAIK, to use QList you need a default constructor. So I would change explicit A(QString name) : m_name(name) { } to explicit A(const QString &name = QString()) : m_name(name) { }.

              Second, I may be wrong, but for my understanding if (auto index = a_list.indexOf(A("ONE")) > -1) will create a local variable index which is destroyed after the test.
              And the calculation is also wrong your have done index = a_list.indexOf(A("ONE")) > -1, which should be (index = a_list.indexOf(A("ONE"))) > -1

              I would change this to:

              int index;
                  if ((index = a_list.indexOf(A("ONE"))) > -1)
                  {
                      std::cout << "Item index:" << index << std::endl;
                      a_list.removeAt(index);
                  }
              

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              sbelaS 1 Reply Last reply
              1
              • sbelaS sbela

                This throws an index out of bounds for me ! But I can not figure out why!
                Maybe this:
                Q_ASSERT_X(size_t(i) + size_t(n) <= size_t(d->size), "QList::remove", "index out of range");
                shoud be this
                Q_ASSERT_X(size_t(i) + size_t(n) < size_t(d->size), "QList::remove", "index out of range");

                #include <QCoreApplication>
                #include <iostream>
                
                class A
                {
                public:
                    explicit A(QString name) : m_name(name) { }
                
                    bool operator==(const A &other) const
                    {
                        return other.m_name == m_name;
                    }
                private:
                    QString m_name;
                };
                
                int main(int argc, char *argv[])
                {
                    QCoreApplication app(argc, argv);
                    A a("ONE");
                    QList<A> a_list;
                
                    a_list.append(a);
                
                    std::cout << "Item count:" << a_list.count() << std::endl;
                
                    if (auto index = a_list.indexOf(A("ONE")) > -1)
                    {
                        std::cout << "Item index:" << index << std::endl;
                        a_list.removeAt(index);
                    }
                
                    return app.exec();
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6

                @sbela said in Qt6.2.1 QList index out of bound:

                if (auto index = a_list.indexOf(A("ONE")) > -1)

                Oh dear! And this is why index is being set to 1 @jsulm

                Hint: Operator precedence.... :)

                P.S.
                @KroMignon
                Your post crossed with mine.

                Second, I may be wrong, but for my understanding if (auto index = a_list.indexOf(A("ONE")) > -1) will create a local variable index which is destroyed after the test.

                No. Says in scope for whole of if body. New (well not very new) C++ thingie :)

                which should be (index = a_list.indexOf(A("ONE"))) > -1

                Indeed! But you gave it away! ;-) I assume the whole line with the auto would actually read:

                if ((auto index = a_list.indexOf(A("ONE"))) > -1)
                
                jsulmJ sbelaS 2 Replies Last reply
                1
                • JonBJ JonB

                  @sbela said in Qt6.2.1 QList index out of bound:

                  if (auto index = a_list.indexOf(A("ONE")) > -1)

                  Oh dear! And this is why index is being set to 1 @jsulm

                  Hint: Operator precedence.... :)

                  P.S.
                  @KroMignon
                  Your post crossed with mine.

                  Second, I may be wrong, but for my understanding if (auto index = a_list.indexOf(A("ONE")) > -1) will create a local variable index which is destroyed after the test.

                  No. Says in scope for whole of if body. New (well not very new) C++ thingie :)

                  which should be (index = a_list.indexOf(A("ONE"))) > -1

                  Indeed! But you gave it away! ;-) I assume the whole line with the auto would actually read:

                  if ((auto index = a_list.indexOf(A("ONE"))) > -1)
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @JonB Good catch!

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @sbela said in Qt6.2.1 QList index out of bound:

                    if (auto index = a_list.indexOf(A("ONE")) > -1)

                    Oh dear! And this is why index is being set to 1 @jsulm

                    Hint: Operator precedence.... :)

                    P.S.
                    @KroMignon
                    Your post crossed with mine.

                    Second, I may be wrong, but for my understanding if (auto index = a_list.indexOf(A("ONE")) > -1) will create a local variable index which is destroyed after the test.

                    No. Says in scope for whole of if body. New (well not very new) C++ thingie :)

                    which should be (index = a_list.indexOf(A("ONE"))) > -1

                    Indeed! But you gave it away! ;-) I assume the whole line with the auto would actually read:

                    if ((auto index = a_list.indexOf(A("ONE"))) > -1)
                    
                    sbelaS Offline
                    sbelaS Offline
                    sbela
                    wrote on last edited by
                    #8

                    Ok! But this:

                    if ((auto index = a_list.indexOf(A("ONE"))) > -1)
                    

                    does not compile for me because:

                    error: expected primary-expression before ‘auto’
                       52 |     if ((auto index = a_list.indexOf(A("ONE"))) > -1)
                          |          ^~~~
                    

                    @JonB said in Qt6.2.1 QList index out of bound:

                    if ((auto index = a_list.indexOf(A("ONE"))) > -1)

                    I would like!

                    JonBJ 1 Reply Last reply
                    0
                    • sbelaS sbela

                      Ok! But this:

                      if ((auto index = a_list.indexOf(A("ONE"))) > -1)
                      

                      does not compile for me because:

                      error: expected primary-expression before ‘auto’
                         52 |     if ((auto index = a_list.indexOf(A("ONE"))) > -1)
                            |          ^~~~
                      

                      @JonB said in Qt6.2.1 QList index out of bound:

                      if ((auto index = a_list.indexOf(A("ONE"))) > -1)

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #9

                      @sbela
                      It was only a guess, because I would never use auto, and not in the condition of an if!

                      Can it be:

                       if (auto (index = a_list.indexOf(A("ONE"))) > -1)
                      

                      ? I can't believe that's right!

                      In which case, you tell me how you can both use auto (or explicit type specifier) and get the operator precedence right here, because it's not clicking with me :) And I'm not sure how to Google for this....

                      P.S.
                      Did I recently see yet another C++-ism which would allow this:

                       if (auto index; (index = a_list.indexOf(A("ONE"))) > -1)
                      

                      ? See that ; and multi-statement in the condition? I believe that's a "thing" now with C++, might require a recent version? @jsulm , @KroMignon ?

                      But that won't work with auto. So presumably either of:

                       if (int index; (index = a_list.indexOf(A("ONE"))) > -1)
                       if (auto index = a_list.indexOf(A("ONE")); index > -1)
                      
                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        If you want to use c++17 you should learn the syntax...

                        if (auto index = a_list.indexOf("ONE"); index > -1)

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        sbelaS 1 Reply Last reply
                        1
                        • KroMignonK KroMignon

                          @sbela I have many problems with your code. Does this really build?

                          AFAIK, to use QList you need a default constructor. So I would change explicit A(QString name) : m_name(name) { } to explicit A(const QString &name = QString()) : m_name(name) { }.

                          Second, I may be wrong, but for my understanding if (auto index = a_list.indexOf(A("ONE")) > -1) will create a local variable index which is destroyed after the test.
                          And the calculation is also wrong your have done index = a_list.indexOf(A("ONE")) > -1, which should be (index = a_list.indexOf(A("ONE"))) > -1

                          I would change this to:

                          int index;
                              if ((index = a_list.indexOf(A("ONE"))) > -1)
                              {
                                  std::cout << "Item index:" << index << std::endl;
                                  a_list.removeAt(index);
                              }
                          
                          sbelaS Offline
                          sbelaS Offline
                          sbela
                          wrote on last edited by
                          #11

                          This works! But this is not what I want to write... :)
                          I want that "index" to be local to that "if" - if that is possible?! :)

                          @KroMignon said in Qt6.2.1 QList index out of bound:

                          int index;
                          if ((index = a_list.indexOf(A("ONE"))) > -1)
                          {
                          std::cout << "Item index:" << index << std::endl;
                          a_list.removeAt(index);
                          }

                          I would like!

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            If you want to use c++17 you should learn the syntax...

                            if (auto index = a_list.indexOf("ONE"); index > -1)

                            sbelaS Offline
                            sbelaS Offline
                            sbela
                            wrote on last edited by
                            #12

                            This works! Thank you!

                            if (int index = a_list.indexOf(A("ONE")); index > -1)
                            

                            @Christian-Ehrlicher

                            I would like!

                            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