Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7022615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:36:19+00:00 2026-05-27T23:36:19+00:00

I am a little confused on whether I should prefer to initialize my main

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:36:20+00:00Added an answer on May 27, 2026 at 11:36 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im a little confused about whether should I use a nested Subquery Or JOINS
I'm a little confused as to how I should be using didSelectRowAtIndexPath . Without
I'm a little confused over the documentation of type TIMESTAMP in MySQL and whether
Although having a little experience, I am still confused with the question of whether
I am a little confused in my about permissions, in my dev environment I
I am a little confused on how exactly postScale method calculates the values of
I am a little confused about Accept-Encoding . I have Web Service which would
I'm a little confused by the structure of the Node class in OpenJDK's implementation
I am a little confused about the roles of a java application server and
I'm a little confused about something. I wrote an app and tested it on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.