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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:24:51+00:00 2026-05-20T00:24:51+00:00

Possible Duplicate: C++ Which is faster: Stack allocation or Heap allocation What is more

  • 0

Possible Duplicate:
C++ Which is faster: Stack allocation or Heap allocation

What is more efficient from memory allocation perspective – stack memory or heap memory? What it depends on?

Obviously there is an overhead of dynamic allocation versus allocation on the stack. Using heap involves finding a location where the memory can be allocated and maintaining structures. On the stack it is simple as you already know where to put the element. I would like to understand what is the overhead in worst case in milliseconds on supporting structures that allow for dynamic allocation?

  • 1 1 Answer
  • 1 View
  • 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-20T00:24:51+00:00Added an answer on May 20, 2026 at 12:24 am

    Stack is usually more efficient speed-wise, and simple to implement!

    I tend to agree with Michael from Joel on Software site, who says,

    It is more efficient to use the stack
    when it is possible.

    When you allocate from the heap, the
    heap manager has to go through what is
    sometimes a relatively complex
    procedure, to find a free chunk of
    memory. Sometimes it has to look
    around a little bit to find something
    of the right size.

    This is not normally a terrible amount
    of overhead, but it is definitely more
    complex work compared to how the stack
    functions. When you use memory from
    the stack, the compiler is able to
    immediately claim a chunk of memory
    from the stack to use. It’s
    fundamentally a more simple procedure.

    However, the size of the stack is
    limited, so you shouldn’t use it for
    very large things, if you need
    something that is greater than
    something like 4k or so, then you
    should always grab that from the heap
    instead.

    Another benefit of using the stack is
    that it is automatically cleaned up
    when the current function exits, you
    don’t have to worry about cleaning it
    yourself. You have to be much more
    careful with heap allocations to make
    sure that they are cleaned up. Using
    smart pointers that handle
    automatically deleting heap
    allocations can help a lot with this.

    I sort of hate it when I see code that
    does stuff like allocates 2 integers
    from the heap because the programmer
    needed a pointer to 2 integers and
    when they see a pointer they just
    automatically assume that they need to
    use the heap. I tend to see this with
    less experienced coders somewhat –
    this is the type of thing that you
    should use the stack for and just have
    an array of 2 integers declared on the
    stack.

    Quoted from a really good discussion at Joel on Software site:

    stack versus heap: more efficiency?

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

Sidebar

Related Questions

Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc I
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc. I
Possible Duplicate: Which is more correct: <h1><a>..</a></h1> OR <a><h1>..</h1></a> Semantically, which is more correct:
Possible Duplicate: Regex.IsMatch vs string.Contains Which is faster, preferable and why? What the difference
Possible Duplicate: .NET Integer vs Int16? Which is faster for large numbers of tests
Possible Duplicate: for vs foreach vs while which is faster for iterating through arrays
Possible Duplicate: Which style of Ruby string quoting do you favour? Is there a
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc. SELECT
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc. I
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc What

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.