Stacks & Stack and Heap

Source: Internet
Author: User

What ' s the difference between a stack and a heap?

The differences between the stack and the heap can be confusing for many people. So, we thought we would has a list of questions and answers about stacks and heaps this we thought would be very helpful.

Where is the stack and heap stored?

They is both stored in the computer ' s RAM (Random Access Memory). For a refresher in RAM and virtual memory, read this article:how virtual memory Works

How does threads interact with the stack and the heap? How does the stack and heap work in multithreading?

In a multi-threaded application, each thread would have the its own stack. But, the different threads would share the heap. Because the different threads share the heap in a multi-threaded application, this also means that there have to be some co Ordination between the threads so, they don ' t try to access and manipulate the same piece (s) of memory in the heap at The same time.

Can An object is stored on the stack instead of the heap?

Yes, an object can is stored on the stack. If you create an object inside a function without using the ' new ' operator then this would create and store the object on T He stack, and not on the heap. Suppose we have a C + + class called Member, for which we want to create an object. We also have a function called somefunction (). Here's what the code would look like:

Code to create a object on the stack:
void somefunction () {/* Create an object ' m ' of class Member this'll be put on the    stack since the     "new" keyword  Is isn't used, and we are    creating the object inside a function*/    Member m;}//the object ' m ' is destroyed once the Function ends

So, the object "M" was destroyed once the function has a run to completion–or, and in other words, when it was "goes out of scope". The memory being used for the object "M" on the stack would be removed once the function was done running.

If we want to the "create an object" on the heap inside a function and then the "what" the code would look like:

Code to create a object on the heap:
void somefunction () {/* Create an object ' m ' of class Member this'll be put on the    heap since the     "new" keyword is used, and we are    creating the object inside a function*/    member* m = new Member ();    /* The object "M" must be deleted      otherwise a memory leak occurs  */  

In the code above, you can see the ' m ' object is created inside a function using the ' new ' keyword. This means, "M" would be created on the heap. But, since ' m ' is created using the ' new ' keyword, that also means that we must delete the ' M ' object on our own as well– Otherwise we'll end up with a memory leak.

How long does memory on the stack last versus memory on the heap?

Once a function call runs to completion, any data on the stack created specifically for that function call would automatica Lly be deleted. Any data on the heap would remain there until it ' s manually deleted by the programmer.

Can The stack grow in size? Can the heap grow in size?

The stack is set to a fixed size, and can not grow past it's fixed size (although some languages has extensions that does a Llow this). So, if there was not enough the class on the stack to handle the memory being assigned to it, a stack overflow Occurs. This often happens when a lot of nested functions be being called, or if there is an infinite recursive call.

If the current size of the heap was too small to accommodate new memory and then more memory can added to the heap by the O Perating system. This is one of the big differences between the heap and the stack.

How is the stack and heap implemented?

The implementation really depends on the language, compiler, and run-time–the Small details of the Implementatio N for a stack and a heap would always is different depending on what language and compiler is being used. But, the "big picture", the stacks and heaps in one language is used to accomplish the same things as stacks and heaps I n another language.

which is faster–the stack or the heap? and why?

The stack is much faster than the heap. This is because of the the-the-allocated on the stack. allocating memory on the stack was as simple as moving the stack pointer up.

How are memory deallocated on the stack and heap?

Data on the stack was automatically deallocated when variables go out of scope. However, in languages like C and C + +, data stored on the heap have to is deleted manually by the programmer using One of the built in keywords like free , delete, or delete[]. Other languages like Java and. NET use garbage collection to automatically delete memory from the heap, without the Progra Mmerhas to do anything.

What can go wrong with the stack and the heap?

If the stack runs out of memory, then this is called a stack overflow –and could cause the program to crash. The heap could has the problem of fragmentation, which occurs when the available memory on the heap is being STO Red as noncontiguous (or disconnected) blocks–because used blocks of memory is in between the unused Memory blocks. When excessive fragmentation occurs, allocating new memory could be impossible because of the fact that even though there is Enough memory for the desired allocation, there is not being enough memory in one big block for the desired amount of memor Y.

which one should I use–the stack or the heap?

For people new to programming, it's probably a good idea to use the stack since it ' s easier.
Because the stack is small, you would want to use it when you know exactly how much memory you'll need for your data, or If you know the size of your data is very small. It's better to use the heap if you know so you'll need a lot of memory for your data, or you just is not sure how M Uch memory you'll need (like with a dynamic array).

Stacks & Stack and Heap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.