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

📅  最后修改于: 2021-09-13 02:12:26             🧑  作者: Mango

静态分配:
静态分配是用于在编译时分配所有数据对象的过程。只有当编译器在编译时知道数据对象的大小时,才可能进行静态分配。在这种类型的分配中,在任何情况下都无法在运行时形成数据对象。在静态分配中,编译器决定每个数据对象的存储量,并将数据对象的名称绑定到分配的存储空间。

堆栈分配:
堆栈分配是使用堆栈来组织存储的过程。堆栈分配中使用的堆栈称为控制堆栈。在这种类型的分配中,数据对象的创建是动态执行的。在堆栈分配中,为内存分配创建活动记录。这些激活记录使用后进先出 (LIFO) 方法推送到堆栈上。局部变量在运行时存储在激活记录中,内存寻址是通过使用指针和寄存器完成的。

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

S.No. Static Allocation Stack Allocation
1. Static Allocation does not makes data structures and objects dynamically. Stack allocation makes data structures and objects dynamically.
2. In static allocation, allocation of all data objects is performed at compile time. While in stack allocation, allocation of data objects is performed at run time.
3. It does not support recursive procedures. It supports recursive procedures.
4. Static allocation is not able to manage the allocation of memory at run time. Stack allocation use stack to manage the allocation of memory at run time.
5. In static allocation, at compile time the data object names are fixed. In stack allocation, index and registers performs the memory addressing.
6. This strategy is easy and simple in implementing. This strategy is slower than static allocation.