Static explanation in C/

Source: Internet
Author: User

static explanation in C/

http://blog.csdn.net/lwbeyond/article/details/6184035

I. Storage of the program.

Historically, the C program has been composed of the following parts:

1. Body Segment
The part of the machine instruction that the CPU executes. Normally, the body segment is shareable, so even a program that is executed by a regular environment pointer environment string (such as a text editor, C compiler, S H e L, etc.) can only have one copy in memory, and the body segment is often read-only to prevent the program from modifying its own instructions due to an accident.

2. Initializing data segments
This segment is often referred to as a data segment, which contains variables that need to be assigned an initial value in the program. The initialized global variables and static variables are stored here. For example, a description outside any function in the C program: int maxcount = 99; Causes this variable to be stored in the initialization data segment with an initial value.
A. Initialized global variables
B. Initializing a static variable

3. Non-initialized data segments
This segment is often referred to as the BSS segment, which is derived from an operator of the early assembler, meaning "block started by symbol (blocks starting with symbols)" , where uninitialized global variables and static variables are stored. The kernel initializes this segment to 0 before the program begins execution. Description outside the function: Long sum[1000]; Causes this variable to be stored in a non-initialized data segment.
A. Uninitialized global variables
B. Uninitialized static variables

4. Heap
Release management needs to be assigned by the programmer, and if the programmer does not release it, it may be recycled by the OS at the end. Dynamic storage allocations are typically performed in the heap.
Functions such as malloc, Calloc, realloc, etc. in the program are assigned from this side. The heap is assigned from the bottom up.

5. Stack
Release management is automatically assigned by the compiler. Local variables and the return address of each function call, and the caller's environment information (for example, some machine registers) are stored in the stack. The newly called function allocates storage space for its automatic and temporary variables on the stack. By using the stack in this way, the C function can be called recursively. The recursive function uses a new stack frame each time it calls itself, so that the set of variables in one instance of a function call does not affect the variables in another instance of the function call.
A. Local variables
B. Returning an address when a function is called
C. Caller's environment information (for example, some machine registers)

Static in the second and C languages.

1. Static local Variables

Static local variables belong to static storage, which has the following characteristics:

(1) A static local variable defines its lifetime as the entire program life cycle within a function, but its scope is still the same as an automatic variable, which can only be used within the function that defines the variable. After exiting the function, it cannot be used even though the variable continues to exist.

(2) If a static local variable of the basic type is not assigned an initial value at the time of declaration, the system automatically assigns 0 values. The value of the automatic variable is indeterminate if it is not assigned the initial values.

  1. void Test_static ()
  2. {
  3. static int Temp = 1;
  4. temp++;
  5. printf ("temp is:%d/n", temp);
  6. }
  7. int main (int argc,char *argv[])
  8. {
  9. int i=0;
  10. For (i=0;i<=4;i++)
  11. {
  12. Test_static ();
  13. }
  14. System ("pause");
  15. }

In fact, the static int Temp = 1; This sentence will only be executed at the first call.

2. Static global variables
A static global variable is formed when the description of the global variable is preceded by static.

(1). The scope of a non-static global variable is the entire source program, which is valid in each source file.

(2). While a static global variable is valid only within the source file that defines the variable, it cannot be used in other source files of the same source program.

The advantage of using static global variables is that you can avoid causing errors in other source files.

3. Static function
To define a static function, simply add a "static" keyword before the function type, as follows:

static int fun (int xxx)
{......}
The meaning of "static" here means that the scope of the function is limited to this file.

The advantage of using static functions is that when different people write different functions, you don't have to worry about the functions you define, or whether they have the same name as the functions in other files, because the same name doesn't matter.


The static keyword in C + + (the static keyword in the Class)

1. Static data member

Precede the declaration of a data member within a class with the keyword static, which is a static data member within the class.

 

  1. #include <iostream.h>
  2. Class Myclass
  3. {
  4. Public
  5. Myclass (int A,int b,int c);
  6. void Getsum ();
  7. Private
  8. int a,b,c;
  9. static int Sum; //Declare static data members
  10. };
  11. int myclass::sum=0; //define and initialize static data members
  12. static int myclass::sum = 0; Note that adding static is wrong.
  13. Myclass::myclass (int A,int b,int c)
  14. {
  15. this->a=a;
  16. this->b=b;
  17. this->c=c;
  18. Sum+=a+b+c;
  19. }
  20. void Myclass::getsum ()
  21. {
  22. cout<<"sum=" <<Sum<<endl;
  23. }
  24. void Main ()
  25. {
  26. Myclass M (a);
  27. M.getsum (); //cout 6
  28. Myclass N (4,5,6);
  29. N.getsum (); //cout
  30. M.getsum (); //cout
  31. }

