Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Prevent abnormal program termination



  • Hi at all, I would like to know if exist in Qt a specific method to prevent abnormal program termination.
    For example, if I've this code:

    QStringList lst = {"Bananas", "Apple", "Dog";}
    qDebug() << lst.at(0);
    qDebug() << lst.at(1);
    qDebug() << lst.at(2);
    qDebug() << lst.at(3);
    

    The line qDebug() << lst.at(3); will cause an abnormal program termination because the index is out of range.
    Have Qt a specific managment for these case to prevent the program crashes?
    Thanks.

    Stefano


  • Lifetime Qt Champion

    Hi,

    There's nothing more in Qt than what is available in C++. Whether you do that with a Qt container or a STL, you will have the same result.

    In any case, it's always better to check for your containers sizes when reaching for specific indexes like that. Otherwise you can use iterators for example.



  • Hi SGaist and many thanks for your answer :)


Log in to reply