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

  • Home
  • SEARCH
  • 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 163543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:35:09+00:00 2026-05-11T11:35:09+00:00

I was looking for a rule of thumb for allocating objects on stack or

  • 0

I was looking for a rule of thumb for allocating objects on stack or heap in C++. I have found many discussions here on SO. Many people said, it’s about the lifetime of an object. If you need more lifetime than the scope of the function, put it in the heap. That makes perfect sense.

But what made me confusing is, many people said, allocate objects to stack if they are small. If objects are big, put it to heap. But none of them said how to identify an object is big or not?

I have the following questions,

  1. How to identify an object is big or not?
  2. What will be the stack maximum size? Each OS will have different stack size?
  3. I have a wrapper class which wraps vector<string>. It will have around 100 items. Will it make a stack overflow if I allocate this class to a stack? I tried this, but it worked perfectly. Not sure I am doing something wrong.
  • 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. 2026-05-11T11:35:09+00:00Added an answer on May 11, 2026 at 11:35 am

    Well firstly vectors (and all the STL container classes) always allocate from the heap so you don’t have to worry about that. For any container with a variable size it’s pretty much impossible to use the stack.

    If you think about how stack allocation works (at compile time, basically by incrementing a pointer for each object) then it should be clear that vector memory comes from the heap.

    std::vector<int> myInts; std::string myString; SomeOther Class;  // this memory must come from the heap, there's no way it // can now be allocated on the stack since there are other // objects and these parameters could be variable myString = 'Some String'; myInts.reserve(256); 

    Unless you are in a recursive function you can place several kilobytes of data on the stack without much worry. Stack-sizes are controlled by the program (not the OS) and the default tends to range from 32kb – 1mb. Most desktop software comes in at the 1mb range.

    Individual objects are almost never a concern. In general they will either be small enough for the stack, or will allocate internally from the heap.

    If objects are local to a function put them on the stack. If not put them on the heap.

    Use the heap for large buffers you allocate for loading/sorting/manipulating data.

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

Sidebar

Ask A Question

Stats

  • Questions 80k
  • Answers 80k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I ran into this problem a while back. I'm not… May 11, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer It depends on what you look for. If you are… May 11, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer You could just cast it with (uint)ptr I believe (If… May 11, 2026 at 4:24 pm

Related Questions

Good morning, I am currently writing a python library. At the moment, modules and
I'm looking to write unit tests for a method such as this one: public
I've been looking around for at a way to make some more advanced business
I'm building a .NET 3.5 application and have the need to evaluate JS code

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.