The 1th case of the--static keyword three kinds of functions

Source: Internet
Author: User

The term static has an unusual history. At first, the keyword static was introduced in C in order to represent a local variable that still exists after exiting a block. Then, Static has a second meaning in C: the global variables and functions that represent the inability to be accessed by other files. To avoid introducing new keywords, the static keyword is still used to represent this second meaning. Finally, C + + reuses this keyword and gives it a different third meaning from the previous one: the variables and functions that represent any particular object belonging to a class other than this class.


The first function:

The program initially allocates space for static variables, frees up space at the end, defaults to 0, and can change its value when used.

Demo001_001.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <windows.h>int fun (void) {static int count = 10;//The program begins to initialize once, then no longer executes, and the scope is inside the fun function, The prestigious cycle is the entire program return count--;}            int count; Global variables, and static count is not the same variable, default initialized to 0int _tmain (int argc, _tchar* argv[]) {printf ("global\t\tlocal static\n"); for (; count <= 10; ++count) printf ("%d\t\t%d\n", Count, Fun ()); System ("pause"); return 0;}


The second function:

A static variable or static function can only access it at its scope, and its name is not visible in other files.

Why is the count and global variable count with the same name in the first action, the entire life cycle, but no conflict? This is the second function of static, hidden.

// stdafx.cpp :  only include source files for standard include files #include  "stdafx.h" static int number = 100;          // Global static variable, scope is current file int count = 100;                   //global variable, scope is the entire project Void show ()                         // Global function, scope is the entire project {printf ("6666666666\n");} 
Demo001_001.cpp: Defines the entry point of the console application.                #include "stdafx.h" #include <windows.h>extern int count;              Statement, extern key second regular meeting description extern void Show ();               Declaration, extern key second regular meeting description extern int number; Error, number can only be accessed in stdafx.cpp int _tmain (int argc, _tchar* argv[]) {printf ("%d\n", count); show (); System ("pause"); re Turn 0;}

Static hides variables and functions from their scopes, outside access.


The third function:

You can use static member variables and static functions by using classes directly, without requiring specific objects.

static void Show () {}; The static method of the class can be called directly by the class Democlass::show ();


Expand:


C, c + + memory allocation method can be divided into three kinds:

(1) Allocation from a static storage area:

Memory is already allocated at the time of program compilation, and the entire running period of the program exists in this block. Fast and error-prone because the system will clean up. such as global variables, static variables, and so on.

(2) Allocate on stack:

When executing a function, the storage units of the local variables within the function are created on the stack and are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.

(3) Allocation from the heap:

That is, dynamic memory allocation. The program uses malloc or new to request any size of memory at run time, and the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by the programmer and is very flexible to use. If space is allocated on the heap, it is the responsibility to reclaim it, otherwise the running program will have a memory leak, and allocating and releasing the heap space of different sizes frequently will result in heap fragments.

A C, C + + program compile-time memory is generally considered to be divided into 5 large storage areas: Heap area, stack area, global zone, literal constant area, program code area and so on.



Differences between global variables, static global variables, static local variables, and local variables

Variables can be divided into: global variables, static global variables, static local variables, and local variables.

(1) According to the storage area, global variables, static global variables and static local variables are stored in the static storage area of memory, local variables are stored in the memory stack area.


(2) by scope, the global variables are valid throughout the project file; a static global variable is only valid within the file that defines it; a static local variable is only valid within the function that defines it, except that the program allocates only one memory, and the variable does not disappear after the function returns, and the local variable is valid within the function that defines it. But the function returns and then fails.


A static global variable is formed by the description of the global variable (external variable), preceded by static. Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the whole source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.


from the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use.


This article is from the "very blog" blog, make sure to keep this source http://10093949.blog.51cto.com/10083949/1714738

1th Example of the--static keyword three effects

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.