Static allocation vs. Dynamic allocation vs. Automatic allocation

后端 未结 2 1610
渐次进展
渐次进展 2020-12-15 10:24

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

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 11:01

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

    • Static: allocated at program startup, exists for entire life of program
    • Automatic: allocated upon entry into a block, exists for duration of that block

    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.

    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).

提交回复
热议问题