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. QApplication limits malloc
Forum Updated to NodeBB v4.3 + New Features

QApplication limits malloc

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.4k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    SongCn
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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!

      Greetz, Jeroen

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SongCn
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

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