静态内存分配与动态内存分配 [英] static memory allocation versus dynamic memory allocation

查看:115
本文介绍了静态内存分配与动态内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C编程中,我想知道在什么情况下我们应该使用

静态内存分配而不是动态内存分配。我的理解是,使用数组的静态内存分配比malloc更快,但动态内存分配更灵活。


请评论......谢谢。

解决方案

Ken说:


在C编程中,我想知道在什么情况下我们应该使用

静态内存分配而不是动态内存分配。我的理解是,使用数组的静态内存分配比malloc更快,但动态内存分配更灵活。



这是正确的。从广义上讲,如果你事先知道你需要多少内存,你就不需要使用malloc(除非你的实现对数量有一个非常低的
下限可用于自动对象的内存,但对免费商店的限制限制较少。


但是malloc& co让你灵活地扩展你的数据结构只需要它们就可以了,并且(最重要的是)决定是将b / b推迟到运行时间(即你真的知道有多少数据

你有!)。


-

Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。




Ken写道:


在C编程中,我想知道在什么情况下我们应该使用

静态内存分配而不是动态内存分配。我的理解是,使用数组的静态内存分配比malloc更快,但动态内存分配更灵活。



Ho,Ho,Ho,做好作业!


Tom


Ken写道:


在C编程中,我想知道在什么情况下我们应该使用

static内存分配而不是动态内存分配。我的理解是,使用数组的静态内存分配比malloc更快,但动态内存分配更灵活。



如果事先知道需要的内存量,或者动态分配不可用,则使用静态分配(可能发生)在

一些嵌入式系统中)。使用自动变量(一种静态的分配形式,虽然实际上可能不是这样),也有助于数据封装和自动内存管理。


当您预计数据结构增长时,动态内存尤为重要,您可能只知道所需的最大内存,但是只需要内存,但是实际使用量可能远低于此值,当你需要

时,数据会在整个过程的整个过程中持续存在,但是你不是b
),想要将全局记忆用于此目的。


In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understanding is that static memory allocation like using array is
faster than malloc, but dynamic memory allocation is more flexible.

Please comment... thanks.

解决方案

Ken said:

In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understanding is that static memory allocation like using array is
faster than malloc, but dynamic memory allocation is more flexible.

That''s about right. Broadly, if you know in advance how much memory you
need, you don''t need to use malloc (unless your implementation has a very
low limit on the amount of memory available for auto objects, but a less
restrictive limit on free store).

But malloc & co give you the flexibility to grow your data structures only
as large as they need to be, and (most importantly) the decision is
deferred until runtime (which is when you actually find out how much data
you''ve got!).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.



Ken wrote:

In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understanding is that static memory allocation like using array is
faster than malloc, but dynamic memory allocation is more flexible.

Ho, Ho, Ho, Hold the homework!

Tom


Ken wrote:

In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understanding is that static memory allocation like using array is
faster than malloc, but dynamic memory allocation is more flexible.

Use static allocation when you know in advance the amount of memory
needed, or if dynamic allocation is not available, (as may happen in
some embedded systems). Use of auto variables, (a form of static
allocation, though in reality it may not be), also helps in data
encapsulation and automatic memory management.

Dynamic memory is particularly relevant when you anticipate growth in
your data structures, where you may only know the maximum needed
memory, but actual usage may be far less than that, and when you need
for the data to persist throughout the process''s lifetime but you
don''t, (rightly), want to use global memory for the purpose.


这篇关于静态内存分配与动态内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