Static variables and static functions in C ++

Source: Internet
Author: User

 

C LanguageMemory is required to store data. The memory is mainly divided into two types: static storage area and dynamic storage area;

 

1.Static storage ZoneIt can be divided into read-only data, RW data, and uninitialized data (BSS ). They are all inProgramThe compilation connection phase is determined and will not change during the program execution phase.

 

2.Dynamic Storage ZoneIt can be divided into heap and stack. They are dynamically allocated during program execution, and the size also changes dynamically. From the perspective of memory management, the stack uses the linked list, while the stack uses the linear storage method.

 

Stack: The stack is advanced and backward. In actual operations, the stack memory can be full or empty stacks. When the stack is full, the current position of the stack pointer is the used stack region; in the case of empty stack, the current position of the stack pointer is not used in the stack area, so the operation sequence of the pointer and data is different in the two cases.
Full Stack: When the stack is full, the pointer is moved first, and the data is put in; when the stack is output, the pointer is moved;
Empty Stack: The data is first put into the stack and the pointer is moved. The output stack moves the pointer first to output data;

 

Notes for C:
1.Memory leakage: Apply for a piece of memory, but it has not been released, and the program has not been recycled. As a result, other programs cannot be used.
2.Wild pointer: Indicates that a memory pointer has been released free or realloc, but the pointer is still in use. To avoid the case of a wild pointer, set the memory pointer to null and determine whether the memory is null when the program is used. If it is null, the memory has been released, do not access the memory.
3.Illegal memory release: In principle, only the memory allocated by malloc (), calloc (), or realloc () and returned by the return value can be released. Otherwise, the memory released other than this is invalid. Even if there is a pointer that * P is malloc, then for p1 = P ++, free (P1) is also invalid, but free (p) does. It is also invalid to release the local variables in the function. In another case, releasing a heap memory twice is also an incorrect usage. Because the free () function cannot release unallocated heap memory. After the program uses free to release the memory, it should set the pointer to null, free a null address is no problem.

 

C:

Before the introduction, let's discuss the heap, stack, free storage area, global/static storage area, and constant storage area.
Heap: allocate with new and release with free
Free storage zone: malloc allocation and delete release
STACK: local variables and function parameters managed by the compiler.
Global/static storage area: stores static and global variables
Character constant area: memory for constant Storage
That is to say, the static modifier only means to tell the compiler that I am an internal function and do not use it casually.

1. static variable

The type description of static variables is static. Static variables belong to static storage, but not necessarily static variables. For example, an external variable is a static storage method, but not necessarily a static variable. It must be defined by the static variable before it can become a static external variable or a static global variable.

2. Static local variables
Static local variables are stored in static mode and have the following features:
(1) the lifetime of a static local variable is defined as the entire source program in the function, but its scope is still the same as that of an automatic variable. It can only be used within the function that defines the variable.After exiting the function, although the variable still existsBut cannot use it.

(2) An initial value such as an array can be assigned to the static local volume of the constructor class. If the initial value is not assigned, the system automatically assigns the value 0.
(3) If an initial value is not assigned to a static local variable of the basic type, the system automatically assigns the value 0. If the initial value is not assigned to the automatic variable, the value is not fixed. According to the characteristics of static local variables, it can be seen that it is a kind of lifetime for the entire source program. Although this function cannot be used after it is defined, it can continue to be used when the function is called again, and the value left after the previous call is saved. Therefore, when you call a function multiple times and require that the values of some variables be retained between calls, you can consider using static local variables. Although global variables can also achieve the above purpose, global variables sometimes cause unexpected side effects, so it is best to use local static variables.

3. Static global variables
The description of global variables (external variables) is preceded by static to form a static global variable. Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files,Non-static global variables are valid in each source file.. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided. From the above analysis, we can see that,After a local variable is changed to a static variable, its storage mode is changed, that is, its lifetime is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use.. Therefore, the description of static plays different roles in different places.

4. Static functions .....

Internal and external functions:When a source program is composed of multiple source files, the C language divides the function into internal functions and external functions based on whether the function can be called by functions in other source files.

(1) internal functions (also known as static functions)
A function defined in a source file can only be called by functions in this file, but cannot be called by functions in other files of the same program. This function is called an internal function. To define an internal function, you only need to add a "static" keyword before the function type, as shown below:
Static function name (function parameter table)
{......}
The keyword "static" is "static" in Chinese. Therefore, internal functions are also called static functions. However, the meaning of "static" here is not the storage method, but the scope of the function is limited to this file. The advantage of using internal functions is that when different people write different functions, you don't have to worry about whether your own defined functions will have the same name as the functions in other files, because the same name does not matter.

2. External Functions
External Function Definition: when defining a function, if the keyword "static" is not added or the keyword "extern" is appended, this function is an external function:
[Extern] function name (function parameter table)
{......}
When calling an external function, you need to describe it:
[Extern] function name of the function type (parameter type table) [, function name 2 (parameter type table 2)…];

[Case] external function application.
(1) file mainf. c
Main ()
{Extern void input (...), Process (...), Output (...);

Input (...); Process (...); Output (...);

}

(2) file subf1.c
......
Extern void input (......) /* Define external functions */
{......}

(3) file subf2.c
......
Extern void process (......) /* Define external functions */
{......}
(4) file subf3.c
......
Extern void output (......) /* Define external functions */
{......}

Static member variables and static member functions in C ++

(1) static data members of the class are created and initialized at Compilation: they exist before any object of the class is created and do not belong to any object, non-static class member variables belong to the object. A static data member of a class has only one copy, which is shared by all such objects. Note that static data members cannot be initialized in the class (for constant static class variables, it seems that they can be defined before the class or main () function, initialization can be placed in the class). Generally, it is initialized before the class and the main () function. The lack of time is initialized to 0. Static data members are used to define the public data of each object of the class, which is safer than global variables.

(2) static member functions of the class belong to the whole class and do not belong to an object, which is shared by all objects of the class. Static members can be defined as inline functions. Generally, static member functions are used to access static data members or global variables in the same class, instead of non-static members. to access non-static members, you need to use objects as parameters, access non-static members of the object through the object name. Static member functions can also be defined outside the class. Static modification is not allowed at this time. Why does a static member function exist? It is mainly used to access static data members before any object is created. Common functions cannot implement this function.

Use of C ++ static members and static member functions: Call format of static members: Class Name: static data member name, Object Name. static data member name, object pointer-> static data member, object reference. static data members (but there are very few public data members in the class. This section only discusses the syntax and does not consider data encapsulation in actual use ). Call format of static member functions: Class Name: static member function name, Object Name. static member function name, object pointer-> static member function name, object reference. static data member. A static member function does not have the this pointer because it is not associated with a specific object. We recommend that you use the "Class Name: static member function name" format for calling. In summary, in the case of objects, static member functions and static member variables can be called by calling common class member functions and common member variables. It can be seen from this that static member variables and static member functions should be called without creating any objects. For its application, see Singleton pattern in design patterns.

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.