A summary of the static keyword from C to CPP needs to be sorted down!!!!!!!!!!!!!!!!!!!!!!

Source: Internet
Author: User
Tags define local

A complete program, in-memory distribution is as follows: Specific distribution map READ to!!!!!
1. Stack area: Automatically allocated by the compiler release, such as local variables, function parameters, are in the stack area. Frees up space as it functions to exit.
3. Heap area: Areas allocated and released by programmers, like malloc (c), new (C + +)
3. Global Data Zone (static zone): Global variables and static storage are placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another area adjacent. The program ends the release.
4. Code Area

There are three (specifically two) uses of the static keyword for C language:

1. Static local variables: used in the function body interior modification variables, the lifetime of such a variable longer than the function.

2. Static global variable: defined in the function body, used to decorate the global variable, indicating that the variable is visible only in this file.

3. Static functions: Static functions act like static global variables:
I. Static functions cannot be used by other files;
Two. The function of the same name can be defined in other files, and no conflict will occur;

==========================================================================

: In C, static mainly defines global static variables, defines local static variables, defines static functions

Define a global static variable: precede the global variable with the keyword static, and the global variable becomes a global static variable. Global static variables have the following characteristics:

(1) Allocating memory within the global data area

(2) If not initialized, its default value is 0

(3) The variable is visible in this file from the beginning of the definition to the end of the file

Second, define local static variables: The local static variable is preceded by the keyword static, the local variable becomes a static local variable. Static local variables have the following characteristics:

(1) The variable allocates memory in the global data area

(2) If initialization is not displayed, it is implicitly initialized to 0

(3) It always resides in the global data area until the end of the program run

(4) Its scope is a local scope, and when the function or block of statements defining it ends, its scope ends.

Define static functions: The function is defined as a static function when the return type of the function is added to the static keyword. Static functions have the following characteristics:

(1) The static function can only be used in the source file

(2) The inline function declared in the file scope defaults to static

Description: A static function is only a normal global function, but is limited by static, and can only be used within the compilation unit where the file resides, and cannot be used within other compilation units.

Extension of C + + to static
1) The static decorated member variables in a class belong to a class that can be shared, equivalent to a global variable, not a specific instance or object, that is, when an instance of a class modifies the static member variable, its modified value is seen by all other instances of the class, and must be initialized outside the class. Static member variable Access form: 1° can be accessed by the object 2° can be accessed by the class 3° can be accessed by the object pointer
2) The normal function has this pointer, the pointer can access all members, but the static member function in the class is owned by the entire class, and this function does not receive the this pointer, so only static member variables of the class can be accessed. Static can only be added when declaring a function, and can no longer be defined (declared in class, out-of-class initialization)

There are two new roles in the C + + language: Defining static data members or static function members

(1) define static data members. Static data members have the following characteristics:

(1) Memory allocation: In the program's Global data area assignment

(2) initialization and definition: Static data member definitions are allocated space, so they cannot be defined in a class declaration

(3) static member function. A static member function is associated with a class and is not associated with the object of the class. Static member functions cannot access non-static data members. The reason is simple, non-static data members belong to a particular class instance and are primarily used for operations on static data members.

(4) Neither the static member function nor the static data member have the this pointer.

1. Static members can access each other, including static member functions to access static data members and access static member functions;
2. Static member functions and static data members can be accessed arbitrarily by non-static member functions;
3. Static member functions cannot access non-static member functions and non-static data members;
4. Call the static member function, which can be accessed with the member access operator (.) and (-) call a static member function for an object of a class or pointer to a class object, or you can call it with the class name:: Function name (because he belongs to the class, it is normal to call with the class name)
---------------------

The 1 static modified variable precedes the object, so the static modified variable is initialized outside of the class. Because static is something shared by all objects, it must exist before the object.

2 member variables decorated with static are not memory-occupied in the object because he is not generated in the heap or stack with the object, and static-modified variables are generated in the quiescent store, so the advantage of static modification is that it can save the object's memory space.

3 Because a static decorated class member belongs to a class and does not belong to an object, the static class member function has no this pointer, and the this pointer is a pointer to this object. Because there is no this pointer, the static class member function cannot access a non-static class member and can access only the static decorated class member.

A summary of the static keyword from C to CPP needs to be sorted down!!!!!!!!!!!!!!!!!!!!!!

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.