Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. Recurring C++ and Qt anti-patterns
QtWS25 Last Chance

Recurring C++ and Qt anti-patterns

Scheduled Pinned Locked Moved The Lounge
126 Posts 17 Posters 58.6k 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.
  • A Offline
    A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on 27 May 2019, 07:24 last edited by aha_1980
    #1

    When doing code review, you always see things that hurt your eyes, and it's impossible to get away from them.

    I thought it would be good to collect them as bad example - Feel free to add your's to the hall of shame.

    So here we go, one thing I see again and again:

    int items = 42;
    QString s = "In your basket are " + QString().setNum(items) + " items.";
    
    // better so:
    // QString s = tr("In your basket are %1 items.").arg(items);
    

    And if you think that cannot be topped, here is my second example:

    int ch = 42;
    QString cmd = "ch";
    if (ch < 10) cmd += "0";
    cmd += QString().setNum(ch) + ".foo";
    
    // better so:
    // QString cmd = QString("ch%1.foo").arg(ch, 2, 10, QChar('0'));
    

    Regards

    Qt has to stay free or it will die.

    J 1 Reply Last reply 27 May 2019, 07:33
    6
    • A aha_1980
      27 May 2019, 07:24

      When doing code review, you always see things that hurt your eyes, and it's impossible to get away from them.

      I thought it would be good to collect them as bad example - Feel free to add your's to the hall of shame.

      So here we go, one thing I see again and again:

      int items = 42;
      QString s = "In your basket are " + QString().setNum(items) + " items.";
      
      // better so:
      // QString s = tr("In your basket are %1 items.").arg(items);
      

      And if you think that cannot be topped, here is my second example:

      int ch = 42;
      QString cmd = "ch";
      if (ch < 10) cmd += "0";
      cmd += QString().setNum(ch) + ".foo";
      
      // better so:
      // QString cmd = QString("ch%1.foo").arg(ch, 2, 10, QChar('0'));
      

      Regards

      J Online
      J Online
      J.Hilk
      Moderators
      wrote on 27 May 2019, 07:33 last edited by
      #2

      @aha_1980
      😆
      Maybe also add how it should be done ?


      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.

      A 1 Reply Last reply 27 May 2019, 07:40
      6
      • J J.Hilk
        27 May 2019, 07:33

        @aha_1980
        😆
        Maybe also add how it should be done ?

        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 27 May 2019, 07:40 last edited by
        #3

        @J.Hilk

        Maybe also add how it should be done ?

        Done :)

        Qt has to stay free or it will die.

        1 Reply Last reply
        6
        • F Offline
          F Offline
          fcarney
          wrote on 28 May 2019, 16:54 last edited by fcarney
          #4

          This one I have found amusing:

          QString s = tr("Some text: " + variable + " some more text" );
          

          Or this:

          QString s = tr("Some text: ") + variable + tr(" some more text" );
          

          C++ is a perfectly valid school of magic.

          A 1 Reply Last reply 28 May 2019, 19:08
          3
          • F fcarney
            28 May 2019, 16:54

            This one I have found amusing:

            QString s = tr("Some text: " + variable + " some more text" );
            

            Or this:

            QString s = tr("Some text: ") + variable + tr(" some more text" );
            
            A Offline
            A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 28 May 2019, 19:08 last edited by
            #5

            @fcarney nice one! that makes translations rather pointless.

            Qt has to stay free or it will die.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 7 Jun 2019, 11:32 last edited by
              #6

              I just saw the following again:

              auto *p = new ...;
              // ...
              if (p != NULL) {
                delete p;
                p = NULL;
              }
              

              which can be shortened to:

              delete p;
              p = NULL; // p = nullptr in C++11 and upwards
              

              because delete does the check anyway.

              Qt has to stay free or it will die.

              J F 2 Replies Last reply 7 Jun 2019, 11:36
              4
              • A aha_1980
                7 Jun 2019, 11:32

                I just saw the following again:

                auto *p = new ...;
                // ...
                if (p != NULL) {
                  delete p;
                  p = NULL;
                }
                

                which can be shortened to:

                delete p;
                p = NULL; // p = nullptr in C++11 and upwards
                

                because delete does the check anyway.

                J Online
                J Online
                J.Hilk
                Moderators
                wrote on 7 Jun 2019, 11:36 last edited by
                #7

                @aha_1980
                well, I would give the creator here the benefit of the doubt and they that's refactored code and used to be p->deleteLater() :-)


                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.

                A 1 Reply Last reply 7 Jun 2019, 11:53
                2
                • J J.Hilk
                  7 Jun 2019, 11:36

                  @aha_1980
                  well, I would give the creator here the benefit of the doubt and they that's refactored code and used to be p->deleteLater() :-)

                  A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 7 Jun 2019, 11:53 last edited by
                  #8

                  Hi @J.Hilk,

                  Yeah, but in that case it was pure C++ without Qt. Otherwise you would be right.

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  0
                  • A aha_1980
                    7 Jun 2019, 11:32

                    I just saw the following again:

                    auto *p = new ...;
                    // ...
                    if (p != NULL) {
                      delete p;
                      p = NULL;
                    }
                    

                    which can be shortened to:

                    delete p;
                    p = NULL; // p = nullptr in C++11 and upwards
                    

                    because delete does the check anyway.

                    F Offline
                    F Offline
                    fcarney
                    wrote on 7 Jun 2019, 16:05 last edited by
                    #9

                    @aha_1980 said in Recurring C++ and Qt anti-patterns:

                    because delete does the check anyway

                    Wait, what? Since when does C++ check if the pointer is nullptr before delete? I didn't know this was a thing now.

                    Also, is deleteLater not a way to delete Qt objects? Its morning and I haven't had my coffee.

                    C++ is a perfectly valid school of magic.

                    F 1 Reply Last reply 7 Jun 2019, 16:09
                    0
                    • F fcarney
                      7 Jun 2019, 16:05

                      @aha_1980 said in Recurring C++ and Qt anti-patterns:

                      because delete does the check anyway

                      Wait, what? Since when does C++ check if the pointer is nullptr before delete? I didn't know this was a thing now.

                      Also, is deleteLater not a way to delete Qt objects? Its morning and I haven't had my coffee.

                      F Offline
                      F Offline
                      fcarney
                      wrote on 7 Jun 2019, 16:09 last edited by
                      #10

                      @fcarney said in Recurring C++ and Qt anti-patterns:

                      Wait, what? Since when does C++ check if the pointer is nullptr before delete?

                      Wow, since 2003? Lol, keep this thread going! I am learning a lot.

                      C++ is a perfectly valid school of magic.

                      F 1 Reply Last reply 7 Jun 2019, 16:11
                      1
                      • F fcarney
                        7 Jun 2019, 16:09

                        @fcarney said in Recurring C++ and Qt anti-patterns:

                        Wait, what? Since when does C++ check if the pointer is nullptr before delete?

                        Wow, since 2003? Lol, keep this thread going! I am learning a lot.

                        F Offline
                        F Offline
                        fcarney
                        wrote on 7 Jun 2019, 16:11 last edited by fcarney 6 Jul 2019, 16:13
                        #11

                        Okay, I am just confusing myself. If you delete a pointer you must immediately set it to null. Otherwise you risk double delete, which is bad. But its okay to delete something set to null. Got it.

                        Edit:
                        Why doesn't delete set the pointer to null then? That seems like it may be an antipattern in and of itself.

                        C++ is a perfectly valid school of magic.

                        A Christian EhrlicherC 2 Replies Last reply 7 Jun 2019, 16:17
                        1
                        • F fcarney
                          7 Jun 2019, 16:11

                          Okay, I am just confusing myself. If you delete a pointer you must immediately set it to null. Otherwise you risk double delete, which is bad. But its okay to delete something set to null. Got it.

                          Edit:
                          Why doesn't delete set the pointer to null then? That seems like it may be an antipattern in and of itself.

                          A Offline
                          A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on 7 Jun 2019, 16:17 last edited by
                          #12

                          @fcarney said in Recurring C++ and Qt anti-patterns:

                          Why doesn't delete set the pointer to null then? That seems like it may be an antipattern in and of itself.

                          I have indeed asked that myself. If someone has the correct answer for that, I'm all ears.

                          Qt has to stay free or it will die.

                          F ODБOïO S 3 Replies Last reply 7 Jun 2019, 16:21
                          0
                          • A aha_1980
                            7 Jun 2019, 16:17

                            @fcarney said in Recurring C++ and Qt anti-patterns:

                            Why doesn't delete set the pointer to null then? That seems like it may be an antipattern in and of itself.

                            I have indeed asked that myself. If someone has the correct answer for that, I'm all ears.

                            F Offline
                            F Offline
                            fcarney
                            wrote on 7 Jun 2019, 16:21 last edited by
                            #13

                            @aha_1980
                            Apparently the standard allows for it:
                            https://stackoverflow.com/questions/704466/why-doesnt-delete-set-the-pointer-to-null

                            The creator himself wonders why it isn't so. Its like C++ is this beautiful, amazing, and now, WILD animal roaming free in cyberspace... Yeah, maybe the analogy isn't all that great, but it does conjure up a cool picture.

                            C++ is a perfectly valid school of magic.

                            Kent-DorfmanK 1 Reply Last reply 5 Aug 2019, 04:25
                            1
                            • A aha_1980
                              7 Jun 2019, 16:17

                              @fcarney said in Recurring C++ and Qt anti-patterns:

                              Why doesn't delete set the pointer to null then? That seems like it may be an antipattern in and of itself.

                              I have indeed asked that myself. If someone has the correct answer for that, I'm all ears.

                              ODБOïO Offline
                              ODБOïO Offline
                              ODБOï
                              wrote on 7 Jun 2019, 16:39 last edited by
                              #14

                              @fcarney said in Recurring C++ and Qt anti-patterns:

                              Why doesn't delete set the pointer to null

                              likely because it would bring more problems than solutions

                              A 1 Reply Last reply 7 Jun 2019, 16:54
                              0
                              • ODБOïO ODБOï
                                7 Jun 2019, 16:39

                                @fcarney said in Recurring C++ and Qt anti-patterns:

                                Why doesn't delete set the pointer to null

                                likely because it would bring more problems than solutions

                                A Offline
                                A Offline
                                aha_1980
                                Lifetime Qt Champion
                                wrote on 7 Jun 2019, 16:54 last edited by
                                #15

                                @LeLev

                                likely because it would bring more problems than solutions

                                That would mean, that this pointer shows to an invalid memory region after the delete. Can you think of an example where you still want to use that pointer afterwards? (That is a real question - because for now I have no idea).

                                Qt has to stay free or it will die.

                                ODБOïO 1 Reply Last reply 7 Jun 2019, 17:28
                                0
                                • F Offline
                                  F Offline
                                  fcarney
                                  wrote on 7 Jun 2019, 17:02 last edited by
                                  #16

                                  @LeLev said in Recurring C++ and Qt anti-patterns:

                                  likely because it would bring more problems than solutions

                                  I could see a case where a program is deleting thousands of pointers and there might actually be overhead in a mov instruction for each delete. I have no idea if this overhead would be significant over the delete operation, but it would still be overhead. It would not be that hard to test such a scenario. I should try it!

                                  C++ is a perfectly valid school of magic.

                                  F 1 Reply Last reply 7 Jun 2019, 17:34
                                  0
                                  • A aha_1980
                                    7 Jun 2019, 16:54

                                    @LeLev

                                    likely because it would bring more problems than solutions

                                    That would mean, that this pointer shows to an invalid memory region after the delete. Can you think of an example where you still want to use that pointer afterwards? (That is a real question - because for now I have no idea).

                                    ODБOïO Offline
                                    ODБOïO Offline
                                    ODБOï
                                    wrote on 7 Jun 2019, 17:28 last edited by
                                    #17

                                    @aha_1980 said in Recurring C++ and Qt anti-patterns:

                                    Can you think of an example where you still want to use that pointer afterwards?

                                    not a real world application

                                    1 Reply Last reply
                                    0
                                    • F fcarney
                                      7 Jun 2019, 17:02

                                      @LeLev said in Recurring C++ and Qt anti-patterns:

                                      likely because it would bring more problems than solutions

                                      I could see a case where a program is deleting thousands of pointers and there might actually be overhead in a mov instruction for each delete. I have no idea if this overhead would be significant over the delete operation, but it would still be overhead. It would not be that hard to test such a scenario. I should try it!

                                      F Offline
                                      F Offline
                                      fcarney
                                      wrote on 7 Jun 2019, 17:34 last edited by
                                      #18

                                      @fcarney said in Recurring C++ and Qt anti-patterns:

                                      deleting thousands of pointers and there might actually be overhead

                                      I cannot actually tell if the overhead in this code is the indexing of the array, or if the movement of data is significant. I tried doing a dummy no op index, but I am guessing it is being optimized out:

                                      #include <QCoreApplication>
                                      #include <QElapsedTimer>
                                      #include <QDebug>
                                      
                                      #define MEM_SEG_LEN 8
                                      #define MEM_SEGS 100000000
                                      
                                      char** createMemoryList(){
                                          char** list = new char*[MEM_SEGS];
                                          for(int index=0; index<MEM_SEGS; index++){
                                              list[index]=new char[MEM_SEG_LEN];
                                          }
                                      
                                          return list;
                                      }
                                      
                                      void deleteMemoryList(char** list){
                                          for(int index=0; index<MEM_SEGS; index++){
                                              delete list[index];
                                              list[index]; // can you force an index to occur?
                                          }
                                          delete list;
                                      }
                                      
                                      void deleteMemoryListNull(char** list){
                                          for(int index=0; index<MEM_SEGS; index++){
                                              delete list[index];
                                              list[index] = nullptr;
                                          }
                                          delete list;
                                          list = nullptr;
                                      }
                                      
                                      int main(int argc, char *argv[])
                                      {
                                          QCoreApplication a(argc, argv);
                                      
                                          QElapsedTimer timer1;
                                      
                                          char** list1 = createMemoryList();
                                          timer1.start();
                                          deleteMemoryList(list1);
                                          qInfo() << timer1.elapsed();
                                      
                                          QElapsedTimer timer2;
                                      
                                          char** list2 = createMemoryList();
                                          timer2.start();
                                          deleteMemoryListNull(list2);
                                          qInfo() << timer2.elapsed();
                                      
                                          return a.exec();
                                      }
                                      

                                      I get the following output:

                                      813
                                      1301
                                      

                                      I doubt that is the overhead of the movement of null into the pointer. My guess is the the index overhead is in there too.

                                      C++ is a perfectly valid school of magic.

                                      1 Reply Last reply
                                      0
                                      • F Offline
                                        F Offline
                                        fcarney
                                        wrote on 7 Jun 2019, 18:03 last edited by fcarney 6 Jul 2019, 19:27
                                        #19

                                        I eliminated the extra index (probably compiler already did this before):

                                        void deleteMemoryList(char** list){
                                            for(int index=0; index<MEM_SEGS; index++){
                                                char* tmp = list[index];
                                                delete tmp;
                                            }
                                            delete list;
                                        }
                                        
                                        void deleteMemoryListNull(char** list){
                                            for(int index=0; index<MEM_SEGS; index++){
                                                char* tmp = list[index];
                                                delete tmp;
                                                tmp = nullptr;
                                            }
                                            delete list;
                                            list = nullptr;
                                        }
                                        

                                        Results:

                                        877
                                        1369
                                        

                                        Edit: Real world usage? I really highly doubt it. That is a LOT of iterations of delete. So I would say the extra cycles are negligible.

                                        Edit2:
                                        Pointer math:

                                        void deleteMemoryList(char** list){
                                            for(int index=0; index<MEM_SEGS; index++){
                                                char** tmp = &(list[index]);
                                                delete *tmp;
                                            }
                                            delete list;
                                        }
                                        
                                        void deleteMemoryListNull(char** list){
                                            for(int index=0; index<MEM_SEGS; index++){
                                                char** tmp = &(list[index]);
                                                delete *tmp;
                                                *tmp = nullptr;
                                            }
                                            delete list;
                                            list = nullptr;
                                        }
                                        

                                        Results:

                                        853
                                        1307
                                        

                                        Sometimes apples and apples is hard.

                                        C++ is a perfectly valid school of magic.

                                        JonBJ 1 Reply Last reply 7 Jun 2019, 19:00
                                        0
                                        • F fcarney
                                          7 Jun 2019, 18:03

                                          I eliminated the extra index (probably compiler already did this before):

                                          void deleteMemoryList(char** list){
                                              for(int index=0; index<MEM_SEGS; index++){
                                                  char* tmp = list[index];
                                                  delete tmp;
                                              }
                                              delete list;
                                          }
                                          
                                          void deleteMemoryListNull(char** list){
                                              for(int index=0; index<MEM_SEGS; index++){
                                                  char* tmp = list[index];
                                                  delete tmp;
                                                  tmp = nullptr;
                                              }
                                              delete list;
                                              list = nullptr;
                                          }
                                          

                                          Results:

                                          877
                                          1369
                                          

                                          Edit: Real world usage? I really highly doubt it. That is a LOT of iterations of delete. So I would say the extra cycles are negligible.

                                          Edit2:
                                          Pointer math:

                                          void deleteMemoryList(char** list){
                                              for(int index=0; index<MEM_SEGS; index++){
                                                  char** tmp = &(list[index]);
                                                  delete *tmp;
                                              }
                                              delete list;
                                          }
                                          
                                          void deleteMemoryListNull(char** list){
                                              for(int index=0; index<MEM_SEGS; index++){
                                                  char** tmp = &(list[index]);
                                                  delete *tmp;
                                                  *tmp = nullptr;
                                              }
                                              delete list;
                                              list = nullptr;
                                          }
                                          

                                          Results:

                                          853
                                          1307
                                          

                                          Sometimes apples and apples is hard.

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on 7 Jun 2019, 19:00 last edited by JonB 6 Jul 2019, 19:02
                                          #20

                                          @fcarney
                                          Since this is the lounge... Surprised by your findings (in earlier examples). What exactly is the difference in the assembly between the two versions? What is being generated for your tmp = nullptr;? (Not the later *tmp = nullptr;, that's different.)

                                          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