Why is heap slower than stack




















But it's not the only pattern, or even the only common pattern. To write efficient program that can tackle a wide variety of problems we have to make allowance for the less common patterns, even if it means a more complex infratstructure.

If I can get all meta for a paragraph: you are a beginner, as a beginner you value simplicity, and black and white rules. However, as a beginner you have only a peephole view of the range of problems and constraints that have to be accommodated by computer programs. You are entering a technology that has been under active development for going on 75 years. There is nothing wrong with asking why things are the way they are, but the answer is generally going to be "Yeah, we tried the simple, straight-forward method 50 years ago, and it turned out not to work very well for entire classes of problems, so we had to do something more complicated".

As a technology advances simplicity generally has to give way to efficiency and flexibility. For another example, closures. If you stack pop, then you can lose your closure's activation record. So if you want that anonymous function and its data to persist you've got to store it somewhere other than the runtime stack.

If you want to pass the address of an object back to a calling program, you should not pass the address of a stack variable, as, the stack storage will be reused and possibly overwritten by the next function called. You need to get the required storage using malloc to ensure it will not be overwritten by calls to subsequent functions. You can pass addresses of stack items from your function to functions you call, because you can guarantee they will exist until your program "returns ".

But as soon as your function returns all the stack storage is up for grabs. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Learn more. Why do we need a Heap if everything can be done much more efficiently on the Stack? Ask Question. Asked 10 years, 1 month ago. Active 10 years, 1 month ago. Viewed 16k times. Improve this question. Community Bot 1. Dark Templar Dark Templar 6, 16 16 gold badges 44 44 silver badges 45 45 bronze badges. Two excerpts from your previous question: "the most important downside is that it has limited space, and so keeping large objects in it, or trying to use it for long-lived objects, are both bad ideas" and "stacks are an extremely efficient structure for managing data that obeys LIFO last in first out rules".

Your premise is faulty - not everything can be done much more efficiently on the stack. This is no contradiction to the answers you received - that what can be done on the stack can be done much faster there. Here we discuss the Stack vs Heap Memory key differences with infographics and comparison table.

You may also have a look at the following articles to learn more —. Submit Next Question. By signing up, you agree to our Terms of Use and Privacy Policy. Forgot Password? This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy.

Course Price View Course. Free Software Development Course. Login details for this Free course will be emailed to you. Email ID. Contact No. The Heap-memory allocation is further divided into three categories:- These three categories help us to prioritize the data Objects to be stored in the Heap-memory or in the Garbage collection. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Next Memory Layout of C Programs. Recommended Articles.

Article Contributed By :. Easy Normal Medium Hard Expert.



0コメント

  • 1000 / 1000