Storage class, scope, life cycle, link properties

Source: Internet
Author: User

1.linux C Language Program memory image code snippet (. Text), data segment (. data), BSS segment, stack, heap concept

Code snippet (. Text)

(1) corresponding to the code (function) in the program, the code snippet in Linux is also called the text segment (. Text)
(2) A const-modified variable under some platform.

Segment (. Data)

1, explicitly initialized to non-0 global variables;

2. Explicitly initialize to non-0 static local variable

BSS segment

1, explicitly initialized to 0 or not explicitly initialized global variables;

2. Static local variable that is explicitly initialized to 0 or not explicitly initialized.

Stack The local variables are allocated on the stack, and the function call pass-through process also uses the stack
Heap Manual application is required.
File Map Area The file map area is the process opened the file, the contents of this file from the hard disk read the file map of the process, and then directly in memory operation of the file, read and write after the memory of the files in the file to write to the hard disk.
Kernel map Area

(1) The kernel mapping area is to map the operating system kernel program to this area.
(2) for every process in Linux, it thinks that it is only itself and the kernel in the whole system. It thinks the memory address 0xc0000000 below is its own activity space, 0xc0000000 above is the OS kernel's activity space.
(3) Each process is living in its own separate process space, where each process is different (because virtual address technology is used), but the kernel is 0-3g.

2. Storage-Class-related keywords
(1) Describe the storage class keyword auto, static, register, where the focus is static.

The Auto keyword has only one function in the C language, which is to modify the local variables. The default is auto

Static two ways to use:

The first modified local variable is a static local variable.

1. Static local variables are the same as global variables in terms of storage classes. (Data segment or BSS segment)
2. Static local variables are the same as global variables in terms of life cycle. (Entire program)
3, the difference between static local variables and global variables is: scope, connection properties. The scope of a static local variable is the scope of the code block (the same as the normal local variable), the Link property is no connection, the scope of the global variable is the file scope (and the function is the same), the Link property aspect is an outer join.

The second is to modify the global variable, the function name. Indicates that it is useful only under the current file to avoid duplicate names. (The modified link becomes an inner link from an outer join)

Register: The variable compiler modified with register is assigned to the register preferentially.


(2) Describe storage class keywords extern, volatile, restrict, TypeDef, which focus on extern and volatile.

extern: Used in a declaration to indicate that the variable is defined in another file.

Volatile: A relatively sensitive amount that may be altered by the outside environment. With this modification, the compiler can avoid excessive optimizations.

typedef

3. Scope

1. code block scope for local variables

(1) A block of code can be understood as a pair of curly braces {} parts.
(2) The code block is not equal to the function because the IF and for have {}. So the code block <= function
(3) The scope of a local variable is the scope of the code block, which means that a local variable can be accessed and used only after the definition in the code block that defines the local variable.

2. File scopes for function names and global variables
(1) The scope of the file means that the global access rights, that is, the entire. c file can access these things. This is what you usually call local and global, the global is the file scope.
(2) Detailed and accurate: the scope of functions and global variables is the definition of the entire. c file after the definition of the part.

3. Masking rules for variables with the same name
(1) Problem: When programming, the variable with the same name will inevitably appear. A variable with the same name does not necessarily make an error.
(2) First, if two variables of the same name have different scopes and do not overlap, in this case the same name has no effect.
(3) Second, if two variables with the same name have overlapping scope, C language rules in the scope overlap range, the scope of a small variable will cloak the scope of the larger one (magistrate than the current tube).

4. Life cycle

The life cycle of a stack variable
(1) Local variables (stack variables) are stored on the stack, and the life cycle is temporary. Temporary means: The code executes as needed to create, use, and perish.
(2) For example, a local variable defined within a function is created once every time the function is called, then used, and finally dies when the function returns.

The life cycle of a heap variable
(1) First of all understand: heap memory space is objective, is maintained by the operating system. Our program just goes to apply then use then release.
(2) We only care about the time that our program uses heap memory, so the heap variable also has its own life cycle, which is: It was born from malloc and then used until free.

Life cycle of data segment and BSS segment variables
(1) The life cycle of a global variable is permanent. The permanent meaning is born when the program is executed, and dies when the program terminates.
(2) The memory occupied by the global variable cannot be freed by the program itself, so the program will always consume a lot of memory if it applies too many global variables.
(3) If heap memory is a library borrowed book, then the global variable is the book that you buy.

Life cycle of code snippets, read-only segments
(1) Actually is the code that executes the program, actually is the function, its life cycle is permanent. But the life cycle of the general code is not our concern.
(2) Sometimes it is not just code that is placed in the code snippet, but also constants of const type, as well as string constants. (const type constants, string constants are sometimes placed in the Rodata segment, sometimes in code snippets, depending on the platform)

5. Link Properties

