The understanding of keywords in C + +--static

Source: Internet
Author: User

The understanding of keywords in C + +---Static

Contents: 1, Static origin

2. Static self-understanding

3, static example use

4. Summary

--------------------------------------------------------------------------------------------------

1.Static Origin

C language is a process-oriented architecture, multi-file-based coding is excellent in maintaining the different source files and functions of the mutual reference (that is, contact) and independent of each other, resulting in the extern and static keywords. And the process-oriented C and object-oriented C + + are inextricably linked, let alone C can be fully structured and simulated derivation of C + +, the second part of my own understanding will use a sentence to describe: The keyword static and C + + implicated. In addition, you need to know: Since the keyword static modifier is a variable/function, then where is this data?

---the layout of the process in memory:

The text segment in the keep is the binary data of the program; the. Data segment holds all the initialized global variables of the process; the. BSS (Block Started by Symbol) segment holds a chunk of memory for a global variable that is not initialized in the program, which belongs to the static memory allocation. In the life cycle of a process, the data in the. Database and. BSS segments coincide with the entire process.

2.static self-understanding

Static is similar to private protection in C + +, and the special attributes are left dead (program error) due to deep sea fish and water relationships.

C++private private protection (only activities within the class, can be called through member functions, not used outside the class);

Statically static global variable (valid only in the source file that defines it,--c/cpp, other source files cannot be accessed);

Someone's image describes a normal global variable put on a static coat, it becomes the bride, has the heart to belong to, can only be defined by its source file (groom) in the variable or function access.

3,static example use

1> global static variables

file1.h: Declaring child code

#include <stdio.h>void  printstr (); void PrintS ();
File1.h

File1.cpp defines a static global variable Hello, which can only be accessed by functions printstr and prints under File1.cpp

#include"File1.h"Static Char* Hello ="Hello cobing!";voidPrintstr () {printf ("%s\n", hello);}voidPrintS () {printf ("%s\n", hello); printf ("%s\n", hello);}
File1.cpp

file2.cpp refers to the definition function under File1.h Declaration and invoke file1.cpp to compare the ability to access static global variables in different source files

#include"file1.h"void  printfilestr () {    printf ("%s\ n", Hello);}
File2.cpp

the results proved to be impossible.

The main function in the Main.cpp Master file accesses the static global variable by calling the function under File1, which is the same as private in C + +

#include"file1.h"int  main () {    printstr ();    PrintS ();     return 0 ;}
View Code

The above function proves the private protection comparison of static global variables??!

2> local static variables

Local static variables VS common local variables

1 "Location: Static local variables are placed in the global store by the compiler. Data, so it is local, but exists throughout the lifetime of the program.

2 "Access rights: Static local variables can only be accessed by variables or functions within their scope. This means that although it will exist throughout the life of the program, because it is static, it cannot be accessed by other functions and source files.

3 "Value: If the static local variable is not initialized by the user, it will be automatically assigned a value of 0 by the compiler, and each time the static local variable is called with the value of the last call. This is a good understanding, each time the function calls static local variables are modified and then left, the next time you read from the global store read the static local variable is the last modified value.

-------A word, the private protection strength of static local variables further (╯▔ dish ▔) ╯ upgrade, on a static global basis is limited to: only in the same source file under the same function.

eg

File1.h with the above can.

/* Static local */ void   printstr () {    int0;     Static int 0; // static local Variables    printf ("normal =%d----stat =%d\n", Normal, stat);     Normal+ +;    Stat+ +;}
File1.cpp

The above direct application is wrong, proving that the private is stronger.

The above results show that the static local variables are used when the value of the last call (as if the memory power off, each on the original basis of action). But the normal local variable has been initialized for a state of 0;

3> static Functions

Reference link:http://blog.csdn.net/keyeagle/article/details/6708077/ written by the author is already very (??????)?? That's great!

4. Summary

In a word, the static is similar to C + + private;

The understanding of keywords in C + +--static

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.