Static
#include <stdio.h>#include<iostream>#include<assert.h>using namespacestd;Static intA= -;voidfunc () {Static intA=Ten; A++; cout<<a<<Endl;}intMain () { for(intI=0; i<Ten; i++) func (); A++; cout<<a<<Endl;}
#include <stdio.h>#include<iostream>#include<assert.h>using namespace std; Static int a=; void func () { a+ +;} int Main () {
Func (); A+ +; cout<<a<<Endl;}
#include <iostream>using namespacestd;classa{ Public: A () {Updatecount ();} Static intShowcount; voidUpdatecount () {showcount++;} Static voidPrintCount () {cout<<showcount<<Endl;}Private:};intA::showcount=0;intMain () {A::p rintcount (); A A1; A A2; A::p rintcount ();}
Joint:
Union (Union) Introduction
#include <stdio.h> #include <iostream> #include <assert.h>using namespace std;union test{ int A; Char b; Short C;}; int main () { Test T; t.a=10; t.c=20; cout<<t.a<<endl; cout<<t.c<<endl; Cout<<sizeof (test) <<endl;}
Const
Read-only
Register
Register variables , which require the compiler to store the value of a variable in the CPU register instead of memory, making the operation on the register variable faster than the normal memory variable.
Note : The local and formal parameters can only be modified with register, and in c The Register variable address cannot be taken from the & operator, and the address of the register variable in C + + may prevent it from being optimized.
extern
extern can be placed variables or functions ago, to define a variable or function in another file. , prompts the compiler to find its definition in other modules when encountering this variable and function
mutable
In C + +, mutable is set to break through the limitations of Const. Variables modified by mutable will always be in a mutable state, even if the struct variable or class object is const in a const function, and its mutable members can be modified.
struct st{ int A; int b;}; Const ST st={1,2};st.a=one; // Compile Error st.b=; // Allow
Volatile
Volatile modified data,"direct access to the original memory address", the compiler is not allowed to perform the execution of the register to register the optimization. This feature is designed to meet the special needs of multi-threaded synchronization, interruption, hardware programming and so on. When you encounter a variable declared by this keyword, the compiler will no longer optimize the code that accesses the variable, providing direct access to the special address.
Generally, volatile is used in several places:
1, the change in the Interrupt service program for other programs to detect variables need to add volatile;
2, multi-tasking environment to share the logo should be added volatile;
3, Memory mapping hardware register usually also add volatile description, because each time it read and write may have different meanings
The memory used by a program compiled by C + + is divided into the following sections.
- Stack: The compiler automatically allocates releases, stores the function's parameter values, the values of local variables, and so on. It operates in a manner similar to a stack in a data structure.
- Heap: Allocated by the programmer, if the programmer does not release, the program may end up being reclaimed by the operating system. Note that it is not the same as the heap in the data structure, but the distribution is similar to the linked list.
- Global/static zone (Static): The storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another area adjacent. Released by the system after the program is finished.
- Literal constant: The constant string is placed here and released by the system at the end of the program.
- Program code area: binary code that holds the body of the function.
int a = 0;//global initialization zone char *p1;//global uninitialized zone void main () { int b;//Stack char s[] = "abc";//Stack, runtime assignment Char *p2;//stack char *p3 = "123456";//123456\0 in the constant area, p3 on the stack. Compile-time determination. p1 = (char *) malloc;//p1 points to the heap, p1 on the stack
pointer identification;
int * p;//pointer to int type void * p;//NULL type pointer int * arr[10];//pointer array x holds 10 pointers to int type int** pp;// Pointer to pointer (pointer to int type) int (*func_p) (int,int);//function pointer
Note: In C You can directly assign the void* pointer to any other type of pointer, whereas in C + + you need to use a forced type conversion.
Using pointers to pass memory
voidGetMemory1 (Char*p) {P=(Char*) malloc ( -);//p is a local variable}Char*GetMemory2 () {Charch[]="Hello World";//allocate memory on the stack, function ends reclaim memory, CH will be a wild pointer returnch;}voidGetMemory3 (Char**p) { *p= (Char*) malloc ( -);//correct}Char*GetMemory4 () {Char*p; P=(Char*) malloc ( -);//correct returnp;}
extern can be placed variables or functions ago, to define a variable or function in another file. , prompts the compiler to find its definition in other modules when encountering this variable and function
C + + interview knowledge points