QT - Main Widget - Stack or Heap?
Asked Answered
E

2

10

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.

Evelyn answered 31/12, 2011 at 15:36 Comment(3)
Use sizeof to see how large the object is. There is no "average stack", it varies widely between applications, and the limits vary widely by platform. If you're worried, and since this one is easy to track and there is absolutely no performance aspect, just use the heap.Upbear
You say it varied by application, I assume you mean stack-used, but what about available. How about instead of looking for an average per application, what's the minimum stack-size for a Windows, Linux or Mac application in the past 15 years? Basically, if I'm creating a cross-platform application, I'm curious what limits I should expect. --- sizeof shows 20, is this accurate, that seems a bit small.Evelyn
@Serodis: sizeof is accurate.Upbear
B
4

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.

Brendabrendan answered 31/12, 2011 at 16:2 Comment(1)
It's pretty standard for QMainWindow to be stack like this. It makes the lifetime and the fact that there is only one very clear to the readerMonumental
F
3

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.

Feticide answered 31/12, 2011 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.