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
Forum Updated to NodeBB v4.3 + New Features

Recurring C++ and Qt anti-patterns

Scheduled Pinned Locked Moved The Lounge
126 Posts 17 Posters 60.2k Views 10 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.
  • fcarneyF fcarney

    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.

    aha_1980A Offline
    aha_1980A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on 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.

    fcarneyF ODБOïO S 3 Replies Last reply
    0
    • aha_1980A aha_1980

      @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.

      fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on 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
      1
      • aha_1980A aha_1980

        @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 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

        aha_1980A 1 Reply Last reply
        0
        • ODБOïO ODБOï

          @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

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 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
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on 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.

            fcarneyF 1 Reply Last reply
            0
            • aha_1980A aha_1980

              @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 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
              • fcarneyF fcarney

                @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!

                fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on 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
                • fcarneyF Offline
                  fcarneyF Offline
                  fcarney
                  wrote on last edited by fcarney
                  #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
                  0
                  • fcarneyF fcarney

                    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 last edited by JonB
                    #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
                    • fcarneyF Offline
                      fcarneyF Offline
                      fcarney
                      wrote on last edited by
                      #21

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

                      tmp = nullptr;

                      I changed it to not update a local variable. char* tmp is local, so setting it to null is just setting a local variable to null. So it was setting the wrong area of memory to null. That is why I took the address of where that pointer is stored.

                      The assembler for tmp = nullptr in previous incarnation:

                      movq   $0x0,-0x8(%rbp)
                      

                      The assembler for *tmp = nullptr in latest incarnation:

                      mov    -0x8(%rbp),%rax
                      movq   $0x0,(%rax)
                      

                      But you are right, the tmp = nullptr is more representative.
                      The timing is not much different.

                      C++ is a perfectly valid school of magic.

                      JonBJ 1 Reply Last reply
                      0
                      • fcarneyF Offline
                        fcarneyF Offline
                        fcarney
                        wrote on last edited by fcarney
                        #22

                        Okay, I think I am done, but here is my last incarnation:

                        #include <QCoreApplication>
                        #include <QElapsedTimer>
                        #include <QDebug>
                        
                        #define MEM_SEG_LEN 8
                        #define MEM_SEGS 100000000 // 846 1315 // assignment
                        #define MEM_SEGS 100000000 // 885 1349 // correct assignment
                        //#define MEM_SEGS 100
                        
                        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++){
                                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;
                        //        char* tmp = (list[index]);
                        //        delete tmp;
                        //        tmp = nullptr;
                            }
                            delete list;
                            list = nullptr;
                        }
                        
                        int main(int argc, char *argv[])
                        {
                            QCoreApplication a(argc, argv);
                        
                            double time1, time2;
                        
                            qInfo() << QString(R"(Starting memory test 1 of not setting pointer to nullptr: %1 cycles)").arg(MEM_SEGS);
                        
                            QElapsedTimer timer1;
                        
                            char** list1 = createMemoryList();
                            timer1.start();
                            deleteMemoryList(list1);
                            time1 = timer1.elapsed();
                            qInfo() << time1/1000.0 << QString().number((time1/1000.0)/double(MEM_SEGS),10,12);
                        
                            qInfo() << QString(R"(Starting memory test 2 of setting pointer to nullptr: %1 cycles)").arg(MEM_SEGS);
                        
                            QElapsedTimer timer2;
                        
                            char** list2 = createMemoryList();
                            timer2.start();
                            deleteMemoryListNull(list2);
                            time2 = timer2.elapsed();
                            qInfo() << time2/1000.0 << QString().number((time2/1000.0)/double(MEM_SEGS),10,12);
                        
                            qInfo() << "Difference:" << QString().number((time2/1000.0)/double(MEM_SEGS)-(time1/1000.0)/double(MEM_SEGS),10,12);
                        
                            return a.exec();
                        }
                        

                        So, 5 nanoseconds difference for a delete operation of dereffed pointer assignment:

                        "Starting memory test 1 of not setting pointer to nullptr: 100000000 cycles"
                        0.848 "0.000000008480"
                        "Starting memory test 2 of setting pointer to nullptr: 100000000 cycles"
                        1.321 "0.000000013210"
                        Difference: "0.000000004730"
                        

                        Edit:
                        Math was wrong.

                        C++ is a perfectly valid school of magic.

                        1 Reply Last reply
                        0
                        • fcarneyF fcarney

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

                          tmp = nullptr;

                          I changed it to not update a local variable. char* tmp is local, so setting it to null is just setting a local variable to null. So it was setting the wrong area of memory to null. That is why I took the address of where that pointer is stored.

                          The assembler for tmp = nullptr in previous incarnation:

                          movq   $0x0,-0x8(%rbp)
                          

                          The assembler for *tmp = nullptr in latest incarnation:

                          mov    -0x8(%rbp),%rax
                          movq   $0x0,(%rax)
                          

                          But you are right, the tmp = nullptr is more representative.
                          The timing is not much different.

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

                          @fcarney
                          You're talking about a single movq. Just how big is MEM_SEGS? Your timings, are they in milliseconds?? And I assume list is filled with zeroes? I'm talking about your earlier barebones example, where your timings were
                          877
                          1369

                          Oh now I see more code in other examples. If your list contained 10 million news and you are deleteing them, common-sense should tell you the cost of whether or not you set one local variable to nullptr with just a mov instruction must be negligible, compared to whatever is involved in freeing memory, no?

                          1 Reply Last reply
                          0
                          • fcarneyF Offline
                            fcarneyF Offline
                            fcarney
                            wrote on last edited by
                            #24

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

                            one local variable to nullptr with just a mov instruction must be negligible, compared to whatever is involved in freeing memory, no?

                            Correct. My math was wrong. It is 5 nanoseconds. At least for dereffed pointer, but timing was nearly the same for local variable as well.

                            C++ is a perfectly valid school of magic.

                            JonBJ 1 Reply Last reply
                            3
                            • fcarneyF fcarney

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

                              one local variable to nullptr with just a mov instruction must be negligible, compared to whatever is involved in freeing memory, no?

                              Correct. My math was wrong. It is 5 nanoseconds. At least for dereffed pointer, but timing was nearly the same for local variable as well.

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

                              @fcarney
                              Ahhh, that would make much more sense! :)
                              A nanosecond doesn't sound too long, I don't think I could get much done in it could I?

                              1 Reply Last reply
                              0
                              • aha_1980A Offline
                                aha_1980A Offline
                                aha_1980
                                Lifetime Qt Champion
                                wrote on last edited by
                                #26

                                One more example for the hall of shame:

                                QByteArray ba = "Hello World";
                                QString s = QString::fromStdString(ba.toStdString());
                                

                                We have two problems here:

                                1. The conversion to and from std::string is unneeded
                                2. This only works for ASCII characters. QString::fromUtf8(ba); would most often be the correct choice, sometimes also QString::fromLocal8Bit(ba);

                                Qt has to stay free or it will die.

                                1 Reply Last reply
                                4
                                • aha_1980A Offline
                                  aha_1980A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #27

                                  One more note to string processing:

                                  QString empty = ""; is not the correct way to create an empty string, that is QString empty;

                                  To clear an non-empty QString data, use data.clear() instead data = "";.

                                  The same applies to QByteArrays.

                                  Qt has to stay free or it will die.

                                  1 Reply Last reply
                                  4
                                  • fcarneyF Offline
                                    fcarneyF Offline
                                    fcarney
                                    wrote on last edited by
                                    #28

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

                                    QString empty = ""; is not the correct way to create an empty string, that is QString empty;
                                    To clear an non-empty QString data, use data.clear() instead data = "";

                                    Interesting... I did a couple of tests to see if it behaves the same way for comparisons. The only difference I can find between clear and setting to "" is the isNull test:

                                        qInfo() << "string test";
                                        QString empty;
                                        QString str = "";
                                        qInfo() << bool(empty == str);
                                        qInfo() << str.isNull();
                                        str.clear();
                                        qInfo() << bool(empty == str);
                                        qInfo() << str.isNull();
                                    

                                    output:

                                    string test
                                    true
                                    false
                                    true
                                    true
                                    

                                    If the string was set to "" is returns false for isNull. I have never had the occasion to use this test and am not sure what it is for. I guess it would be important if you want to determine if an empty string was assigned to the variable versus nothing being assigned.

                                    C++ is a perfectly valid school of magic.

                                    aha_1980A 1 Reply Last reply
                                    0
                                    • fcarneyF fcarney

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

                                      QString empty = ""; is not the correct way to create an empty string, that is QString empty;
                                      To clear an non-empty QString data, use data.clear() instead data = "";

                                      Interesting... I did a couple of tests to see if it behaves the same way for comparisons. The only difference I can find between clear and setting to "" is the isNull test:

                                          qInfo() << "string test";
                                          QString empty;
                                          QString str = "";
                                          qInfo() << bool(empty == str);
                                          qInfo() << str.isNull();
                                          str.clear();
                                          qInfo() << bool(empty == str);
                                          qInfo() << str.isNull();
                                      

                                      output:

                                      string test
                                      true
                                      false
                                      true
                                      true
                                      

                                      If the string was set to "" is returns false for isNull. I have never had the occasion to use this test and am not sure what it is for. I guess it would be important if you want to determine if an empty string was assigned to the variable versus nothing being assigned.

                                      aha_1980A Offline
                                      aha_1980A Offline
                                      aha_1980
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #29

                                      @fcarney I just think the isNull() method should be deprecated. Even the documentation states:

                                      Qt makes a distinction between null strings and empty strings for historical reasons. For most applications, what matters is whether or not a string contains any data, and this can be determined using the isEmpty() function.

                                      Qt has to stay free or it will die.

                                      1 Reply Last reply
                                      1
                                      • fcarneyF fcarney

                                        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.

                                        Christian EhrlicherC Online
                                        Christian EhrlicherC Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on last edited by Christian Ehrlicher
                                        #30

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

                                        Why doesn't delete set the pointer to null then?

                                        Because the operator delete is defined to take a pointer, not a reference to a pointer.
                                        https://en.cppreference.com/w/cpp/memory/new/operator_delete

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

                                        1 Reply Last reply
                                        3
                                        • fcarneyF Offline
                                          fcarneyF Offline
                                          fcarney
                                          wrote on last edited by fcarney
                                          #31

                                          Welcome to another edition of Is this an Anti-Pattern?

                                          Harmless looking pattern 1:

                                          std::vector<int64_t> list = {9,9,9,9,9,9,9,9,9,9};
                                          int64_t sum = std::accumulate(list.begin(), list.end(), 0);
                                          

                                          Harmless looking pattern 2:

                                          std::vector<int64_t> list = {9,9,9,9,9,9,9,9,9,9};
                                          int64_t prod = std::accumulate(list.begin(), list.end(), 1, std::multiplies<int64_t>());
                                          

                                          Lack of understanding pattern 3:

                                          std::set<int> list = {9,8,7,6};
                                          int last = *(list.end()--);
                                          

                                          Lack of understanding pattern 4:

                                          std:vector<int> list = {9,8,7,6};
                                          int last = *(list.end()--);
                                          

                                          I still don't get why this cannot be done for sets and vectors...
                                          Okay, was using post decrement instead of a prefix decrement. DOH!

                                          int last = *(--list.end());
                                          

                                          Logic fudgery pattern 5 (this is more an optical illusion, it was to me anyway):

                                          bool is_leap_year(int year){
                                              return (year % 4 == 0) && (year % 100 == 0) ? (year % 400 == 0) : true;
                                          }
                                          

                                          C++ is a perfectly valid school of magic.

                                          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