As you can see, static data members have the following characteristics:

(1). For non-static data members, each class object has its own copy. Static data members are treated as members of the class. Regardless of how many objects of this class are defined, static data members have only one copy in the program and are shared by all objects of that type.


(2). Static data members are stored in the global data area. A static data member is defined only when space is allocated, so it cannot be defined in a class declaration.

In the example above, the statement int myclass::sum = 0; is to define static data members;


(3). Static data members conform to public, protected, private access rules, as well as ordinary data members, except for definitions, which define no access rules.


(4). Because static data members allocate memory in the global data area, all objects of this class are shared, so it does not belong to a particular class object, and its scope is visible when no class object is produced, that is, when there is no instance of the class, we can manipulate it;


(5). Static data member initialization differs from general data member initialization. The static data member is initialized in the following format:


    1. Data type >< class name >::< static data member name >=< value
    2. int myclass::sum=0;


(6). Static data members of a class have two types of access:

    1. Class object name >.< static data member name >
    2. M.sum = 0
    3. Class type name >::< static data member name >
    4. Myclass::sum = 0

But the above example is not possible, because he is a private variable, if the static data members access permission (that is, public members), in the program, in the format mentioned above to reference static data members;

(7). Static data members are used primarily when each object has the same property. For example, for a deposit class, the interest is the same for each instance. Therefore, interest should be set as a static data member of the deposit class. There are two benefits, first, regardless of how many deposit class objects are defined, the interest data members share the memory allocated in the global data area, thus saving storage space. Second, once the interest needs to be changed, the interest of all the deposit classes will change as soon as it is changed;


(8). Using static data members has two advantages over global variables:
A. Static data members do not enter the program's global namespace, so there is no possibility of conflicts with other global names in the program;
B. Information hiding can be implemented. A static data member can be a private member, while a global variable cannot;

2. Static member function
The static member function, which serves all objects of a class instead of a specific object for a class. A normal member function typically implies a this pointer, but a static member function does not have the this pointer because it is not associated with any object. In this sense, it cannot access the No-static data member that belongs to the class object, nor can it access the No-static member function, which can only invoke the rest of the static member functions.

  1. #include <iostream.h>
  2. Class Myclass
  3. {
  4. Public
  5. Myclass (int A,int b,int c);
  6. static void Getsum ();
  7. Private
  8. int a,b,c;
  9. static int Sum; //Declare static data members
  10. };
  11. int myclass::sum = 0; //define and initialize static data members
  12. Myclass::myclass (int A,int b,int c)
  13. {
  14. this->a=a;
  15. this->b=b;
  16. this->c=c;
  17. Sum+=a+b+c; //non-static member functions can access static data members
  18. }
  19. static void Myclass::getsum () {...}//plus static is wrong
  20. void Myclass::getsum () //static member function implementation
  21. {
  22. //cout<<a<<endl;//error code, a non-static data member
  23. cout<<"sum=" <<Sum<<endl; //Static functions are able to access static data members
  24. }
  25. void Main ()
  26. {
  27. Myclass M (a);
  28. M.getsum ();
  29. Myclass N (4,5,6);
  30. N.getsum ();
  31. Myclass::getsum ();
  32. }

For static member functions, you can summarize the following points:

(1). In addition to the class body, the static function cannot be added with the keyword static before it is defined;


(2). Static members can be accessed from each other, including static member functions accessing static data members and accessing static member functions;


(3). Static member functions and static data members can be accessed arbitrarily by non-static member functions;


(4). Static member functions cannot access non-static member functions and non-static data members, and can only access static;


(5). Because there is no additional overhead for this pointer, the static member function has a slight increase in speed compared to the global function of the class;


(6). Call the static member function, which can be accessed with the member access operator (.) and (a) or call a static member function directly with the class name


    1. M.getsum ();
    2. Myclass::getsum (); invokes the static member function of the class.

However, you should follow the Public,protected,private access rules as well.

Appendix:

The above is a summary of my work, not very whole, but it is easy to see.

Static explanation in C/

Related Article

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.