Memory allocation areas in C++ (Stack vs heap vs Static)
I understand there are three, not two areas of memory in C++: stack, heap AND the area for static-assigned features. I have two questions
Why is the heap so much slower than the stack? Surely it should just be one extra level of indirection?
Is the area of memory allocated for static "features" (variables, functions, classes) provide faster performance than the heap?
A couple of side notes first. The correct terminology is automatic rather than stack, dynamic rather than heap. The other is that with C++11 there are now four rather than three types of memory. C++11 adds thread local memory to the mix.
Automatic memory is fast because it is implemented using the call stack on most machines. All it takes is to adjust the stack pointer by the right amount and voila! memory is allocated. Dynamic memory requires a whole lot more work underneath the hood. The requisite memory might not be attached to the process, and getting that to happen requires going through the OS. Even if memory is available, the dynamic memory management tools still have to find it and mark it as in use.
Static memory is "allocated" as a part of the compilation and linking process. When you define a static variable in some source file, the compiled code contains special instructions for the linker to reserve space for that variable. The compiler also converts your C/C++ code to machine code. The linker combines all of those different chunks of data and code and resolves addresses to form the executable binary image. When you run your program, that binary image is loaded into (virtual) memory. The memory for that static variable exists as soon as the program starts executing.
As far as performance is concerned, it's best not to worry too much about performance ahead of time. While static memory is fast, there are lots and lots of drawbacks. The last thing you want to do is to make all of your data static.
1) Why is the heap so much slower than the stack? Surely it should just be one extra level of indirection?
Because allocating memory on stack means increasing1 the stackpointer sp by N
where N
is the number of bytes. Changing sp in that way is fast enough. With heap, you do lots of things, finding free slot or requesting OS for memory, for example, are expensive operations.
1. or decreasing, depending on which way the stack grows!
2) Is the area of memory allocated for static "features" (variables, functions, classes) provide faster performance than the heap?
If you mean static
variables, then yes, they're faster than heap, as they're statically allocated, and they exist till the end of the program. However, there is no such thing as static
class, and memory allocated for functions cannot be compared in this context (the question doesn't make complete sense to me).
When comparing the speed of stack, heap or static allocation areas, there are two different speeds to compare.
First, there is the speed of access . This is comparable for each of the three areas, although local (stack allocated) variables can have a slight edge because they are easier for the compiler to cache in a CPU register. Otherwise, it is essentially just memory access.
Secondly, there is the speed of allocation . This is where the big differences arise.
Statically allocated objects get their memory reserved at program startup (or library load time when they reside in a dynamically loaded library), so their allocation time is immesurably short as far as the program is concerned.
Stack allocated objects are also cheap to allocate, because they take a predictable allocation pattern (last allocated == first deallocated) that the compiler can easily take into account. For example, if an application has multiple threads, it will also have multiple stacks (each stack reserved for one thread), so the threads don't have to compete with each other for access to the stack memory.
Heap allocated objects are the difficult ones, because the heap is used for everything that does not nicely fit within the earlier groups. Also, there is typically only one heap for the entire application, so thread synchronisation is needed to allocate memory from the heap. And, because the allocation/deallocation patterns are fairly random, measures have to be taken to ensure not too much heap memory gets 'lost' due to fragmentation of the heap. This all takes some time that gets paid when doing an allocation or deallocation.
上一篇: 哪种方法可以在C ++中实例化对象?