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. runtime error terminate called after throwing an instance of 'std::bad_alloc'
QtWS25 Last Chance

runtime error terminate called after throwing an instance of 'std::bad_alloc'

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt creatorqlistqstringlistruntime error
8 Posts 3 Posters 7.8k 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.
  • L Offline
    L Offline
    Lasith
    wrote on last edited by
    #1

    In my Qt C++ application I have a QList<QStringList> I want to increase the contents of each index by the same amount!

    void MainWindow::on_pushButton_clicked()
    {

        list1<<"a"<<"b"<<"c"<<"d"<<"e";
        list2<<"f"<<"g"<<"h"<<"i"<<"j";
    
        List1<<list1;
        List2<<list2;
    
    
    
            for(int k=0;k<2;k++){
            for(int i=0;i<List1.size();i++){
                for(int j=0;j<List1[0].size();j++){
                List1[i]<<List1[i][j];
                }
            }
    
    }
    }
    

    from the above code I want to make the contents of 0th index of List (i.e List[0]) a QStringList of "a" "b" "c" "d" "e" "a" "b" "c" "d" "e" and the contents of 1st index of List (i.e List[1]) a QStringList of "f" "g" "h" "i" "j" "f" "g" "h" "i" "j"! But when I click the button I get the run time error stating

    terminate called after throwing an instance of 'std::bad_alloc'
    How can I correct this?

    JonBJ 1 Reply Last reply
    0
    • L Lasith

      In my Qt C++ application I have a QList<QStringList> I want to increase the contents of each index by the same amount!

      void MainWindow::on_pushButton_clicked()
      {

          list1<<"a"<<"b"<<"c"<<"d"<<"e";
          list2<<"f"<<"g"<<"h"<<"i"<<"j";
      
          List1<<list1;
          List2<<list2;
      
      
      
              for(int k=0;k<2;k++){
              for(int i=0;i<List1.size();i++){
                  for(int j=0;j<List1[0].size();j++){
                  List1[i]<<List1[i][j];
                  }
              }
      
      }
      }
      

      from the above code I want to make the contents of 0th index of List (i.e List[0]) a QStringList of "a" "b" "c" "d" "e" "a" "b" "c" "d" "e" and the contents of 1st index of List (i.e List[1]) a QStringList of "f" "g" "h" "i" "j" "f" "g" "h" "i" "j"! But when I click the button I get the run time error stating

      terminate called after throwing an instance of 'std::bad_alloc'
      How can I correct this?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Lasith

      List1[i]<<List1[i][j];

      What's this? :)

      L 1 Reply Last reply
      0
      • JonBJ JonB

        @Lasith

        List1[i]<<List1[i][j];

        What's this? :)

        L Offline
        L Offline
        Lasith
        wrote on last edited by
        #3

        @JNBarchan adding elemnts to existing indices

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What is list1 and list2 defined as ?
          QStringList or
          QList<QStringList> ?

          L 1 Reply Last reply
          0
          • mrjjM mrjj

            What is list1 and list2 defined as ?
            QStringList or
            QList<QStringList> ?

            L Offline
            L Offline
            Lasith
            wrote on last edited by Lasith
            #5

            @mrjj They are QStringLists!

            mrjjM 1 Reply Last reply
            0
            • L Lasith

              @mrjj They are QStringLists!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @Lasith
              Seems somebody else doing the same :)
              https://forum.qt.io/topic/85876/increasing-qlist-qstringlist-contents-twice
              VRonin show how to add.

              L 1 Reply Last reply
              0
              • mrjjM mrjj

                @Lasith
                Seems somebody else doing the same :)
                https://forum.qt.io/topic/85876/increasing-qlist-qstringlist-contents-twice
                VRonin show how to add.

                L Offline
                L Offline
                Lasith
                wrote on last edited by
                #7

                @mrjj sorry he does not get any error but I get a run time error :(

                mrjjM 1 Reply Last reply
                0
                • L Lasith

                  @mrjj sorry he does not get any error but I get a run time error :(

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @Lasith
                  well std::bad_alloc often comes when out of memory.
                  My guess is that <List1[0].size() is always true since you add to it for each loop
                  and hence create an infinite loop.

                  Did you try VRonin code?

                  Update:
                  If you just want list1 in [0] and list2 in[1]
                  why all the loops then?

                  alt text

                  1 Reply Last reply
                  1

                  • Login

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