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

📅  最后修改于: 2021-06-28 07:14:39             🧑  作者: 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.