Basic function of const
(1) Modify variables
C uses const to modify variables. The function is to declare variables as read-only and protect their values from modification. Examples are as follows:
const int i = 5;
Copy the code
The above example shows that variable I is read-only and cannot be changed; If you want to reassign I, say I = 10; Is wrong.
It is important to note that when a variable is defined, it must be initialized. The definition can also be written as int const I =5, which is also true.
In addition, const modifiers also save space. Usually, the compiler does not allocate space to ordinary const read-only variables. Instead, it stores them in a symbol table.
(2) Modify the array
Const can also modify arrays in C, as in the following example:
Const int array[5] = {1,2,3,4,5}; array[0] = array[0]+1; / / errorCopy the code
Array elements, like variables, have read-only properties that cannot be changed; Once changed, such as the program will report an error.
(3) Modify pointer
Const pointer (C); const pointer (C); const pointer (C) The other is to make the pointer immutable. Examples are as follows:
int i = 5; int j = 6; int k = 7; const int * p1 = &i; // define 1int * const p2 =&j; / / definition 2Copy the code
Two Pointers p1 and p2 are defined above.
In definition 1, const is limited to p1, that is, the value of the pointer to the space cannot be changed. If the value of the pointer to the space is changed, such as P1 =20, the program will report an error. However, the value of P1 can be changed, and there is no problem in reassigning p1, such as P1 =&k.
In definition 2, const is limited to p2. If p2=&k, an error will be reported. However, p2, the value of the space it points to, can be changed, such as P2 =80 is no problem, the program runs normally.
(4) Modify function parameters
The const keyword decorates a function parameter, qualifying it to prevent it from being modified inside the function. The qualified function parameters can be ordinary variables or pointer variables. Examples are as follows:
Void fun1(const int I){ i++; }void fun2(const int *p){void fun2(const int *p){ (*p)++; // p < span style = "box-sizing: border-box! Important; word-wrap: break-word! Important;Copy the code
Static basic functions
(1) When modifying a variable, the static local variable is initialized only once, and extends the life cycle of the local variable until the end of the program.
Extern/extern/extern/extern/extern/extern/extern/extern/extern/extern/extern/extern/extern/extern/extern/extern/extern
(3) Static refers to a function that can only be called from this file. Static variables are stored in the static variable area of the global data area. Both global and local static variables are allocated memory in the global data area. Automatically initialize to 0 when initialized.
(4) You don’t want to release static. For example, modify an array that is stored in stack space in a function. You can use the static modifier if you don’t want the array to be freed at the end of the function call.
(5) Data security considerations (applications that want to use global variables should consider using static first)
Static global variables have the following characteristics:
(1) Static variables are allocated memory in the global data area, including static local variables mentioned later;
(2) Uninitialized static global variables are automatically initialized to 0 by the program (the value of an automatic variable declared in a function body is random unless it is explicitly initialized, and automatic variables declared outside the function body are also initialized to 0);
(3) A static global variable is visible throughout the file in which it is declared, but not outside the file.
** Static global variables cannot be used by other files; Variables with the same name can be defined in other files without conflict.
The difference between global variables and global static variables
Extern = extern; extern = extern; extern = extern; extern = extern; extern = extern;
Extern; extern; extern; extern; extern; extern; extern; extern; extern; extern
Static local variables have the following characteristics:
(1) This variable allocates memory in the global data area; (2) Static local variables are initialized for the first time when the program executes at the declaration of the object, that is, subsequent function calls are no longer initialized; (3) Static local variables are generally initialized at the declaration, if not explicitly initialized, will be automatically initialized to 0 by the program; (4) It always resides in the global data area until the end of the program. But its scope is local and ends when the function or block that defines it ends.
General programs store newly generated dynamic data in the heap area, and automatic variables inside the function are stored in the stack area. Automatic variables generally free up space when a function exits, and static data (even static local variables inside a function) is stored in the global data area. Data in the global data area does not free up space due to function exit.
Extern basic functions
Extern can be placed in front of a variable or function to indicate that the definition of the variable or function is in another file, prompting the compiler to look for its definition in another module if it encounters the variable or function. Extern can also be used for link designation.
Extern “C” void fun(int a, int b) extern “C” void fun(int a, int b) The compiler is told to translate the name of fun using C rules instead of C++. C++ rules will change the name of fun beyond recognition. It could be fun@aBc_int_int#%$or something else. This depends on the compiler’s “attitude” (different compilers use different methods), why do this, because C++ supports function overloading, I will not discuss this problem too much, if you are interested in the Internet search, I believe you can get satisfactory explanation! Second, when extern is not used with “C” to modify a variable or function, as in a header file: extern int g_Int; Its function is to declare the scope of the function or global variable keyword, its declared function and variable can be used in this module or other modules, remember that it is a declaration not definition! That is to say, if B module (translation units) reference module (translation units) defined in A global variable or function, it can be as long as the header file contains A module, in the compile phase, although module B can’t find this function or variable, but it won’t be an error, it will be in the connection from module A generated object code found in this function.
2 Problem: extern variables define an array in a source file: char a[6]; Extern char *a; extern char *a; extern char *a Excuse me, is that ok? Answer and analysis: 1), no, the program will tell you illegal access. The reason is that a pointer to type T is not equivalent to an array of type T. Extern char A declares a pointer variable instead of an array of characters, and thus differs from the actual definition, resulting in illegal access at runtime. Extern char a[] should be declared instead. 2), example analysis is as follows, if a[] = “abcd”, then the external variable A =0x61626364 (THE ASCII code value of ABCD), A is obviously meaningless, obviously the space a points to (0x61626364) is meaningless, and illegal memory access is easy to occur. Extern = extern; extern = extern; extern = extern; extern = extern; extern = extern; extern = extern; Extern = extern; extern = extern; extern = extern; extern = extern; extern = extern; extern = extern; extern = extern;
Problem: Extern function prototypes when a function provider modifies a function prototype from a single aspect, the compiler will not report errors at compile time if the user continues with the original extern declaration without knowing it. However, in the process of operation, because of less or more input parameters, often as a system error, how to solve this situation? There is no perfect solution to this problem. It is common for the provider to declare the external interface in its xxx_pub.h file and then call the include header file to omit the extern step. To avoid this mistake. Extern has double edges, and extern should be used differently in different situations.
Four problems: extern “C” in the C + + environment using C function, often appear the compiler cannot find obj modules in C function definitions, leading to link failure situation, should how to solve this situation?
Answer and analysis: When compiling C + + language in order to solve the problems of polymorphic function, the function name and parameters will be combined to generate a middle name of the function, the C language is not, so will cause the link can’t find the corresponding function, the C function requires using extern “C” link specified, this tells the compiler, please keep my name, Don’t give me intermediate function names for linking. #ifdef __cplusplus #if __cplusplus extern “C”{#endif #endif /* __cplusplus … #ifdef __cplusplus #if __cplusplus} #endif #endif / __cplusplus */
Extern function declarations extern is always extern in front of functions as part of function declarations. What role does the C keyword extern play in function declarations? Extern: If a function is declared with the extern keyword, it simply indicates that the function may be defined in another source file. Extern int f(); extern int f(); And int f (); There is, of course, a way to declare functions in programs instead of include “*.h”. In more complex projects, I like to add extern extern before all function declarations. For reasons and pros and cons, see the following example: “extern global variables”
#ifndef TEST1H #define TEST1H extern char g_str[]; // declare the global variable g_str void fun1(); #endif (2) in test1.cpp #include “test1.h” char g_str[] = “123456”; // define the global variable g_str void fun1() {cout << g_str << endl; } (1) if you want to use g_str in the test2 module, you need to reference g_str in the original file.
void fun2() { cout << g_str << endl; Obj = 123456; obj = 123456; obj = 123456; obj = 123456 This is because g_str is a global variable for the entire project, and there is only one copy of it in memory. The test2.obj compilation unit does not need another copy, otherwise it will report a repeat error when connecting. Extern char g_str[] = “123456”; extern char g_str[] = “123456”; // extern = extern; // extern = extern; // extern = extern; // extern = extern; // extern = extern; // extern = extern The test1. CPP module contains test1.h so g_str is defined once, and test2. CPP contains test1.h so g_str is defined again, at which point the connector finds two g_str when connecting test1 and test2. Extern char g_str[]; extern char g_str[]; extern char g_str[]; extern char g_str[]; void fun2() { cout << g_str << endl; } At this point, the compiler knows that g_str is derived from an external compilation module and will not re-define one in this module, but I would say that this would be very bad, If you can’t use #include “test1.h” in test2. CPP, then you can’t use any function declared in test1.h unless you also extern it. In this case, you’ll have a long list of functions declared in test1. CPP. So remember, just declare in the header file, the truth is always that simple.
4. Extern and static
Extern = extern; extern = extern; extern = extern; extern = extern; extern = extern;
A static scope is an internal connection, as opposed to extern. Extern is stored separately from the object itself. Extern is also stored separately, but extern can be referred to by other objects with extern. Static is not. Extern and extern are extern and extern are extern and extern are extern and extern are extern and extern are extern and extern. Second, static global variables are declared at the same time as the definition. That is, when you declare a global variable static in a header file, it is defined at the same time. Test1.h test1.h test1.h test1.h test1.h test1.h test1.h test1.h #ifndef TEST1H #define TEST1H static char g_str[] = “123456”; void fun1(); #endif
(2) test1.cpp: #include “test1.h” void fun1() { cout << g_str << endl; } (3) test2.cpp #include “test1.h” void fun2() { cout << g_str << endl; } The above two compilation units can connect successfully. When you open test1.obj, you can find the string “123456” in it, and you can also find them in test2.obj. They can connect successfully without repeating the definition error because although they have the same content, But the physical address of the storage is different, as if the same value were assigned to two different variables that operate on their respective compilation units. Test1,test2,test2, test1,test2, test1,test2, test1,test2,test2, test1,test2, test1,test2,test2, test1,test2,test2, test1,test2,test2, test1,test2,test2, test1,test2,test2, test1,test2,test2, test1,test2,test2, test1,test2,test2, test1,test2,test2, test1,test2 Most compilers optimize the code to save memory and execute more efficiently. When the compiler connects various compilation units, it will make a single copy of the memory with the same contents. For example, “123456” above, variables in both compilation units have the same contents. If you change the code above to look like this, you can immediately catch the compiler lying: #include “test1.h” void fun1() { g_str[0] = ”a”; cout << g_str << endl; }
(2) test2.cpp #include “test1.h” void fun2() { cout << g_str << endl; } (3) void main() { fun1(); // a23456 fun2(); // 123456} When you trace the code, you will find that the g_str address in the two compilation units is not the same, because you changed it in one place, so the compiler is forced to restore the original memory, there are two copies in memory for the use of variables in both modules. When defining a static global variable, put it in a header file instead of the original file. This will not cause unnecessary information pollution to other modules.
5. Extern and const
Extern const char g_str[]; / / extern const char g_str[]; / / extern const char g_str[]; / / extern const char g_str[]; Const char g_str[] = “123456”;
So const is like static when used alone, and extern when used with extern! Const char* g_str =” 123456″ is different from const char* g_str[] =”123465″, The previous const is a char instead of g_str. Its g_str is not a constant. It is treated as a defined global variable (which can be used by other compilation units), so if you want to make charg_str a const global constant, Const char* const g_str=”123456″ is best defined as such.