📜  静态分配和堆分配的区别

📅  最后修改于: 2021-09-28 09:24:00             🧑  作者: Mango

静态分配:
静态分配是一种分配过程,用于在编译时分配所有数据对象。在这种类型的分配中,数据对象的分配仅在编译时完成。由于在静态分配中编译器决定了不随时间变化的存储范围,因此编译器很容易在后期的活动记录中知道这些数据对象的地址。静态分配是在 FORTRAN 中实现的。

堆分配:
堆分配是使用堆来管理内存分配的分配过程。堆有助于管理动态内存分配。在堆分配中,动态数据对象和数据结构的创建也与堆栈分配相同。堆分配克服了堆栈分配的限制。即使在堆分配策略中的活动记录之后,也可以保留变量的值,这在堆栈分配中是不可能的。它为空闲块维护一个链表,并使用最佳拟合重用释放的空间。

静态分配和堆分配的区别如下:

S.No. Static Allocation Heap Allocation
1. Static allocation allocates memory on the basis of size of data objects. Heap allocation make use of heap for managing the allocation of memory at run time.
2. In static allocation, there is no possibility of creation of dynamic data structures and objects. In heap allocation, dynamic data structures and objects are created.
3. In static allocation, the names of the data objects are fixed with storage for addressing. Heap allocation allocates contiguous block of memory to data objects.
4. Static allocation is simple, but not efficient memory management technique. Heap allocation does memory management in efficient way.
5. Static allocation strategy is faster in accessing data as compared to heap allocation. While heap allocation is slow in accessing as there is chance of creation of holes in reusing the free space.
6. Static allocation is inexpensive, it is easy to implement. While heap allocation is comparatively expensive.