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

QApplication limits malloc



  • Hi everyone,

    I've got some problems when I want to dynamically allocate a large array with a QApplication. But the allocation is successfull when I allocate with a QCoreApplication or without Qt at all.

    With QApplication, the second allocation fails :
    @
    int main(int argc, char ** argv)
    {

    double *matriceBrute,*matriceSortie;
    int numberFiles = 1000;
    int nCol = 320;
    int nRow = 240;
    QApplication app(argc,argv);
    matriceBrute = (double*)malloc(nCol*nRow*numberFiles*sizeof(double));
    matriceSortie = (double*)malloc(nCol*nRow*numberFiles*sizeof(double));
    if (matriceBrute == NULL)
        cout << "Matrice Brute failed" << endl;
    else
        cout << "Matrice Brute succeed" << endl;
    if (matriceSortie == NULL)
        cout << "Matrice Sortie failed" << endl;
    else
        cout << "Matrice Sortie succed" << endl;
    

    }
    @

    With QCoreApplication, the two allocations are successful :
    @
    int main(int argc, char ** argv)
    {

    double *matriceBrute,*matriceSortie;
    int numberFiles = 1000;
    int nCol = 320;
    int nRow = 240;
    QCoreApplication app(argc,argv);
    matriceBrute = (double*)malloc(nCol*nRow*numberFiles*sizeof(double));
    matriceSortie = (double*)malloc(nCol*nRow*numberFiles*sizeof(double));
    if (matriceBrute == NULL)
        cout << "Matrice Brute failed" << endl;
    else
        cout << "Matrice Brute succeed" << endl;
    if (matriceSortie == NULL)
        cout << "Matrice Sortie failed" << endl;
    else
        cout << "Matrice Sortie succed" << endl;
    

    }
    @

    And without Qt at all, it's also working :
    @
    int main(int argc, char ** argv)
    {

    double *matriceBrute,*matriceSortie;
    int numberFiles = 1000;
    int nCol = 320;
    int nRow = 240;
    matriceBrute = (double*)malloc(nCol*nRow*numberFiles*sizeof(double));
    matriceSortie = (double*)malloc(nCol*nRow*numberFiles*sizeof(double));
    if (matriceBrute == NULL)
        cout << "Matrice Brute failed" << endl;
    else
        cout << "Matrice Brute succeed" << endl;
    if (matriceSortie == NULL)
        cout << "Matrice Sortie failed" << endl;
    else
        cout << "Matrice Sortie succed" << endl;
    

    }
    @

    Have any idea how I can make it work with QApplication ? By the way, I'm working on Win7 64 bits with Qt Creator mingw 32 bits.

    Thanks.

    Song Chen



  • Hi, What QApplication does is protect you from some serious malpractice! From the OS you only get a limited amount of stack! You try to allocate a very large amount of stack data which probably doesn't fit in that part of memory.
    Using malloc is highly discouraged in C++ and in Qt it's almost forbidden!
    Use the 'new' keyword and put all elements in a QList or QMap! Use Qt containers!! They will be stored in HEAP, not stack. In stack only a list of pointers is kept, so a minimum of stack is required.
    You're not writing C++, your doing C!



  • Well, I tried with new double[] and std::vector and had the same error. But I'm going to look in QList and QMap.

    Thanks


Log in to reply