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. QList<QVector< . >>: how to reserve
Forum Updated to NodeBB v4.3 + New Features

QList<QVector< . >>: how to reserve

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 905 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.
  • JonBJ JonB

    @Davide87 said in QList<QVector< . >>: how to reserve:

    or should I myList->reserve(N*M);?

    No, the QList and QVector are quite separate from one another, not related. Any reservations must be done on each one separately, and for the elements of the list which are vectors in each vector individually, as your code does.

     myList->at(i)->reserve(M);
    

    Not sure you will be able to do this (or have you already done it and it compiles)? const T &QList::at(int i) const returns a const reference to the (QVector) element, so you won't be able to call QList::reserve() on it? Create a temporary for the new QVector<float>(), call reserve() on that, then add it to your QList.

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

    @JonB said in QList<QVector< . >>: how to reserve:

    Create a temporary for the new QVector<float>(), call reserve() on that, then add it to your QList.

    or use operator[] :)

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

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @JonB said in QList<QVector< . >>: how to reserve:

      Create a temporary for the new QVector<float>(), call reserve() on that, then add it to your QList.

      or use operator[] :)

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

      @Christian-Ehrlicher
      I could have suggested that, but to me I should not have to think about how I am addressing the element in the list ([] vs at()) when all I want to do is set reserve() on the QVector I am adding. I also have a feeling it will be 1 nanosecond slower to do it via []/on an element I have already put into the list than if I do it do it beforehand (though i amy be wrong on this)... ;-)

      If you can afford to actually create all the elements (value 0.0) initially and overwrite them instead of reserve()ing (because it will be fixed size), Qt6 at least seems to have QList::QList(qsizetype size) constructor which you can use.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Davide87
        wrote on last edited by
        #6

        Thank you guys.

        @Christian-Ehrlicher
        In my application N is in the order of few tens. The subvectors may be instead hundreds or thousands of elements, depending on the configuration of the hardware that sends me the data to store/display.
        I thought I may have to reserve the list depending on the actual maximum number of bytes it is going to contain.
        The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.

        @JonB
        myList->at(i)->reserve(M); compiles without issues.
        I'm actually having some memory issues in my application, but they are probably related to other parts of may code. That's why I opened this topic.

        JonBJ 1 Reply Last reply
        0
        • D Davide87

          Thank you guys.

          @Christian-Ehrlicher
          In my application N is in the order of few tens. The subvectors may be instead hundreds or thousands of elements, depending on the configuration of the hardware that sends me the data to store/display.
          I thought I may have to reserve the list depending on the actual maximum number of bytes it is going to contain.
          The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.

          @JonB
          myList->at(i)->reserve(M); compiles without issues.
          I'm actually having some memory issues in my application, but they are probably related to other parts of may code. That's why I opened this topic.

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

          @Davide87 said in QList<QVector< . >>: how to reserve:

          @JonB
          myList->at(i)->reserve(M); compiles without issues.

          @Christian-Ehrlicher could you explain, am I having a mind block? Given it's const T &QList::at(int i) const, doesn't the const T & prevent me from calling QList::reserve() on it?

          @Davide87
          I still suspect reserve() won't make that much difference, Qt's list/vector allocation will be reasonable even without it. You would have to test.

          The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.

          I don't think reserve() will have any effect on the maximum size of the list/vector. That will happen dynamically whether you reserve or not. It's only to do with efficient use of memory during re-allocations as list grows.

          Christian EhrlicherC D 2 Replies Last reply
          0
          • JonBJ JonB

            @Davide87 said in QList<QVector< . >>: how to reserve:

            @JonB
            myList->at(i)->reserve(M); compiles without issues.

            @Christian-Ehrlicher could you explain, am I having a mind block? Given it's const T &QList::at(int i) const, doesn't the const T & prevent me from calling QList::reserve() on it?

            @Davide87
            I still suspect reserve() won't make that much difference, Qt's list/vector allocation will be reasonable even without it. You would have to test.

            The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.

            I don't think reserve() will have any effect on the maximum size of the list/vector. That will happen dynamically whether you reserve or not. It's only to do with efficient use of memory during re-allocations as list grows.

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

            @JonB said in QList<QVector< . >>: how to reserve:

            Given it's const T &QList::at(int i) const, doesn't the const T & prevent me from calling QList::reserve() on it?

            Because he is working with heap-allocated QList/QVector instances for unknown reaons / he wants to debug memory leaks later on because he forgets to delete them afterwards...

            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
            1
            • JonBJ JonB

              @Davide87 said in QList<QVector< . >>: how to reserve:

              @JonB
              myList->at(i)->reserve(M); compiles without issues.

              @Christian-Ehrlicher could you explain, am I having a mind block? Given it's const T &QList::at(int i) const, doesn't the const T & prevent me from calling QList::reserve() on it?

              @Davide87
              I still suspect reserve() won't make that much difference, Qt's list/vector allocation will be reasonable even without it. You would have to test.

              The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.

              I don't think reserve() will have any effect on the maximum size of the list/vector. That will happen dynamically whether you reserve or not. It's only to do with efficient use of memory during re-allocations as list grows.

              D Offline
              D Offline
              Davide87
              wrote on last edited by
              #9

              @JonB said in QList<QVector< . >>: how to reserve:

              @Davide87 said in QList<QVector< . >>: how to reserve:

              @JonB
              myList->at(i)->reserve(M); compiles without issues.

              @Christian-Ehrlicher could you explain, am I having a mind block? Given it's const T &QList::at(int i) const, doesn't the const T & prevent me from calling QList::reserve() on it?

              It may be because myList is a list of pointers. QList::at(int i) returns a pointer in this case. Therefore ->reserve() works in this case. Maybe.

              @Davide87
              I still suspect reserve() won't make that much difference, Qt's list/vector allocation will be reasonable even without it. You would have to test.

              The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.

              I don't think reserve() will have any effect on the maximum size of the list/vector. That will happen dynamically whether you reserve or not. It's only to do with efficient use of memory during re-allocations as list grows.

              Ok, thank you. I will test that.

              JonBJ 1 Reply Last reply
              0
              • D Davide87

                @JonB said in QList<QVector< . >>: how to reserve:

                @Davide87 said in QList<QVector< . >>: how to reserve:

                @JonB
                myList->at(i)->reserve(M); compiles without issues.

                @Christian-Ehrlicher could you explain, am I having a mind block? Given it's const T &QList::at(int i) const, doesn't the const T & prevent me from calling QList::reserve() on it?

                It may be because myList is a list of pointers. QList::at(int i) returns a pointer in this case. Therefore ->reserve() works in this case. Maybe.

                @Davide87
                I still suspect reserve() won't make that much difference, Qt's list/vector allocation will be reasonable even without it. You would have to test.

                The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.

                I don't think reserve() will have any effect on the maximum size of the list/vector. That will happen dynamically whether you reserve or not. It's only to do with efficient use of memory during re-allocations as list grows.

                Ok, thank you. I will test that.

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

                @Davide87 said in QList<QVector< . >>: how to reserve:

                It may be because myList is a list of pointers. QList::at(int i) returns a pointer in this case. Therefore ->reserve() works in this case. Maybe.

                Ah, yes indeed, I did not notice that. Only the pointer is const then, it does not make *const_pointer be const :)

                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  Again: don't use pointers here - they are completely unneeded and will get you in trouble later on when you hunt for memory leaks

                  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
                  1
                  • D Offline
                    D Offline
                    Davide87
                    wrote on last edited by
                    #12

                    Ok Christian. Thank you.

                    1 Reply Last reply
                    0
                    • D Davide87 has marked this topic as solved on
                    • JonBJ JonB

                      @Davide87 said in QList<QVector< . >>: how to reserve:

                      or should I myList->reserve(N*M);?

                      No, the QList and QVector are quite separate from one another, not related. Any reservations must be done on each one separately, and for the elements of the list which are vectors in each vector individually, as your code does.

                       myList->at(i)->reserve(M);
                      

                      Not sure you will be able to do this (or have you already done it and it compiles)? const T &QList::at(int i) const returns a const reference to the (QVector) element, so you won't be able to call QList::reserve() on it? Create a temporary for the new QVector<float>(), call reserve() on that, then add it to your QList.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #13

                      @JonB said in QList<QVector< . >>: how to reserve:

                      No, the QList and QVector are quite separate from one another, not related

                      alt text

                      Not in Qt6

                      https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qvector.h

                      you're technically correct in relation to the OP post. And technically correct is the best kind of correct


                      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.

                      JonBJ 1 Reply Last reply
                      3
                      • J.HilkJ J.Hilk

                        @JonB said in QList<QVector< . >>: how to reserve:

                        No, the QList and QVector are quite separate from one another, not related

                        alt text

                        Not in Qt6

                        https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qvector.h

                        you're technically correct in relation to the OP post. And technically correct is the best kind of correct

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

                        @J-Hilk
                        I did not say QList and QVector are not related to one another! I said THE QList and THE QVector in OP's

                        QList<QVector<float>*> *myList;
                        

                        declaration are unrelated to each other (Qt4, 5, 6 or anything else). The OP asked whether

                        or should I myList->reserve(N*M);?

                        where he thinks a list/vector of lists/vectors are "one blob" which can be reserved like that....

                        J.HilkJ 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @J-Hilk
                          I did not say QList and QVector are not related to one another! I said THE QList and THE QVector in OP's

                          QList<QVector<float>*> *myList;
                          

                          declaration are unrelated to each other (Qt4, 5, 6 or anything else). The OP asked whether

                          or should I myList->reserve(N*M);?

                          where he thinks a list/vector of lists/vectors are "one blob" which can be reserved like that....

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #15

                          @JonB

                          @J-Hilk said in QList<QVector< . >>: how to reserve:

                          you're technically correct in relation to the OP post. And technically correct is the best kind of correct


                          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

                          • Login

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