理解static storage area VS. stack VS. heap In C++

  • Static vs Dynamic

Static : Storage can be made by compiler looking only at the text of the program. One reason for statically allocating as many data objects as possible is that the addresses of these objects can be compiled into target code.

Dynamic : Storage can be made by looking at what the program does when the program is running.

  • Stack and heap

The definitions of stack and heap don’t depend on value and reference types whatsoever. In other words, the stack and heap can be fully defined even if value and reference types never existed. Further, when understanding value and reference types, the stack is just an implementation detail.(参考《The Stack Is An Implementation Detail, Part One》《References are not addresses》)

  • references are not addresses

From “References are not addresses”, the words - a variable of reference type stores the address of the object - which is not right.

  • stack
  • What’s the difference between a stack and a heap

  1. where are the stack and heap stored?

    They are both stored in the computer’s RAM

  2. how do threads interact with the stack and the heap? how do the stack and heap work in multithreading?

    In a multi-threaded application, each thread will have its own stack. But, all the different threads will share the heap.

    Because the different threads share the heap in a multi-threaded application, this also means that there has to be some coordination between the threads so that they don’t try to access and manipulate the same piece of memory in the heap at the same time.

  3. can an object be stored on the stack instead of the heap?

    Yes, an object can be stored on the stack.

    If you create an object inside a function without using the “new” operator then this will create and store the object on the stack, and not on the heap.

    If we want to create an object on the heap inside a function, then the “new” keyword should be used. But same reason, the object on the heap created by “new” should be deleted on our own, otherwise we will end up with a memory leak.

  4. how long does memory on the stack last versus memory on the heap?

    Once a function call runs to completion, anyu data on the stack created specifically for that function call will automatically be deleted.

    Any data on the heap will remain there until it’s manually deleted by the programmer.

  5. can the stack grown in size? or heap?

    The stack is set to a fixed size, and can not grow past it’s fiexed size. So, if there is not enough room on the stack to handle the memory being assigned to it, a stack overflow occurs. This often happens when a lot of nested fucntions are being called, or if there is an infinite recursive call.

    If the current size of the heap is too small to accommodate new memory, then more memory can be added to the heap by the operating system. This is one of the big difference between the heap and the stack.

  6. how are the stack and heap implementd

    The implementation really depends on the language, compiler, and run-time - the small details of the implementation for a stack and heap will always be different depending on what language and compiler are being used. But, in the big picture, the stacks and heaps in one language are used to accomplish the same things as stacks and heaps in another language.

  7. which is faster - the stack or the heap? and why?

    The stack is much faster than the heap.

    This is because of the way that memory is allocated on the stack. Allocating memory on the stack is as simple as moving the stack pointer up.

  8. how is memory deallocated on the stack and heap?

    Data on the stack is automatically deallocated when variables go out of scope.

    However, in languages like C and C++, data stored on the heap has to be 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 collcetion to automatically delete memory from the heap, without the programmer having to do anything.

  9. 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 have the problem of fragmentation, which occurs when the available memory on the heap is being stored as noncontiguous (or disconnected) blocks - beacuse used blocks of the momery are in between the unused memory blocks. When excessive fragmentation occurs, allocating new memory may be impossible because of the fact that even though there is enough memory for the desired allocation, there may not be enough memory in one big block for the desired amount of memory.

  10. 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 will need for your data, or if you know the size of your data is very small.

    It’s better to use the heap when you will need a lot of memory for your data, or you just are not sure how much memory you will need (like with a dynamic array).

  • References

  1. Static Storage vs Heap vs Stack
  2. What’s the difference between a stack and a heap?
  3. Six important .NET concepts: Stack, heap, value types, reference types, boxing, and unboxing

你可能感兴趣的:(#,C,C++,Cython)