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 8584827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:51:50+00:00 2026-06-11T21:51:50+00:00

In the past whenever I needed to create an instance of a class I

  • 0

In the past whenever I needed to create an instance of a class I would use new to allocate it on the heap (except for stl classes, and math classes like vec3 and mat4).

However, I was just looking critically at some of my code and realized that technically I could just be making these classes on the stack instead. They’re not very big, don’t need to be modified outside of their current scope, etc. When I (occasionally) need to pass them to another function I can use a reference as easily as I could pass a pointer.

In the past I always defaulted to allocating on the heap, and only used the stack in certain cases, however now I’m wondering if it would have been better to default to allocating on the stack, and only use the heap when

  • a pointer is really needed (ie the lifetime of the object to outlast the scope of declaration)
  • the class or array is too big for the stack
  • inheritance requires it (abstract base class/interface)
  • something else?

Which also raises the question: how big a class is too big (roughly) to reasonably allocate on the stack? (assuming we’re working on, at minimum, smartphones, and going up to high end desktops) Am I just worrying unnecessarily about stack size constraints? (probably, as long as we’re not talking large arrays, and no class is going to be even close to a kilobyte)

  • 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-06-11T21:51:51+00:00Added an answer on June 11, 2026 at 9:51 pm

    I prefer to allocate on the stack, for two reasons. First, all else being equal, it is faster than heap. Also, the deallocation happens automatically, I don’t need to rememeber to delete it (of course, there are auto_ptrs and such to help with that).

    a pointer is really needed

    It is OK to pass a pointer to an object on the stack. Just make sure the user of that pointer does not access the object after its lifetime expires.

    the class or array is too big for the stack

    Only for really big things should this matter. You’ve probably got 1MB of stack, so you can put about 1000 1KB objects before there’s a problem.

    inheritance requires it

    Why would it?

    something else?

    The lifetime required of the object is longer than the lifetime of the stack frame. This is the principal reason to allocate on the heap.

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

Sidebar

Related Questions

In the past I always advocated the use of jQuery instead of Flash whenever
In the past, whenever I needed to get the value of an html element,
I'm running into a problem that I can't seem to get past. Whenever I
I've gotten this to work in the past, but now whenever I choose File
Given a vector of datetime values, I needed to create a data.frame containing datetimes
Am new to android programming, just started learning it the past 6 weeks and
In the past, I've always created a database class and in that class assigned
Whenever I've worked with asp.net authentication / authorization in the past, I can remember
In the past, whenever I have added a file to my resources I haven't
In the past whenever I came across #define it was used like #define MOD

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.