📜  C中静态和动态内存分配的区别

📅  最后修改于: 2021-09-15 01:24:13             🧑  作者: Mango

内存分配:内存分配是为计算机程序和服务分配物理或虚拟内存空间的过程。内存分配在程序执行之前或之时完成。有两种类型的内存分配:

  1. 编译时或静态内存分配
  2. 运行时或动态内存分配

静态内存分配:静态内存是由编译器为声明的变量分配的。该地址可以使用运算符的地址找到并且可以分配给一个指针。内存是在编译时分配的。

动态内存分配:在执行时(运行时)完成的内存分配称为动态内存分配。函数 calloc() 和 malloc() 支持分配动态内存。在动态分配内存空间中,当函数返回值并分配给指针变量时,使用这些函数分配内存空间。

C 中静态和动态内存分配之间的表格差异

S.No

Static Memory Allocation

Dynamic Memory Allocation

1 In the static memory allocation, variables get allocated permanently. In the Dynamic memory allocation, variables get allocated only if your program unit gets active.
2 Static Memory Allocation is done before program execution. Dynamic Memory Allocation is done during program execution.
3 It uses stack for managing the static allocation of memory It uses heap for managing the dynamic allocation of memory
4 It is less efficient It is more efficient
5 In Static Memory Allocation, there is no memory re-usability In Dynamic Memory Allocation, there is memory re-usability and memory can be freed when not required
6 In static memory allocation, once the memory is allocated, the memory size can not change. In dynamic memory allocation, when memory is allocated the memory size can be changed.
7 In this memory allocation scheme, we cannot reuse the unused memory. This allows reusing the memory. The user can allocate more memory when required. Also, the user can release the memory when the user needs it.
8 In this memory allocation scheme, execution is faster than dynamic memory allocation. In this memory allocation scheme, execution is slower than static memory allocation.
9 In this memory is allocated at compile time. In this memory is allocated at run time.
10 In this allocated memory remains from start to end of the program. In this allocated memory can be released at any time during the program.
11 Example: This static memory allocation is generally used for array. Example: This dynamic memory allocation is generally used for linked list.
想要从精选的视频和练习题中学习,请查看C 基础到高级C 基础课程