Three memory allocation modes

xiaoxiao2021-04-08  4

The system has three forms for process allocation data space.

Static allocation

The entire static allocation space, including all of the data entities, all assigned by the system when the process is created (simultaneous code allocation space for UNIX is called Text). This space remains unchanged during the process.

The initialized and unexpected entities are placed in the initialization data segment and the uninited data segment (BSS). The latter and the former are different, and there is no existence in the .o file a.out file (only the architecture information), in the virtual space of the process.

The extern variable and static variable are static allocation.

Do static allocation, assign a text (Text), data segment, and stack space when the process is created.

The body and initialization data are copied by A.out; the data is not initialized by the architecture, fill in 0 or empty; the size of the stack space is determined by the linker switch (forget which switch forgets).

Stack assignment

The entire stack space has been allocated when the process is created. The initial value of the stack pointer SP determines the size of the stack space. A switch for the linker can set the size of the stack space. During the process run, the size of the stack space is unchanged. However, when the process is just started, the stack space is empty, there is no entity inside. During the process run, the stack assignment of the specific entity is the process you generate (stack) and release (pop-up) entities, and the system is not involved.

The AUTO variable and function parameters are assigned by stack.

As long as the total length of the embedded entity does not exceed the stack space size, the stack assignment is independent of the system. If it is more than, the stack overflow error will be thrown.

Allocation

When the process needs to generate an entity, apply to the system to allocate space; if the entity is no longer needed, the system can be applied to the system to recycle this space.

Heap allocation uses specific functions (such as malloc (), etc.) or operators (New). The generated entity is anonymous and can only be accessed by a pointer.

For entities, stack allocation and heap allocation are dynamic allocation: entities are generated and disappeared in the process run. All entities that are static allocation are all distributed when the process is created, and there is always the run.

The same is dynamic allocation, stack allocation and heap allocation are very different. The former is allocated by the system when the process is created, and the entity is generated by stacking, and is canceled by pop-up. No matter whether an entity is generated, how many entities are generated, and the stack space always maintains the original size. The latter does not have a preset space. When an entity needs to be generated, it is only possible to accommodate this entity. When this entity is no longer required, you can apply to the system to recycle this space. Therefore, heap allocation is a real dynamic allocation.

Obviously, the spatial utilization rate of the heap is the highest.

Stack allocation and static allocation are also common: the whole space is assigned by the system when the process is created. However, the latter allocates the space of all entities, while the former is empty when the process starts. In addition, entities in the stack and the entities in the data segment are famous entities, which can be accessed by the identifier.

Static Distribution Stack Distribution Stack Allocation The process When the process is created, the process is created, and the process is used to generate the time process. The process runtime process runtime entity generator operating system process application / system implementation life Permanent temporary controllable Amoked / anonymous is famous anonymous access method to identify access to identification access can only be used by pointer access space can not be recycled

转载请注明原文地址: http://www.9cbs.com/read-132885.html

New Post(0)