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. Why is the capacity of qvector smaller than that of std:: vector?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 7 Posters 1.3k 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

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