Unsolved How can i extend the loops limit?
-
@Eduardo12l The problem is mostly likely not in a loop itself.
For example - you allocate memory in the loop and exhaust your system or you are trying dereference a pointer which at a certain iteration become a null ...
In any way get used to the fact that people to help you in general need to see the code.you do not mechanic " hey, my car stops 2 minutes after I start engine, could you fix it without seen the car?"
I like when people trust in me as I were a god, but I can't give you an useful answer. -
Please, give us more context if you want help... Specific questions.
-
@Eduardo12l There is no such limit! So there is nothing to extend.
You're doing something wrong in your code.
But nobody can help you if you do not share your code! -
@Eduardo12l
can you provide a sample code, it could help -
@AlexMal @Charlie_Hdz @jsulm @SGaist @yuvaram Sorry for my english. As you can at the bottom of this comment on_pushbutton_clicked() should calls criterios() 10000 times. However, it crashes close to 6500. If I erase the content of this for
for(int i = 0; i < DiasOrdenados.size(); i++)
it continues crashing close to 6500. If I comment that loop it does not crash (i mean // for(int i = 0; i < DiasOrdenados.size(); i++)). I dont understand why that for is causing a crash.
HEADER ventanas1.h
n = 0; QList<QString> DiasOrdenados << LUNES << MARTES << MIERCOLES << JUEVES << VIERNES;
SOURCE ventanas1.cpp
void Ventanas1::on_pushButton_clicked(){ n++; qDebug()<<"n es"<<n; val = 1; cont = 0; z = true; if(n <= 10000){/ criterios(); }else{ ui->pushButton->setStyleSheet ("background-color: QLinearGradient(spread:pad x1: 0, y1: 0, x2: 0, y2: 1, stop: 0.0" + Vint[2][0] + ", stop: 0.4" + Vint[2][1] + ", stop: 1.0"+ Vint[2][2]+");" "color: #868479; " "border-style: solid;" "border-style: solid;" "border-radius: 7;" "padding: 3px;" "padding-left: 5px;" "padding-right: 5px;" "border-color: #339;" "border-width: 1px;" "font:Bold;" "font-family:Georgia"); //Problema potencial 1. Solo una vez permite meterle el stylesheet. ui->pushButton->setText("No hay más\nopciones"); ui->pushButton->setEnabled(false); } } void Ventanas1::criterios(){ if(Mnh < 8){ qDebug()<<"entro"; umero = 0; for(int i = 0; i < DiasOrdenados.size(); i++){ //In this for happends my problem if(DefDias.count(DiasOrdenados.at(i)) > 0){ cont = 0; val = 1; while(cont < DefDias.size()){ if(DefDias.indexOf(DiasOrdenados.at(i),cont) != -1){ IndCriterios << DefDias.indexOf(DiasOrdenados.at(i), cont); cont = DefDias.indexOf(DiasOrdenados.at(i), cont)+1; }else{ break; } } if(IndCriterios.size() > 1){ for(int j = 0; j < IndCriterios.size(); j++){ VectorAux << DefHoraF[IndCriterios[j]]; } for(int h = 0; h < VectorAux.size(); h++){ VectorAux.prepend(DefHoraI[IndCriterios[h]]); qSort(VectorAux.begin(), VectorAux.end()); if(VectorAux.at(0) != DefHoraI.at(IndCriterios[h])){ if(VectorAux.at(VectorAux.indexOf(DefHoraI[IndCriterios[h]])-1) - DefHoraI.at(IndCriterios[h]) + Mth < 0){ val *= 0; numero++; }else if(VectorAux.at(VectorAux.indexOf(DefHoraI[IndCriterios[h]])-1) - DefHoraI.at(DefHoraI[IndCriterios[h]]) + Mth == Mth){ val *= 1; }else{ numero++; val *= 1; } } VectorAux.removeAt(VectorAux.indexOf(DefHoraI.at(IndCriterios[h]))); } } IndCriterios.clear(); VectorAux.clear(); } } } on_pushButton_clicked(); }
PS: I dont know if recycling variables is causing that problem. Because I used some variables like cont and var several times in others methods.
-
Easy, You are modifying the Vector Aux size indirectly with the functions:
Prepend()
And RemoveAT()
Please modify your implementation.
-
@Charlie_Hdz But it can not be the problem because if i erase all the content of
for(int i = 0; i < DiasOrdenados.size(); i++
Not only that part you said but all the content it continue failing. The difference happends when that for is present, that is the inflection point
-
First,
Are you going to modify the DiasOrdenados, if not? Better use QVector.
Then, your indexes are kind of confusing, please comment the end of the parenthesis.
I Recommend you to debug, do you know how to debug?
Thanks
-
I have noticed that qDebug() << entro is affecting my code in some way but i dont know how. Maybe memory being affected. if i erase that qDebug() << entro it work. Wow it so rare
Ps: The problem happens in the moment when a function is called.
-
@Charlie_Hdz i only use qdebug ¿Is there another way to check my code?
Ps: Whats the problem with having a loop with a condition that is dynamic? Why do you recommend use QVector instead QList? -
@Eduardo12l Now, immediately, learn to debug properly. Qt Creator has the Debug button ("play" arrow with a bug) under Run button, and also Debug top level menu item.
Your code structure is something like
if for if while if else break if for for qSort(VectorAux.begin(), VectorAux.end()); if if else if else
First, it's difficult to read, understand or debug with any method and it's very error prone. After you have solved one problem you may run into another. You should rewrite it to be clear, easy and self-documenting. Second, you use qSort in n-th level inner loop which is probably inefficient (how many times it's executed?). Third, there might be (not necessarily) good candidates for C++ standard library algorithms like find_if which should be preferred to custom loops, both for error-freeness and readability.
-
For debugging : https://www.youtube.com/watch?v=B7UsWtyhXh4
qDebug() is a output stream special for debugging. It doesn't represent the debugging process in its whole.
About QVector,I can see a lot of improvement here:
First, You're not modifying your QVector, so you can put it const. This is useful to avoid bugs (Nice!)
Second, because you're not inserting in the middle of the "Container" then you don't need QList (QList is much more inefficient that QVector)
Third, QVector<QString> can be easily replaced by a QStringList. Look in documentation for its advantages.
Then returning to the code:
index in this way:
while{ if{ }//end if else{ }//end else }//end while
Regardless what @Eeli-K said about qSort(), I think you need to practice more programming in general. Qt is facinating, all I know I learned programming in C++/Qt. Check some books, DEBUG, and enjoy.
... If you finish debugging and you have a good question, please look at us.
Thanks