Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why is the capacity of qvector smaller than that of std:: vector?
Qt 6.12 LTS is out! See what's new in the release blog

Why is the capacity of qvector smaller than that of std:: vector?

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 7 Posters 4.2k Views 1 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.
  • Christian EhrlicherC Christian Ehrlicher

    This post is deleted!

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

    @Christian-Ehrlicher said in Why is the capacity of qvector smaller than that of std:: vector?:

    because in Qt5 'int' is used as index

    220.011.222 - fits perfectly into an int.

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

    Christian EhrlicherC 1 Reply Last reply
    0
    • jsulmJ jsulm

      @Christian-Ehrlicher said in Why is the capacity of qvector smaller than that of std:: vector?:

      because in Qt5 'int' is used as index

      220.011.222 - fits perfectly into an int.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @jsulm oh, correct 🙂

      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
      0
      • jsulmJ jsulm

        @John-Van said in Why is the capacity of qvector smaller than that of std:: vector?:

        QVector<char>ware0 (220011222);

        Works just fine for me. Where exactly do you see this error? I guess in QtCreator editor? Or is it compiler error?
        Also, what is your platform, OS and Qt version?

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

        @jsulm
        Also both lines fine for me under Linux, 64-bit, gcc 11.4, Qt 5.15, Creator 6.0.2.
        I can multiply both sizes by x10 and also fine.
        If I multiply both by x100 I get "overflow conversion from long int to int" error from compiler (not Creator), which is what I would expect.

        1 Reply Last reply
        0
        • J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #8

          QVector<double>ware1 (275111222)

          actually crashes during runtime MSVC 2019 Qt 5.12.9

          with 5.15.8 I get
          e3c2e53e-00c8-4f84-be11-803a46c7b0aa-image.png

          which is even more unusual.


          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.

          Christian EhrlicherC JonBJ 2 Replies Last reply
          0
          • J.HilkJ J.Hilk

            QVector<double>ware1 (275111222)

            actually crashes during runtime MSVC 2019 Qt 5.12.9

            with 5.15.8 I get
            e3c2e53e-00c8-4f84-be11-803a46c7b0aa-image.png

            which is even more unusual.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #9

            @J-Hilk std will crash also as you don't have 800MB of contiguous memory.

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

            J.HilkJ S 2 Replies Last reply
            1
            • J.HilkJ J.Hilk

              QVector<double>ware1 (275111222)

              actually crashes during runtime MSVC 2019 Qt 5.12.9

              with 5.15.8 I get
              e3c2e53e-00c8-4f84-be11-803a46c7b0aa-image.png

              which is even more unusual.

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

              @J-Hilk
              OOI, if you click Abbrechen to debug do you get to see what line abort() is being called from?

              @Christian-Ehrlicher
              Since it's 200 million * double which is 8 bytes, I think it's actually 1.6GB contiguous which is required?
              Still an awful lot, as you said one wonders why someone would want so much (and contiguous)....

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @J-Hilk std will crash also as you don't have 800MB of contiguous memory.

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

                @Christian-Ehrlicher said in Why is the capacity of qvector smaller than that of std:: vector?:

                @J-Hilk std will crash also as you don't have 800MB of contiguous memory.

                I tested that, that actually works with std::vector!

                But you're right, thats 800 mb!!! I did not realise !!


                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.

                Christian EhrlicherC 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @Christian-Ehrlicher said in Why is the capacity of qvector smaller than that of std:: vector?:

                  @J-Hilk std will crash also as you don't have 800MB of contiguous memory.

                  I tested that, that actually works with std::vector!

                  But you're right, thats 800 mb!!! I did not realise !!

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  @J-Hilk said in Why is the capacity of qvector smaller than that of std:: vector?:

                  thats 800 mb!!! I did not realise !!

                  Then it will crash on first access when the memory needs to be allocated.

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

                  J.HilkJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @J-Hilk said in Why is the capacity of qvector smaller than that of std:: vector?:

                    thats 800 mb!!! I did not realise !!

                    Then it will crash on first access when the memory needs to be allocated.

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

                    @Christian-Ehrlicher probably


                    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.

                    JoeCFDJ 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @Christian-Ehrlicher probably

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #14

                      @J-Hilk no crash on Ubuntu 22.04.

                      #include <vector>
                      #include <iostream>
                      
                      int main()
                      {
                          std::vector< double > ware1( 275111222 );
                          for ( auto & value : ware1 ) {
                                 value = 1.0;
                          }
                          
                          std::cout << ware1[ 0 ] << "  " << ware1.size() << std::endl;
                          return 1;
                      }
                      

                      output:
                      1 275111222

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • JoeCFDJ JoeCFD

                        @J-Hilk no crash on Ubuntu 22.04.

                        #include <vector>
                        #include <iostream>
                        
                        int main()
                        {
                            std::vector< double > ware1( 275111222 );
                            for ( auto & value : ware1 ) {
                                   value = 1.0;
                            }
                            
                            std::cout << ware1[ 0 ] << "  " << ware1.size() << std::endl;
                            return 1;
                        }
                        

                        output:
                        1 275111222

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        @JoeCFD said in Why is the capacity of qvector smaller than that of std:: vector?:

                        o crash on Ubuntu 22.04

                        We already discussed the reason... You have enough contiguous memory for this test.

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

                        JoeCFDJ 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @JoeCFD said in Why is the capacity of qvector smaller than that of std:: vector?:

                          o crash on Ubuntu 22.04

                          We already discussed the reason... You have enough contiguous memory for this test.

                          JoeCFDJ Offline
                          JoeCFDJ Offline
                          JoeCFD
                          wrote on last edited by
                          #16

                          @Christian-Ehrlicher
                          Ok. In that case, maybe it is better to add exception handling

                          int main()
                          {
                              try { 
                                  std::vector< double > ware1( 275111222 );
                                  for ( auto & value : ware1 ) {
                                         value = 1.0;
                                  }
                              
                                  std::cout << ware1[ 0 ] << "  " << ware1.size() << std::endl;
                              }
                              catch( std::bad_alloc & ex ) {
                                  std::cerr << "bad allocation caught " << ex.what();
                              }
                              return 1;
                          }
                          
                          JonBJ 1 Reply Last reply
                          0
                          • JoeCFDJ JoeCFD

                            @Christian-Ehrlicher
                            Ok. In that case, maybe it is better to add exception handling

                            int main()
                            {
                                try { 
                                    std::vector< double > ware1( 275111222 );
                                    for ( auto & value : ware1 ) {
                                           value = 1.0;
                                    }
                                
                                    std::cout << ware1[ 0 ] << "  " << ware1.size() << std::endl;
                                }
                                catch( std::bad_alloc & ex ) {
                                    std::cerr << "bad allocation caught " << ex.what();
                                }
                                return 1;
                            }
                            
                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #17

                            @JoeCFD Well, yes, but it doesn't actually get the OP anywhere, does it? :)

                            JoeCFDJ 2 Replies Last reply
                            0
                            • JonBJ JonB

                              @JoeCFD Well, yes, but it doesn't actually get the OP anywhere, does it? :)

                              JoeCFDJ Offline
                              JoeCFDJ Offline
                              JoeCFD
                              wrote on last edited by
                              #18

                              @JonB At least you know quickly where the problem is in a big project.

                              1 Reply Last reply
                              0
                              • JonBJ JonB

                                @JoeCFD Well, yes, but it doesn't actually get the OP anywhere, does it? :)

                                JoeCFDJ Offline
                                JoeCFDJ Offline
                                JoeCFD
                                wrote on last edited by
                                #19

                                @JonB And your app(especially GUI) will not crash. An error message can be displayed.

                                1 Reply Last reply
                                0
                                • J John Van has marked this topic as solved on
                                • Christian EhrlicherC Christian Ehrlicher

                                  @J-Hilk std will crash also as you don't have 800MB of contiguous memory.

                                  S Offline
                                  S Offline
                                  SimonSchroeder
                                  wrote on last edited by
                                  #20

                                  @Christian-Ehrlicher said in Why is the capacity of qvector smaller than that of std:: vector?:

                                  std will crash also as you don't have 800MB of contiguous memory.

                                  "Contiguous" is usually not the problem. With a 64bit OS and paging you have a huge address space which can be used to create contiguous addresses. PC processors don't use 64bit pointers in hardware and IIRC it was only 52bit pointers initially. This still would give you 4 petabytes of address space per process. You only need enough free memory which can then be put anywhere inside that huge address space. You would have to produce some serious fragmentation (or other really huge data) to fill up that address space.

                                  However, if you only have 8 GB of RAM you'll quickly might run of space if you request 800MB and a lot of other things are also running.

                                  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
                                  • Unsolved