runtime error terminate called after throwing an instance of 'std::bad_alloc'
-
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? -
What is list1 and list2 defined as ?
QStringList or
QList<QStringList> ? -
@Lasith
Seems somebody else doing the same :)
https://forum.qt.io/topic/85876/increasing-qlist-qstringlist-contents-twice
VRonin show how to add. -
@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?