内存篇之静态与动态分配

本文探讨了C语言中的静态和动态内存分配。静态内存分配在编译连接时由编译器处理,而动态内存分配在程序运行时通过函数如malloc进行。静态内存包括栈、全局数据区、常量区,而动态内存主要涉及堆。数组是静态分配的典型例子,无需额外开销但大小固定;malloc则允许运行时灵活分配,但有额外CPU成本。选择静态还是动态内存分配,实质上是在编译器预估和运行时灵活性之间权衡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    经常听到诸如静态分配/动态分配以及静态库等类似表述,这些究竟是什么含义呢?老规矩,看看万能wiki上的定义:

The word static refers to things that happen at compile time and link time when the program is constructed--as opposed to load time or run time when the program is actually started.

The term dynamic refers to things that take place when a program is loaded and executed.

    软件范畴的静态和动态,主要以程序编译和运行的不同阶段划分。发生在程序编译连接阶段,由编译器处理和支配的是静态;而发生在程序加载和运行阶段的就是动态。这一划分方式可推及到很多相关概念,如动静态库,动静态链接和动静态内存分配等。

    所以程序编译连接时,由编译器划分的存储空间就是静态内存,对应的过程是静态分配;而在程序运行阶段,通过函数(malloc)按需申请的内存就是动态内存,其过程为动态分配。具体到不同内存区,堆内存是动态分配,没有静态堆的说法,其它如栈、全局数据区、常量区和程序代码区,一般都在编译阶段划分,属静态分配(特殊时也会动态划分)。

    数组和malloc申请的内存各自代表体现了静态和动态的特点(参考”指针篇指针与数组一节)。数组可直接使用,不需要用malloc去临时请求buffer,没有额外CPU开销。但数组无法在运行中根据情况变化按需请求内存,事先定义的数组大了会浪费,不够的话又可能引起下标越界;而用malloc请求内存需要一些开销,但它可以灵活的按需临时分配

    因此选择静态与动态其实是选择事先完成但只能预估的编译器,或者消耗资源但方式灵活的CPU,其中的思想和智慧值得体味。

    "静态"还有一种更狭义的定义,只把static extern变量以及常量等存储于主数据区的元素称为静态,而局部变量和数组等栈里的元素称为自动变量。具体看看wiki大大怎么描述(手痒,自作主张加了点注释):

    The C language manages memory statically, automatically, or dynamically. Static-duration variables are allocated in main memory, usually along with the executable code of the program, and persist for the lifetime of the program(即static变量和全局变量; automatic-duration variables(所谓自动变量,即局部变量) are allocated on the stack and come and go as functions are called and return. For static-duration and automatic-duration variables, the size of the allocation is required to be compile-time constant (before C99, which allows variable-length automatic arrays). If the required size is not known until run-time (for example, if data of arbitrary size is being read from the user), then using fixed-size data objects is inadequate.(确实如此)

    The lifetime of allocated memory is also a concern. Neither static- nor automatic-duration memory is adequate for all situations(各有缺陷). Automatic-allocated data cannot persist across multiple function callslifetime过短,不够), while static data persists for the life of the program whether it is needed or notlifetime过长,浪费). In many situations the programmer requires greater flexibility in managing the lifetime of allocated memory(需要更灵活的内存使用方式).

    These limitations are avoided by using dynamic memory allocation in which memory is more explicitly (but more flexibly) managed, typically, by allocating it from the heap, an area of memory structured for this purpose. In C, the library function malloc is used to allocate a block of memory on the heap. Then the program can access this block of memory via a pointer that malloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值