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. ASSERT failure in QList<T>::operator[]: "index out of range"
Forum Updated to NodeBB v4.3 + New Features

ASSERT failure in QList<T>::operator[]: "index out of range"

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 5 Posters 10.7k Views 2 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.
  • JohanSoloJ Offline
    JohanSoloJ Offline
    JohanSolo
    wrote on last edited by
    #2

    What is the real size of your xy list?

    `They did not know it was impossible, so they did it.'
    -- Mark Twain

    1 Reply Last reply
    0
    • JerwinprabuJ Jerwinprabu

      I am still relatively new to Qt and I have recently been working on a large project. When I attempt to run the project I get this error:

      /ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/arm-linux-gnueabihf/qt5/QtCore/qlist.h, line 487
      The program has unexpectedly finished.
      

      How I might go about tracking down the source of the problem?

      I believe that the addition of this code is causing the error

      startvaluexy = Client::straightxy;
      qDebug() << "start value Received from server :" << startvaluexy;
      QStringList xy = startvaluexy.split("|");
      
      x = xy[2];
      
      QString num1 = x;
      int x = num1.toInt();
      
      qDebug() << "start x value :" << x;
      
      y = xy[3];
      
      QString num2 = y;
      int y = num2.toInt();
      
      qDebug() << "start y value :" << y;
      

      When this x = xy[2]; y = xy[3]; is taken out, then runs fine.

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

      @Jerwinprabu You should never assume that you get what you expect!
      You expect that this line

      QStringList xy = startvaluexy.split("|");
      

      will generate a list with at least 4 elements. But whether this is the case depends on the content of startvaluexy.
      So, you should check the length of xy before accessing any elements of it!
      "index out of range" - this message actually tells you what is wrong: you're accessing an element in the list which does not exist.

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

      1 Reply Last reply
      2
      • JerwinprabuJ Jerwinprabu

        I am still relatively new to Qt and I have recently been working on a large project. When I attempt to run the project I get this error:

        /ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/arm-linux-gnueabihf/qt5/QtCore/qlist.h, line 487
        The program has unexpectedly finished.
        

        How I might go about tracking down the source of the problem?

        I believe that the addition of this code is causing the error

        startvaluexy = Client::straightxy;
        qDebug() << "start value Received from server :" << startvaluexy;
        QStringList xy = startvaluexy.split("|");
        
        x = xy[2];
        
        QString num1 = x;
        int x = num1.toInt();
        
        qDebug() << "start x value :" << x;
        
        y = xy[3];
        
        QString num2 = y;
        int y = num2.toInt();
        
        qDebug() << "start y value :" << y;
        

        When this x = xy[2]; y = xy[3]; is taken out, then runs fine.

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #4

        @Jerwinprabu Hi,friend,welcome.

        I modified your code snippet.

        startvaluexy = Client::straightxy;
        qDebug() << "start value Received from server :" << startvaluexy;
        QStringList xy = startvaluexy.split("|");
        int xySize = sy.size(); ///< get the xy list size;
        
        if(xySize < 4){
            return false;
        }
        
        bool ok;
        int x = xy[2].toInt(&ok); ///< you should check whether convert success
        if(!ok){
            return false;
        }
        
        int y = xy[3].toInt(&ok);
        if(!ok){
            return false;
        }
        
        // when run here, you will get the correct x,y value
        return true;
        

        Just do it!

        1 Reply Last reply
        2
        • JerwinprabuJ Offline
          JerwinprabuJ Offline
          Jerwinprabu
          wrote on last edited by
          #5

          If I sent start command from server, output displayed like this on client side

          WebSocket connected
          message received : "straight|xy|3|0" 
          
          start xy value Received from server : "straight|xy|3|0"
          start x value emitted to Robot : 3
          start y value emitted to Robot: 0
          int of start x value emitted : 3
          int start y value emitted : 0
          

          If I click start button on client side that time only I got the error.

          WebSocket connected
          
          start xy value Received from server : ""
          ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/arm-linux-gnueabihf/qt5/QtCore/qlist.h, line 487
          
          jsulmJ 1 Reply Last reply
          0
          • JerwinprabuJ Jerwinprabu

            If I sent start command from server, output displayed like this on client side

            WebSocket connected
            message received : "straight|xy|3|0" 
            
            start xy value Received from server : "straight|xy|3|0"
            start x value emitted to Robot : 3
            start y value emitted to Robot: 0
            int of start x value emitted : 3
            int start y value emitted : 0
            

            If I click start button on client side that time only I got the error.

            WebSocket connected
            
            start xy value Received from server : ""
            ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/arm-linux-gnueabihf/qt5/QtCore/qlist.h, line 487
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #6

            @Jerwinprabu You see: you're trying to split an empty string, this is not going to work. So please add some check to your code as @joeQ suggested.
            And you should check why you don't get what server sent, but this is another issue.

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

            1 Reply Last reply
            3
            • JerwinprabuJ Offline
              JerwinprabuJ Offline
              Jerwinprabu
              wrote on last edited by Jerwinprabu
              #7

              @joeQ When I was try to run the program I got the error

              error: return-statement with a value, in function returning 'void' [-fpermissive]
                       return false;
              
              joeQJ jsulmJ 2 Replies Last reply
              0
              • JerwinprabuJ Jerwinprabu

                @joeQ When I was try to run the program I got the error

                error: return-statement with a value, in function returning 'void' [-fpermissive]
                         return false;
                
                joeQJ Offline
                joeQJ Offline
                joeQ
                wrote on last edited by joeQ
                #8

                @Jerwinprabu (⊙o⊙)…

                bool myFunctionName() ///< Notes; function return value type must bool
                {
                      /* ... */
                      return true;
                }
                

                Just do it!

                1 Reply Last reply
                4
                • JerwinprabuJ Jerwinprabu

                  @joeQ When I was try to run the program I got the error

                  error: return-statement with a value, in function returning 'void' [-fpermissive]
                           return false;
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @Jerwinprabu @joeQ Or just do

                  return;
                  

                  if you do not use the return value...

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

                  1 Reply Last reply
                  3
                  • JerwinprabuJ Offline
                    JerwinprabuJ Offline
                    Jerwinprabu
                    wrote on last edited by Jerwinprabu
                    #10

                    @jsulm @joeQ Thanks for your help. Ya @joeQ given proper sample. I have tried that. Here I have posted the solution now client side that is working fine, one time I want to check from server side. Kindly check, if anything wrong awaiting for your further response

                    startvaluexy = Client::straightxy;
                    qDebug() << "start xy value Received from server :" << startvaluexy;
                    QStringList xy = startvaluexy.split("|");
                    int xySize = xy.size(); 
                    qDebug() << "start xy size :" << xySize;
                    
                    if(xySize > 4){
                         x = xy[2];
                         y = xy[3];
                      }
                    
                         QString num1 = x;
                         int x = num1.toInt();
                    
                         qDebug() << "start x value  :" << x;
                    
                         QString num2 = y;
                         int y = num2.toInt();
                    
                         qDebug() << "start y value :" << y;
                    
                        xstart = x;
                        ystart = y;
                    

                    After checking the size will modify this xySize > 4

                    jsulmJ 1 Reply Last reply
                    0
                    • JerwinprabuJ Jerwinprabu

                      @jsulm @joeQ Thanks for your help. Ya @joeQ given proper sample. I have tried that. Here I have posted the solution now client side that is working fine, one time I want to check from server side. Kindly check, if anything wrong awaiting for your further response

                      startvaluexy = Client::straightxy;
                      qDebug() << "start xy value Received from server :" << startvaluexy;
                      QStringList xy = startvaluexy.split("|");
                      int xySize = xy.size(); 
                      qDebug() << "start xy size :" << xySize;
                      
                      if(xySize > 4){
                           x = xy[2];
                           y = xy[3];
                        }
                      
                           QString num1 = x;
                           int x = num1.toInt();
                      
                           qDebug() << "start x value  :" << x;
                      
                           QString num2 = y;
                           int y = num2.toInt();
                      
                           qDebug() << "start y value :" << y;
                      
                          xstart = x;
                          ystart = y;
                      

                      After checking the size will modify this xySize > 4

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

                      @Jerwinprabu said in ASSERT failure in QList<T>::operator[]: "index out of range":

                      if(xySize > 4){

                      shouldn't it be

                      if(xySize >= 4){
                      

                      ?
                      And if it is not >= 4 what would be the values of x and y? Shouldn't you just skip processing in this case (do return as @joeQ suggested)?

                      What are you trying to do with this

                      QString num1 = x;
                      int x = num1.toInt();
                      

                      ?! Why do you assign an integer to a string and then convert this string to integer again? Same for

                      QString num2 = y;
                      int y = num2.toInt();
                      

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

                      JerwinprabuJ 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Jerwinprabu said in ASSERT failure in QList<T>::operator[]: "index out of range":

                        if(xySize > 4){

                        shouldn't it be

                        if(xySize >= 4){
                        

                        ?
                        And if it is not >= 4 what would be the values of x and y? Shouldn't you just skip processing in this case (do return as @joeQ suggested)?

                        What are you trying to do with this

                        QString num1 = x;
                        int x = num1.toInt();
                        

                        ?! Why do you assign an integer to a string and then convert this string to integer again? Same for

                        QString num2 = y;
                        int y = num2.toInt();
                        
                        JerwinprabuJ Offline
                        JerwinprabuJ Offline
                        Jerwinprabu
                        wrote on last edited by
                        #12

                        @jsulm from server I will receive the string then I want to convert that string into int. Again I want to convert for sending to server(for map).

                        jsulmJ 1 Reply Last reply
                        0
                        • JerwinprabuJ Jerwinprabu

                          @jsulm from server I will receive the string then I want to convert that string into int. Again I want to convert for sending to server(for map).

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

                          @Jerwinprabu Then why do you convert it back from string to int? x is already int, what's the point to convert from string to int?
                          Also

                          QString num1 = x;
                          

                          is not a conversion to string from int! It will not even compile. If you want to convert int to string then do it like shown here http://doc.qt.io/qt-5/qstring.html#number

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

                          JerwinprabuJ 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @Jerwinprabu Then why do you convert it back from string to int? x is already int, what's the point to convert from string to int?
                            Also

                            QString num1 = x;
                            

                            is not a conversion to string from int! It will not even compile. If you want to convert int to string then do it like shown here http://doc.qt.io/qt-5/qstring.html#number

                            JerwinprabuJ Offline
                            JerwinprabuJ Offline
                            Jerwinprabu
                            wrote on last edited by Jerwinprabu
                            #14

                            @jsulm Because I want to take that value to int only xstart, ystart. I want to convert from string to int only. x, y is not int.

                            QString x;
                            QString y;
                            
                            jsulmJ 2 Replies Last reply
                            0
                            • JerwinprabuJ Jerwinprabu

                              @jsulm Because I want to take that value to int only xstart, ystart. I want to convert from string to int only. x, y is not int.

                              QString x;
                              QString y;
                              
                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by jsulm
                              #15

                              @Jerwinprabu Again

                              QString num1 = x;
                              int x = num1.toInt();
                              

                              x already contains the integer value! Now you convert it to string then again to int - it will be same as before.

                              int x = 1;
                              QString num1 = QString::number(x);
                              x = num1.toInt();
                              qDebug() << x;
                              

                              What do you think the last line will print out?
                              Shouldn't it be just

                              QString num1 = QString::number(x);
                              xstart = x;
                              

                              ?

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

                              1 Reply Last reply
                              0
                              • JerwinprabuJ Jerwinprabu

                                @jsulm Because I want to take that value to int only xstart, ystart. I want to convert from string to int only. x, y is not int.

                                QString x;
                                QString y;
                                
                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @Jerwinprabu Also here you redefine the variable x

                                QString num1 = x;
                                int x = num1.toInt();
                                

                                is there a reason why you're doing this?

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

                                1 Reply Last reply
                                0
                                • jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  After chatting with @Jerwinprabu I realised that I misunderstood his code: x and y are actually QString not int.

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

                                  mrjjM 1 Reply Last reply
                                  1
                                  • jsulmJ jsulm

                                    After chatting with @Jerwinprabu I realised that I misunderstood his code: x and y are actually QString not int.

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @jsulm
                                    Well x is a pretty confusing name for a string in most cases.
                                    I was sure also it was int.

                                    1 Reply Last reply
                                    1

                                    • Login

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