Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT - Main Widget - Stack or Heap?

I am a little confused on whether I should prefer to initialize my main widgets on the stack or on the heap. In "C++ GUI Programming with QT 4," main widgets are initialized on the stack. Before I say more, I will explain what I mean:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow mainWin;
    mainWin.show();
    return app.exec();
}

Now, perhaps this is simply because it is safer, perhaps it is because they don't want to confuse readers about memory allocation in QT. Leaving out any deletes on objects inheriting from QObject certainly does allow readers to "forget" memory-management with QT objects. But, my question is, should we prefer that method, or the following:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow* mainWin = new MainWindow;
    mainWin->show();
    int execReturn = app.exec();
    delete mainWin;
    return execReturn;
}

The main reason I bring this question is I normally prefer to follow a pattern with choosing heap vs stack:

  • If the item is large - Heap
  • If the item is long-term - Heap
  • Otherwise - Stack

Now, I guess my question boils down to two questions:

  • Is QWidget large to the point where I should fear stack-overflows?
  • How large is the stack for the average application? When should I fear for overflowing the stack (Other than obviously recursive functions)?

I realize it is unlikely that QWidget itself causes a stack-overflow, but of course this will be on top of any other stack usage my application may have.

like image 615
Serodis Avatar asked Dec 31 '11 15:12

Serodis


2 Answers

Your pattern for choosing heap vs stack sounds reasonable but I wouldn't worry so much about the size of the object. Any large object should use the heap internally. std::vector is usually the size of three pointers but can be very large.

I don't think you should fear that any object is large enough to overflow the stack by itself. While possible it is certainly very rare (I haven't seen one).

I would recommend thinking about simplicity also, you could allocate any local variable on the heap and then free it before the function returns, but this would be unnecessarily complicated and generally considered bad practice.

Stack size is usually configured through linker settings. On Windows it's 1MB by default.

like image 125
Nikola Smiljanić Avatar answered Nov 20 '22 03:11

Nikola Smiljanić


I prefer to use the stack-based approach as it simply gives short code. Your concern for stack overflow would be plausible, but it is highly unlikely to happen.

Although I don't have idea for the size of QApplication and MainWindow, it will (mostly) use heap for internal data structures that require huge size. So, you don't need to worry that much for potential stack overflow.

Typically, Windows applications have 1MB stack size. But, you can easily change by changing linker option: /STACK.

like image 24
minjang Avatar answered Nov 20 '22 03:11

minjang