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. [Application crashes when I use std::reverse function to reverse a QString]
Qt 6.11 is out! See what's new in the release blog

[Application crashes when I use std::reverse function to reverse a QString]

Scheduled Pinned Locked Moved Solved Qt 6
14 Posts 5 Posters 3.8k Views
  • 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.
  • J.HilkJ J.Hilk

    @JonB even worse, the call is to mid() twice, meaning begin() and end() point to completely different objects.

    because std::reverse should work just fine on a QString.

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

    @J-Hilk said in [Application crashes when I use std::reverse function to reverse a QString]:

    @JonB even worse, the call is to mid() twice, meaning begin() and end() point to completely different objects.

    That is indeed a good observation :) Shouldn't std::reverse() complain that the begin() & end() range do not refer into the same object? :)

    Then I suspect a (single!) QString::midRef() into the QString could be passed as begin/end() to std::reverse() and all should be well?

    1 Reply Last reply
    0
    • A appdev

      Hello again,

      I hope you're doing well.

      So After reading my csv file and displaying its values in a QTable which has 24 rows and 13 columns, I wanted to extract a part from the items that exist in the 9th column of every row, and then reverse them, and finally add them to a 10th column.

      In fact I could only extract the parts I need and add each one to the 10 th column of every row, however when I add the reverse function , the application crashes when I run it.

      Here is an explanation of what I want to do:
      9th column.png

      And here is what I tried to do :

      for (int jx = 0; jx< colCount; ++jx) {
      
      
                 setValueAt(rows, jx, values.at(jx));  //I'm adding other values
      
                 std:: reverse(values.at(8).mid(90,8).begin(), values.at(8).mid(90,8).end());
      
                 setValueAt(rows,9,values.at(8).mid(90,8)); // I added another column which values are some extracted QString from an initial QString at the position 90
      
      

      Also in the code, initially values is a QList, when I read the csv file, I store its data in a QList then I add them to QTable.

      When I remove the reverse function everything works fine, The parts are extracted and added to the 10th column however when I use it to reverse the data extracted, the app crashes.
      I ran it in debug mode, the problem seems to be in this line(160) in the qarraydata.h file
      QArray.PNG

      And in the output of the application, I get this message:
      error1.PNG

      I don't really understand what's going on and what's the real problem.
      Can anyone help me please ?
      Any little help is really appreciated and thank you.

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #5

      @appdev said in [Application crashes when I use std::reverse function to reverse a QString]:

      std:: reverse(values.at(8).mid(90,8).begin(), values.at(8).mid(90,8).end());
      

      How to you expect this to work?
      You are working with temporary created instances.
      From QString::mid() documentation:

      Returns a string that contains n characters of this string, starting at the specified position index.

      This will return a new QString instance.

      This could work with QString::midRef() which returns a reference on current QString.

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        appdev
        wrote on last edited by appdev
        #6

        First of all thank you for all your replies but there is something I still didn't really get.

        The mid() which was called twice and begin()/end() range that don't refer to the same object.

        Well, Yesterday I saw a post where someone wanted to reverse a QString.

        The answer was as follows:

        QString s = "variable";
        std::reverse(s.begin(), s.end());
        qDebug() << s;    //output "elbairav"
        

        link text

        So I thought that begin() points to the first character of the QString and end() to the last one.

        I thought by doing:

        values.at(8).mid(90,8).begin()
        

        begin () would point to the first character of the extracted QString and end() to its last.
        For example: let's say the 8th element of values which is a QList of QString is 123498f1258963.
        mid(7,8) would return f1258963 and begin() would point to f and end() would point to 3.

        so to sum it all up,

        values.at(8).mid(7,8)
        

        would return f1258963 which is a QString

        std:: reverse(values.at(8).mid(90,8).begin(), values.at(8).mid(90,8).end());
        

        is the same as

        std:: reverse(s.begin(), s.end());
        

        where s is f1258963.

        I didn't get what's wrong with mid() being called twice.

        Also I included QStringRef but all I got was QStringRef file not found.

        I also stored the values in the table and then tried to reverse them by accessing to their columns as follows:

        reverse (mModel->item(rows,9)->text().begin(),mModel->item(rows,9)->text().end());
        
        J.HilkJ JonBJ KroMignonK 3 Replies Last reply
        0
        • A appdev

          First of all thank you for all your replies but there is something I still didn't really get.

          The mid() which was called twice and begin()/end() range that don't refer to the same object.

          Well, Yesterday I saw a post where someone wanted to reverse a QString.

          The answer was as follows:

          QString s = "variable";
          std::reverse(s.begin(), s.end());
          qDebug() << s;    //output "elbairav"
          

          link text

          So I thought that begin() points to the first character of the QString and end() to the last one.

          I thought by doing:

          values.at(8).mid(90,8).begin()
          

          begin () would point to the first character of the extracted QString and end() to its last.
          For example: let's say the 8th element of values which is a QList of QString is 123498f1258963.
          mid(7,8) would return f1258963 and begin() would point to f and end() would point to 3.

          so to sum it all up,

          values.at(8).mid(7,8)
          

          would return f1258963 which is a QString

          std:: reverse(values.at(8).mid(90,8).begin(), values.at(8).mid(90,8).end());
          

          is the same as

          std:: reverse(s.begin(), s.end());
          

          where s is f1258963.

          I didn't get what's wrong with mid() being called twice.

          Also I included QStringRef but all I got was QStringRef file not found.

          I also stored the values in the table and then tried to reverse them by accessing to their columns as follows:

          reverse (mModel->item(rows,9)->text().begin(),mModel->item(rows,9)->text().end());
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #7

          @appdev it's not pointing to the character, its pointing to the memory where the character is, of the temporary object, that mid() returns

                    setValueAt(rows, jx, values.at(jx));  //I'm adding other values
                    auto subString = values.at(8).mid(90,8);
                     std:: reverse(subString.begin(), subString.end());
          
                     setValueAt(subString); 
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3
          • A appdev

            First of all thank you for all your replies but there is something I still didn't really get.

            The mid() which was called twice and begin()/end() range that don't refer to the same object.

            Well, Yesterday I saw a post where someone wanted to reverse a QString.

            The answer was as follows:

            QString s = "variable";
            std::reverse(s.begin(), s.end());
            qDebug() << s;    //output "elbairav"
            

            link text

            So I thought that begin() points to the first character of the QString and end() to the last one.

            I thought by doing:

            values.at(8).mid(90,8).begin()
            

            begin () would point to the first character of the extracted QString and end() to its last.
            For example: let's say the 8th element of values which is a QList of QString is 123498f1258963.
            mid(7,8) would return f1258963 and begin() would point to f and end() would point to 3.

            so to sum it all up,

            values.at(8).mid(7,8)
            

            would return f1258963 which is a QString

            std:: reverse(values.at(8).mid(90,8).begin(), values.at(8).mid(90,8).end());
            

            is the same as

            std:: reverse(s.begin(), s.end());
            

            where s is f1258963.

            I didn't get what's wrong with mid() being called twice.

            Also I included QStringRef but all I got was QStringRef file not found.

            I also stored the values in the table and then tried to reverse them by accessing to their columns as follows:

            reverse (mModel->item(rows,9)->text().begin(),mModel->item(rows,9)->text().end());
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #8

            @appdev said in [Application crashes when I use std::reverse function to reverse a QString]:

            Also I included QStringRef but all I got was QStringRef file not found.

            There was nothing to "include". [EDIT My bad. See @jsulm's post below about Qt6 which you are using.]

            As said earlier, if you wish to reverse the characters in place in the middle of your string, you will need to use QString::midRef(). You currently pass QString::mid() to std::reverse(), so that would only reverse a copy of the mid section of the original string, not the mid section of the actual source string.

            1 Reply Last reply
            1
            • A appdev

              First of all thank you for all your replies but there is something I still didn't really get.

              The mid() which was called twice and begin()/end() range that don't refer to the same object.

              Well, Yesterday I saw a post where someone wanted to reverse a QString.

              The answer was as follows:

              QString s = "variable";
              std::reverse(s.begin(), s.end());
              qDebug() << s;    //output "elbairav"
              

              link text

              So I thought that begin() points to the first character of the QString and end() to the last one.

              I thought by doing:

              values.at(8).mid(90,8).begin()
              

              begin () would point to the first character of the extracted QString and end() to its last.
              For example: let's say the 8th element of values which is a QList of QString is 123498f1258963.
              mid(7,8) would return f1258963 and begin() would point to f and end() would point to 3.

              so to sum it all up,

              values.at(8).mid(7,8)
              

              would return f1258963 which is a QString

              std:: reverse(values.at(8).mid(90,8).begin(), values.at(8).mid(90,8).end());
              

              is the same as

              std:: reverse(s.begin(), s.end());
              

              where s is f1258963.

              I didn't get what's wrong with mid() being called twice.

              Also I included QStringRef but all I got was QStringRef file not found.

              I also stored the values in the table and then tried to reverse them by accessing to their columns as follows:

              reverse (mModel->item(rows,9)->text().begin(),mModel->item(rows,9)->text().end());
              
              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #9

              @appdev You are mixing up several things which are (maybe similar) but not equivalent.

              Yes you can use std::reverse() with QString that's not the point here.

              The problem is QString::mid() returns a new QString instance, that is what is wrong with your code.
              With std:: reverse(values.at(8).mid(90,8).begin(), values.at(8).mid(90,8).end()); you are using 2 different QString instances.

              begin() and end() are not for the same QString instance.
              It would be the same as doing:

              QString str1 = "My pretty string";
              QString str2 = "My pretty string";
              std:: reverse(str1.begin(), str2.end());
              

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

              1 Reply Last reply
              4
              • A Offline
                A Offline
                appdev
                wrote on last edited by appdev
                #10

                Okay, thank you all for helping.

                And yeah now I see why I'm using two different QString instances.

                Also, I get no member named midRef in QString :///

                jsulmJ 1 Reply Last reply
                0
                • A appdev

                  Okay, thank you all for helping.

                  And yeah now I see why I'm using two different QString instances.

                  Also, I get no member named midRef in QString :///

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

                  @appdev https://doc.qt.io/qt-5/qstring.html#midRef
                  What Qt version do you use?

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

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    appdev
                    wrote on last edited by
                    #12

                    @jsulm Qt6

                    jsulmJ 1 Reply Last reply
                    0
                    • A appdev

                      @jsulm Qt6

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

                      @appdev For Qt6 see https://doc-snapshots.qt.io/qt6-dev/qtcore-changes-qt6.html ("The QStringRef class" section).

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

                      1 Reply Last reply
                      2
                      • A Offline
                        A Offline
                        appdev
                        wrote on last edited by
                        #14

                        Thank you all for your help.

                        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