[ASM/C ++] Summary of static usage in C Language

Source: Internet
Author: User

Suppose a static bool g_test = false is defined in test. h;

If both test1.c and test2.c contain test. H, test1.c and test2.c generate two g_test copies respectively, and set g_test = true in test1.c, while the value of false in test2.c remains unchanged! Shit !!

I. C program storage space 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 accidents;
2) initialize the data segment (Data Segment) -- all the global values assigned to the initial values in the program
Variable
, Stored here.
3) Non-initialized data segment (BSS segment) -- no initialized global data segment in the programVariable
The kernel initializes this segment to 0.
4) Stack-growth direction: top-down growth; automaticVariable
And the information (return address and Environment Information) to be saved during each function call ).
5) Heap-dynamic storage.

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

Ii. process-oriented programmingStatic

1. Global staticVariable

In the globalVariable
Previously added keywordsStatic
, GlobalVariable
Is defined as a Global staticVariable
.

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

2) initialization: uninitialized Global staticVariable
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 staticVariable
When declaring his
File
Is invisible. From the definitionFile
End.

Let's take a look at the program about the scope:
// Teststatic1.c
Void display ();
Extern int N;
Int main ()
{
N = 20;
Printf ("% DN", N );
Display ();
Return 0;
}
 
// Teststatic2.c
Static
Int N; // defines global staticVariable
, Automatically initialized to 0, only in the localFile
Visible
Void display ()
{
N ++;
Printf ("% DN", N );
}

File
Both are compiled, but teststatic2.c
InVariable
N cannot be found. An error is returned.
 
Define Global staticVariable
Benefits:

<1> no otherFile
Accessed, modified

<2> othersFile
Can use the same nameVariable
.

2. Local staticVariable

In the localVariable
Previously added keywordsStatic
, LocalVariable
Is defined as a local staticVariable
.

1) location in memory: static storage Zone

2) initialization: uninitialized Global staticVariable
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 a local scope. When the function or statement block that defines it ends, the scope ends.

Note: WhenStatic
Used to modify the localVariable
It changes the localVariable
Storage location, from the original stack to static
Storage zone. But local staticVariable
After leaving the scope, it is not destroyed, but still resident in the memory until the end of the program, but we can no longer access it.

WhenStatic
Used to modify the globalVariable
It changes the globalVariable
Scope (in the declaration of hisFile
But it does not change its storage location, it is still in the static storage area.

3. Static Functions

Add a keyword before the return type of the function.Static
The function is defined as a static function.

The Function Definition and declaration are extern by default, but the static function is only declaring itsFile
And cannot beFile
Used.

For example:
// Teststatic1.c
Void display ();
Static
Void staticdis ();
Int main ()
{
Display ();
Staticdis ();
Required urn 0;
}
 
// Teststatic2.c
Void display ()
{
Staticdis ();
Printf ("Display () has been called N ");
}
 
Static
Void staticdis ()
{
Printf ("staticdis () has been calledn ");
}
 
File
The code is compiled separately, but the staticdis () function cannot be found during the connection, resulting in an error.
Not compiled yet. vc2003 reports that the static function staticdis in teststatic1.c has been declared but not defined. By imjacob

Benefits of defining static functions:

<1> othersFile
Function with the same name can be defined in, without conflict

<2> static functions cannot be replaced by other functions.File
Used.
 
Storage specifiers auto, register, extern,Static
, Corresponds to two storage periods: Automatic Storage Period and static storage period.
 
Auto and register correspond to the automatic storage period. With Automatic Storage PeriodVariable
DeclareVariable
The block is created when the block exists during the activity of the block and is revoked when the block is exited.

Keyword extern andStatic
Used to describeVariable
And functions. UseStatic
Declared partialVariable
With static storage duration (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.

BecauseStatic
Variable
To implement some specific functions.

1. Count statistics function

Declare a part of a functionVariable
, And setStatic
Type, as a counter, so that the function can be counted every time it is called. This is the best way to count the number of function calls, becauseVariable
It is closely related to the function, and the function may be called in multiple different places, so it is difficult to count from the caller's perspective. The Code is as follows:
 
Void count ();
Int main ()
{
Int I;
For (I = 1; I <= 3; I ++)
Count ();
Return 0;
}
Void count ()
{
 Static
Num = 0;
Num ++;
Printf ("I have been called % d", num, "timesn ");
}

Output result:
I have been called 1 times.
I have been called 2 times.
I have been called 3 times.

 

From: http://dream2fly.net/blog? Action = show & id = 75

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.