1, the C language program's organization structure: multiple C files + multiple H files
(1) A large and complete C language program (such as the Linux kernel, uboot) consists of multiple C files and multiple H files.
(2) The process of generating the program is: Compile + link. Compiling is to change functions/variables into. o binary format of the machine code, linked to the separate binary functions to link together to form a whole binary executable program.
2, the compilation of files as a unit, linked to the project unit
(1) When the compiler is working, all the source files are read in turn, and the individual is compiled for the unit.
(2) The link is actually the first step to build a single. o File as a whole input, and then process the link into an executable program.
3, three kinds of link properties: Outer connection, inner link, no link
(1) The outer connection means that the external link property, that is, this guy can be in the entire program scope (meaning that can be cross-file) links, such as ordinary functions and global variables are outer joins.
(2) within the meaning of the link (c file) Internal link properties, that is, this guy can be in the current C file within the scope of the link (meaning is not in the current C file outside the other C file access, link). The static modified function/global variable belongs to the inner link.
(3) No connection means that the symbol itself does not participate in the link, it does not matter. All local variables (auto, static) are non-connected
4, function and global variable with the same name conflict (static decoration)
(1) Because functions and global variables are external link properties, that is, every function and global variable will be accessible to all C files in the program in the future, so a function with the same name or global variable with the same name cannot appear in all C files in a program.
(2) The simplest solution is not to repeat the name, but it is difficult to do so. The main reason is that a big project in the function and global variable name too many, and a big project is not a person to complete, is a lot of people collaborate to complete, so it is difficult to guarantee that the duplicate names. What about the solution?
(3) The perfect solution to this problem in modern high-level languages is the namespace namespace (in fact, a prefix that takes each level of a variable). But the C language is not so solved.
(4) C language relatively early encountered this problem, at that time did not invent the concept of namespace, at that time, C language invented a not very perfect but live can use the solution, is three kinds of link attribute method.
(5) C-Language Link properties solve the problem of the same name: we will obviously not reference in other C files (only in the current C file) of the function/global variables, using the static decoration to make it an inner link property, so that in future connections even if there are 2 C files in the same name of the function/global variables, As long as one or 2 of the inner link properties is OK.
(6) This solution solves the problem to some extent. But not fundamentally solve the problem, leaving a lot of trouble. So this led to the C language writing a very large project is very difficult.

5. Second usage of static: modifying global variables and functions
(1) Normal (non-static) function/global variable, the default Link property is external
(2) static (static) function/global variable, link property is internal link.

The final summary


(1) Normal (automatic) Local variables are allocated on the stack, scoped to the code block scope, the life cycle is temporary, and the Connection property is no connection. When defined without explicit initialization, its value is random, the variable address is allocated on the stack by the runtime, the address is not necessarily the same at multiple executions, and the function cannot return the address (pointer) of the class variable as the return value.
(2) Static local variable allocation in the data segment/BSS segment (explicitly initialized to not 0 in the data segment, explicitly initialized to 0 or not shown initialization is in the BSS segment), scope for the code block scope (artificially defined), the life cycle is permanent (natural), the link property is no connection (natural). When defined, the value is 0 (natural) if it is not explicitly initialized, and the variable address is determined by the runtime environment when the program is loaded, and the entire program is the only constant while the static local variable is a global variable scoped to the scope of the code block (while linking the property to no connection). Static local variables can be implemented with global variables instead (global variables are avoided in the program, as they can break the structure).
(3) The only difference between a static global variable/static function and a normal global variable/ordinary function is that static makes the link property of the global variable/function from the external link (the entire program all file scope) to the internal link (in the current C file). This is to solve the problem of duplicate names of global variables/functions (C language has no concept of namespace namespace, so the name of the global variable/function is very serious after the file becomes more numerous in the program, it is unnecessary to be referenced by other files to the global variable/ A function declared as static can greatly improve the problem of duplicate names, but it is still not completely resolved.
(4) write the program to avoid the use of global variables, especially non-static type of global variables. A global variable that can be determined not to be referenced by another file must be static decorated.
(5) Note the definition and declaration of the global variables. The general rule is as follows: If the definition is initialized at the same time, it will be considered a definition, and if it is defined but not initialized, it may be considered as a definition by the compiler, or it may be considered as a declaration, to be specifically analyzed, and if extern is to be considered a declaration (in fact, it can be defined with extern). In fact, extern is the explicit declaration that this variable is an external link property.
(6) A global variable should be defined in a C file and declared in a header file, and not in a header file (because if defined in a header file, the header file is defined repeatedly when it is contained by more than one C file).
(7) There are 2 ways to refer to the global variables/functions defined in A.C in B.C: One is to declare the function/global variable in A.H, and then in B.C # # <a.h>; II, use extern to explicitly declare the function/global variable to be referenced in B.C. The first of these methods is more formal.
(8) Storage class determines life cycle, scope determines link properties
(9) The Link property of the macro and inline functions is no connection.

Storage class, scope, life cycle, link properties

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.