静态分配:
静态分配是一个过程,用于在编译时分配所有数据对象。仅当编译器在编译时知道数据对象的大小时,才可以进行静态分配。在这种类型的分配中,在任何情况下都无法在运行时形成数据对象。在静态分配中,编译器确定每个数据对象的存储量,并将数据对象的名称绑定到分配的存储。
堆栈分配:
堆栈分配是使用堆栈来组织存储的过程。堆栈分配中使用的堆栈称为控制堆栈。在这种分配方式中,数据对象的创建是动态执行的。在堆栈分配中,将创建激活记录以分配内存。使用后进先出(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. |