📜  C语言中静态和动态内存分配之间的区别

📅  最后修改于: 2021-05-25 21:32:14             🧑  作者: 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基础课程》。