The main content of this blog post is ChengData Structure During sequential running, Including RuntimeProgramHow to allocate memory for different parts, implement memory for function calls,
It also introduces a unique feature of C, a technology called "unwinding stack ".
The data structure during runtime. spaces in the middle are specially set aside,
It can be considered as a State of program execution during the runtime, generally including the compile time and runtime, which all indicate a processing state.
Programming LanguageOne of the classic opposites isCodeAnd data. The difference between code and data can also be considered as the boundary between runtime and compilation. Most of the compiler's work is related to the translation of code. Most of the necessary data storage management operations are performed at runtime.
If you have used GCC, you will know that you will get a file named "A. Out" by default when compiling a program with GCC.
Let's briefly describe "A. Out:
He is the abbreviation of assembler output. However,It is not the output of the assembler, but the output of the linker.
This name was once interpreted as "the new program is ready for execution" and is the output file of the linker.
Generally, we can think that the connector outputs a binary file. This file is not in disorder, but is stored in a certain pattern. For example, classification storage involves the concept of segments that we will discuss next.
Segment
Both the target file and the executable file can have different formats. All these formats have the concept of segments ).
As far as the target file is concerned, the segment is a simple area of the binary file, which stores all information related to a specific type. The term section is also widely used,
It can be seen as a part of a segment. A segment can usually contain several sections.
However, the segments here must be different from those in the memory model. In the memory model, segments are the results of the memory model design..
See the section composition:
It can be seen that a. Out contains magic number, which can be understood as a identifier. Generally, it is a special number. The so-called special number is of special significance, such
# Define fs_msgic 0x011954 it is the birthday of Kirk mckusick .~ So you don't need to pay too much attention here.
The following are other contents of A. Out, such as some identifiers .. Other content is described below.
Operating system operations on segments:
Segments can be easily mapped to objects that can be directly loaded by the linker at runtime. The loader only extracts the impression of each end and puts them directly into the memory.
Essentially, an end is a memory area in the program in the execution process.
Text Segment (the text segment)Contains program commands. The linker directly copies the commands from the file to the memory and manages them later, because in general, the text area will not change, whether it is the size or content.
The Data Segment)Including initialized global variables, static variables, and their values. The BSS segment contains uninitialized data. The size can be obtained from the executable file, and the linker obtains the memory block of this size.
After several segments, data segments and BSS segments are generally referred to as data zones. This is because the segment in the operating system is a continuous address, so adjacent ends are combined. Generally, data segments are stored in any
Which is the largest segment.
The stack segment)Displays the memory layout of a process to be executed. We still need some storage space to store temporary variables, temporary data, parameters passed to the function, and so on (local variables, temporaries, parameter passing in function CILS ,).
Note that the lowest part of the virtual address space is not mapped. It is located in the address space, but is assigned a physical address, so any reference to it is illegal. It is used to capture situations where the reference memory is created using NULL pointers and small integer values.
When you take shared libraries into account, a process address space appears,
The address space of a process is shown in the following figure:
C operation on a. Out
What the C Runtime does with your A. Out
Now let's take a look.C LanguageWhat is the data structure during running? There are usually several data structures during running, such as stacks, activity records, data, (the stack, activation records, data, heap) stacks, etc.
The following sections discuss and analyze their supported language features:
The stack segment:
The stack segment contains a single data structure: Stack.
The stack provides storage space for the local variables declared inside the function.
When a function is called, The stack stores maintenance information. This information is referred to as the stack frame, also known as The precedure activation record, which will be discussed later.
A stack can also be used as a temporary storage area. Sometimes a process requires some temporary storage space. For example, when performing a complex computing, the result can be pushed to the stack.
It is worth mentioning that, apart from recursive calls, stacks are not required.
How is a function called: precedure activation record)
What happens when a function gets called
How does the system manage programs in its own address space during C runtime? Here is a simple discussion.
C language automatically provides a function calledThe keeping track of the call chain records which functions are called, and after the return is executed, controls where the return is.
The classic mechanism for solving this problem is the process activity record in the stack. Each function call generates a process record. In fact, it is a data structure that records all the information required by the call point after the call.
For example, the structure of a process activity record may vary with compilers, but the purpose is to record the information of the call point returned after the call.
Astonishing C fact!
The shocking fact in C:
Currently, most programming languages allow you to define functions within a function,However, all functions in C language are at the top of this level.
This restriction slightly simplifies the C compiler. For the previous function that can be defined internally (that is, the nested process language), the process activity record should include a pointer to the outer activity record. This
A pointer is called a static link. It allows an inner process to access outer activity records, so it can also access outer local data. This type of access is called an upper-layer reference.
The following example shows that the difference between program execution and the activities recorded in the stack process.
Static and auto keywords
Why can't I return a pointer to a local variable in the function?
Char * favorite_fruit (){
Char deciduous [] = "apple ";
Return deciduous;
}
When you enter the function, the variable deciduous is automatically allocated space in the stack. When the function ends, the variable does not exist because the space occupied by it is recycled by the stack. It may be overwritten at any time.
The returned Pointer Points to an uncertain stack space, and the pointer loses its validity. It is called a hanging pointer.
If you want to repent of a pointer defined within the function, declare it as static. Static declaration allocates space in the data segment rather than in the stack segment. It is still valid when the defined variable exits the function.
Still exist.
The storage Type Auto is basically unavailable because the default statement is auto. He indicates that "automatically allocate memory after entering this block" is the default distribution of data in the function.
Setjmp and longjmp
Now we will briefly discuss the use of sejmp and longjmp, which are implemented through activity records during the operation process. It is a powerful mechanism unique to C. Partially makes up for the limited transfer capability of C language. These two functions work collaboratively.
• Setjmp (jmp_buf J) must be called first. It says use the variable J to remember where
You are now. Return 0 from the call.
• Longjmp (jmp_buf J, int I) can then be called. It says go back to the place that
The J is remembering. Make it look like you're returning from the original setjmp (),
Return the value of I so the code can tell when you actually got back here via longjmp ().
Phew!
• The contents of the J are destroyed when it is used in a longjmp ().
•Setjmp (jmp_buf J) must be called first. It uses the variable J to record the current position and the function returns 0;
Longjmp (jmp_buf J, int I) can be called to return to the position recorded by J ", let the program look like nothing has happened. Return I to let the code know that it is actually returned through longjmp
When longjmp () is used, the content of J is destroyed.
Setjmp saves a program counter and pointer to the top of the current stack, and can save some initial values. Longjmp is returned to the place set by setjmp, which effectively controls the transfer and resets the status to the saved status.
This is called"Expand Stack"Because you expand the activity record from the stack until you get the value saved in it.
It differs from the GOTO statement:
A goto can't jump out of the current function in C (that's why this is a "longjmp"-you can
Jump a long way away, even to a function in a different file ).
You can only longjmp back to somewhere you have already been, where you did a setjmp,
And that still has a live activation record. In this respect, setjmp is more like a "come from"
Statement than a "go to". longjmp takes an additional integer argument that is passed back,
And lets you figure out whether you got here from longjmp or from carrying on from
Previous statement.
The GOTO statement cannot jump out of the current function of the C language.
Longjmp can only return to a place where it was previously visited (the place set by setjmp)
The following is an example:
# Include <setjmp. h>
Jmp_buf Buf;
# Include <setjmp. h>
Banana (){
Printf (" In banana () \ n " );
Longjmp (BUF, 1 );
/* Notreached */
Printf ( " You'll never see this, Because I longjmp 'd " );
}
Main ()
{
If (Setjmp (BUF ))
Printf ( " Back in main \ n " );
Else {
Printf ( " First time through \ n " );
Banana ();
}
}
Output result:
% A. Out
First time through
In banana ()
Back in main
Setjmp/longjmp is used to recover errors. Once an unrecoverable error is found, it can be transferred to the main input loop.
Hope to communicate and learn with more friends
Let's write so much. What's wrong? I hope you can point it out ~
Reference Expert C Programming
Http://en.wikipedia.org/wiki/Setjmp.h
Http://en.wikipedia.org/wiki/A.out
Reprinted please indicate the source: http://www.cnblogs.com/yanlingyin/
A fish ~ @ Blog site