静态分配 vs. 动态分配 vs. 自动分配 [英] Static allocation vs. Dynamic allocation vs. Automatic allocation

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

问题描述

静态分配、动态分配和自动分配有什么区别?

What are the differences among static, dynamic, and automatic allocation?

推荐答案

会有特定语言的细节,但总体思路是:

There will be language-specific details, but the general idea is:

  • 静态:在程序启动时分配,存在于程序的整个生命周期
  • 自动:在进入块时分配,在该块的持续时间内存在

动态分配需要更多的解释:当你分配它时它就被分配了(例如像'new XXX'这样的东西).在(大多数实现)C++ 中,它会一直存在,直到您明确删除它.对于大多数较新的语言(例如 Java、C#),它会一直存在,直到垃圾收集器确定它不再可访问,届时它会被自动销毁.

Dynamic allocation requires a bit more explanation: it's allocated when you allocate it (e.g. with something like 'new XXX'). In (most implementations of) C++, it'll exist until you explicitly delete it. With most newer languages (e.g. Java, C#) it'll exist until the garbage collector determines that it's no longer accessible, at which time it'll be destroyed automatically.

并非所有语言都具有所有三种分配形式.在某些情况下(例如 Java),即使支持一种分配形式,也存在一些限制,例如允许对内置类型进行自动分配,但需要对对象类型(即类的实例)进行动态分配.

Not all languages have all three forms of allocation. In some cases (e.g. Java), even if a form of allocation is supported, there are restrictions such as allowing automatic allocation for built-in types, but requiring dynamic allocation for object types (i.e. instances of classes).

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

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