Review compilation principles (1)

Source: Internet
Author: User

After coding is complete, whether it can correctly achieve our expected goal, we must first compile the Code through the compiler, that is, our encoding is at least syntactically correct. The so-called correct syntax means that our code must comply with the syntax rules of the language in use. The essence is that the compiler can correctly parse the code to get the target code to be executed on the machine.

Sometimes, compiling is just the beginning, and we will find that the program results are often quite different from what we expected. Aside from the problems of the algorithm itself, the negligence of some programming details is enough to make us feel helpless. For example, problems with parameter passing, scope of variables, and returning local variables have always plagued us. Even if I have seen some related articles on the Internet to explain stack, stack, and other knowledge, I am still confused. It seems like it is so mysterious.

If we can understand how the compiler works, we will be given a higher level of understanding of these issues. Many puzzles and questions may be easily solved. Therefore, we may wish to take some time to review the compilation principles that seem to have been forgotten!

Compiler Memory Allocation Policy

Assume that the compiler obtains a storage area from the operating system for the compiled program to run. The storage area will be segmented to save:

1. generated target code

2. Static Data

3. Record the control stack of program calls

4. Heap

The information required for each execution of a program is managed in a continuous storage area, called activity records. It includes the following information fields: return value, real parameter, control chain (optional), access chain (optional), saved machine status, local data, and temporary data.

The return value is the value returned by the called program to the called program. The real parameter field stores the parameter values passed by the called function to the called function. The control chain points to the activity record of the caller; the access chain is used to apply non-local data in other activity records; it is used to arouse the state domain to save the state information of the machine before the program call, such as the value of the program counter and the value of various registers; the local data domain stores the data locally executed by the program, and the temporary data stores the temporary data such as the intermediate results of the calculation expression.

The compiler generally has three memory allocation policies:

1. Static allocation

2. stack allocation

3. Heap allocation

Static allocation: the storage space is determined when the variable is compiled, and the value of the static variable is bound to a fixed storage space, therefore, after each function call, the static variables locally stored in the function are still bound to the same storage unit. Even if the call is completed, the values of the local variables are still saved.

Stack-based allocation organizes the storage space into stacks and performs stack-based and out-of-stack operations as the program calls start and end. Each call pushes an activity record into the stack, and the stack pops up at the end of the call. Heap allocation separates continuous storage into blocks and is allocated when activity records or other objects are required. The block can be released in any order. Note that this part is not automatically released by the compiler and must be handled by the programmer.

After learning about the memory allocation method of the compiler, we can understand the causes of some common problems.

Returns a reference to a local variable.

During each call, the storage space of local variables is included in the activity records of each call. Since each call generates a new activity record into the stack, local variables are allocated to different storage units during each call. In addition, the storage space of all local variables will be released and the stored values will be deleted when the activity record is on the stack.

Parameter transfer (pass by value, pass by pointer, and pass by reference)

The differences between parameter passing methods are mainly based on whether the real parameter represents the left value, the right value, or the body of the real parameter itself (to be continued)

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/mwsong/archive/2006/08/05/1025239.aspx

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.