static storage and Stack dynamic storage

Source: Internet
Author: User

Stack

This article is also supplements, Ya's, call you university not good to listen to, now still not obediently look. The tenth chapter is about the principle of self-compiling.

Storage organization when the target program is running

Logically, before the code is generated, the compiler must design the environment of the target program and allocate the data space. When the program is running, it needs to get a piece of memory to run on it, which needs to contain the target code and the data space of the target code runtime. Typically, this memory area is divided into: target area, static data area, stack area, heap area.

Static storage allocation

At compile time can determine the target program to run the size of all the data space, compile time to arrange the full data space of the Hao target program run, to determine the number of each data object storage position

Dynamic Storage Allocation

Dynamic storage management technology is required if a programming language allows recursive processes, variable groups, or allowing users to freely request and free space. Because this kind of program does not know at compile time how much storage space he needs to run, The size of the data space it needs needs to be determined dynamically when the program runs. There are two types of dynamic storage allocation: stack (stack) and stacked (heap)

Stack dynamic Storage allocation

This allocation strategy is to design the data space of the entire data as a stack. Whenever a procedure is called, the data space it needs is allocated at the top of the stack, and the space is freed whenever the work is finished. The data space required for a procedure consists of two parts: a data object that is part of the lifetime in this activity, such as a local variable, Parameter units, temporary variables, and so on; the other part is the record information that is used to manage the process activity. That is, when a procedure call occurs, the activity of the process that invokes the procedure is interrupted, the state information of the current machine, such as the program counter (return address). The value of the Register, etc.

When the control returns from the call, the machine state is restored according to the information recorded in the stack, so that the activity of the process continues.

Heap-type dynamic storage allocation

If a program language provides a mechanism for users to freely apply for data space and to return data space (such as new init), or if there is not only a process but also a process structure, the use of space may not necessarily take the "first application after release, after the application first release" principle, Then the dynamic allocation scheme of the stack is not applicable. A dynamic storage allocation scheme called a heap is often used. Assuming that the program allows for a large storage space, whenever necessary to borrow a piece from this space, when not to be refunded, due to the time of borrowing is different, after a period of operation, Program run space will be divided into many blocks, some occupied, some idle. Then when the running program requires a space of volume n, you need to decide which free block should be used to get the space. In theory, n cells should be removed from the free blocks that are slightly larger than N, in order to make large free chunks more useful, But the actual difficulty is very big, the actual method is often used: first encounter which block is larger than n from which to remove the N units. Even so, there will be no more than the free block of N, but the sum of all the free blocks is much larger than the case of N, at this time, some distribution management system using waste recovery method to cope with.

static storage and Stack dynamic storage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.