Static usage in C Language

Source: Internet
Author: User

From: http://tech.ddvip.com/2009-02/1234594734108774.html

I. cProgramBucket Layout

C Programs are always composed of the following parts:

1) body segment-The machine instruction part executed by the CPU; a program has only one copy; read-only to prevent the program from modifying its instruction due to an accident;

2) initialize the data segment (Data Segment)-store all the global variables assigned with the initial value in the program.

3) Non-initialized data segment (BSS segment) -- no global variable is initialized in the program; the kernel initializes this segment to 0.

4) Stack-increment direction: top-down growth; automatic variables and information to be saved for each function call (return address and environment information ).

5) Heap-dynamic storage.

| ----------- |
|
| ----------- |
| Stack |
| ----------- |
|
|/|
|
|
|/|
|
| ----------- |
| Heap |
| ----------- |
| Not initialized |
| ----------- |
| Initialization |
| ----------- |
| Body segment |
| ----------- |

Ii. Static in process-oriented programming

1. Global static variables

When the keyword static is added before the global variable, the global variable is defined as a Global static variable.

1) location in the memory: static storage area (the static storage area exists throughout the Program)

2) initialization: uninitialized Global static variables will be automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless it is displayed as initialized)

3) Scope: Global static variables are invisible outside the file where they are declared. Accurately starts from the definition to the end of the file.

Benefits of defining global static variables:

<1> not accessed by other files. Modify

<2> variables with the same name can be used in other files without conflict.

2. Local static variables

When the keyword static is added before the local variable, the local variable is defined as a local static variable.

1) location in memory: static storage Zone

2) initialization: uninitialized Global static variables will be automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless it is displayed as initialized)

3) scope: the scope is still the local scope. When the function or statement block that defines it ends, the scope ends.

Note: When static is used to modify local variables, it changes the storage location of local variables from the original stack to the static storage zone. However, after leaving the scope, the local static variables are not destroyed, but still resident in the memory until the program ends, but we cannot access them any more.

When static is used to modify a global variable, it changes the scope of the global variable (invisible outside the declared file), but does not change its storage location, or in the static storage area.

3. Static Functions

When the keyword static is added before the return type of the function, the function is defined as a static number of functions.

Function definitions and declarations are extern by default, but static functions are only visible in the declared files and cannot be used by other files.

Benefits of defining static functions:

<1> functions with the same name can be defined in other files without conflict.

<2> static functions cannot be used by other files.

The storage specifiers auto, register, extern, and static correspond to two storage periods: Automatic Storage Period and static storage period.

Auto and register correspond to the automatic storage period. A variable with an automatic storage period is created when it enters the block where the variable is declared. It is stored during the block activity and withdrawn when it exits.

The extern and static keywords are used to describe variables and functions with a static storage period. Local variables declared with static have static storage duration or static range (static extent ). Although its value remains valid between function calls, its name visibility is limited to its local domain. The static local object is initialized for the first time when the program executes the declaration of the object.

Extended analysis:

The term static has an unusual history. At first, the keyword static was introduced in C to indicate that a local variable still exists after exiting a block. Static C has a second meaning: it is used to indicate global variables and functions that cannot be accessed by other files. To avoid introducing new keywords, the static keyword is still used to indicate the second meaning. Finally,

C ++ reused this keyword and gave it a third meaning different from the previous one: variables and functions that belong to a class rather than any specific objects of this class (the same as the meaning of this keyword in Java ).

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.