C knowledge point
I. Difference between # include "filename.h" and #include
#include "filename.h" means that the compiler will start looking for this file from the current working directory.
#include
Second, the role of header file
Strengthen safety testing
The library function can be easily invoked by the header file without having to care about its implementation.
Third, *, & modifier position
For * and & modifiers, in order to avoid misunderstandings, it is best to make the modified variable name.
Fourth, IF statement
Do not compare the Boolean variables with any value, it will be easier to make mistakes.
Plastic variables must be compared with the same value
The floating point variable is preferably less than point, even if it is more than worth
Pointer variables are compared to NULL, do not compare with Boolean and plastic surgery
5. Comparison of consts #define
Const has data type, # define no data type
CONST can be debugged in individual compilers, # define can not debug
There are two ways to define constants in the class.
1. In the category constant, but does not assign the value, the value is assigned in the constructor initialization table;
2. Use enumeration instead of CONST constants.
Six, the way to pass the value in the C function
There are three ways: Pass by value, PASS BY POINTER, PASS BY REFERENCE
Void Fun (Char C) // Pass by Value
Void Fun (Char * Str) // Pass by Pointer
Void Fun (Char & Str) // Pass by Reference
If the input parameter is passed in value, it is best to use the reference transfer instead, because reference to pass the provision and sector of the temporary object
The type of function cannot be omitted, even if there is no Void,
7. The pointer or reference constant in the function body cannot be returned.
Char * func (void)
{
Char str [] = "Hello Word";
// This is not returned because the STR is a specified variable, not a general value, and will be canceled after the function is over.
Return Str;
}
The pointer variable in the function does not release as the function's demise
Eight, a memory copy function implementation
Void * Memcpy (void * pvto, const void * pvfrom, size_t size)
{
Assert ((PVTO! = null) && (pvfrom! = null);
BYTE * PBTO = (Byte *) PVTO; / / Prevent address changes
BYTE * PBFROM = (byte *) pvfrom;
While (size -> 0)
PBTO = PBFORM ;
Return PVTO;
}
Nine, memory distribution
There are three ways to allocate, please remember, if you say that you will ask you this issue when you go to interview.
1. Static storage area, is allocated in the program compile, existed throughout the run, such as global variables, constants.
2, allocation on the stack, local variables in the function are allocated from this, but the allocated memory is limited.
3, allocation on the pile, also known as dynamic allocation,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Ten, memory allocation precautions
When allocating memory with new or malloc, you must assign the initial value.
After release memory with delete or free, you must point the pointer to null
Cannot modify pointer data to constant
XI, content replication and comparison
// array ... char a [] = "Hello Word!";
Char B [10];
STRCPY (B, A);
IF (strcmp (a, b) == 0)
{}
//pointer……
Char a [] = "Hello Word!";
Char * p;
P = new char [strlen (a) 1];
STRCPY (P, A);
IF (strCMP (p, a) == 0)
{}
Twelve, sizeof problem
Remember, C can't know the size of the object referring to the pointer, the size of the pointer is always 4 bytes
Char a [] = "Hello World!"
Char * p = a;
Count << SizeOf (a) << end; // 12 bytes
Count << sizeof (p) << endl; // 4 bytes
Moreover, in the function, the array parameter is degraded into a pointer, so the following content will always be 4
Void Fun (Char A [1000])
{
Count << sizeof (a) << Endl; // Output 4 instead of 1000
}
Thirteen, about pointer
1. Must be initialized when the pointer is created.
2, the pointer must be NULL after Free or Delete
3, the length of the pointer is 4 bytes
4. When the memory is released, if it is an array pointer, you must release all memory, such as
Char * p = new char [100];
STRCPY (P, "Hello World");
Delete [] p; // Watch the front []
p = null;
5, the content of the array pointer cannot exceed the maximum of array pointers.
Such as:
Char * p = new char [5];
STRCPY (P, "Hello World"); // Report error objective is easy enough
Delete [] p; // Watch the front []
p = null;
Fourteen, About Malloc / Free and New / Delete
l Malloc / free is a C / C memory allocation, New / Delete is a C memory allocation.
l Note: Malloc / free is a library function, and new / delete is an operator.
l Malloc / free cannot perform constructor and destructor, and new / delete can
l New / delete can't run on C, so Malloc / Free cannot be eliminated
l must be used in both
The _SET_NEW_HANDER function can be used to define the processing of abnormal memory allocation in the L C .
Features of fifteen, C
C adds overload, inline, const, and Virtual four mechanisms
Overload and inline: can be used for global functions, and can also be used for classes;
Const and Virtual: Only available members of class;
Overload: In the same class, the function name is the same. The function is called by different parameters. The function may not be Virtual keyword. The function of the same name with the full bureau is not called overload. If the global function of the same name is called in the class, you must use the global reference symbol :: reference.
Overlay is a basic class function for derived class functions
The same function name;
The same parameter;
The base class function must have a Virtual keyword;
Different range (derived class and base class).
Hide is the same name function that is the same as the base class. The function name is the same, but the parameters are different, and the base class function will be hidden regardless of the base class without the Virtual keyword.
2, the function name is the same, the parameters are the same, but the base class has no Virtual keyword (which is overwriting), the base class function will be hidden.
Inline: The inline keyword must be placed with the definition, not in the statement.
Const: const is Constant's abbreviation, "constant" means. Things that are modified by const are mandatory, prevent accidents, can improve the robustness of the program.
1. Parameter Do the pointer type parameters used, plus const can prevent accidental changes.
2. When the user type referenced by the value is used to input parameters, it is best to pass the value transfer to reference to the reference, and add the const keyword, the purpose is to improve efficiency. There is no need to do this for internal types; such as:
Change Void Func (A a) to Void Func (Const A & A).
Void func (int A) is not necessary to change to Void Func (Const Int & A);
3. Plus const on the return value is a function of the pointer type, which makes the function return value cannot be modified, and the variable assigned can only be a Const type variable. Such as: function const char * getString (void); char * str = getString () will be wrong. Const char * str = getString () will be correct.
4. Const member functions refer to this function that can only call Const member variables in this function to improve the keyness of the program. If the function int getcount (void) const; this function can only call the Const member variable.
Virtual: virtual function: Detective class can overwrite functions, pure virtual functions: just an empty function, no function implementation;
What is the role of EXTERN "C"?
EXTERN "C" is a connection to which the C provides a specified symbol for telling C code is a C function. This is because the function name in the C compile library will become very long, inconsistent with C, causing C not directly call C functions, plus Extren "C", C can directly call C functions.
Extern "C" mainly uses references and exports of regular DLL functions and use when C contains C functions or C header files. Plus the Extern "C" keyword in front of use.
Seventest, constructor and destructor
The constructor of the derived class should call the constructor of the base class in the initialization table;
The destructor of the derived class and the base class should be plus the Virtual keyword.
Don't look at the constructor and the destructor, it is still not easy.
#include
Class Base
{
PUBLIC:
Virtual ~ base () {cout << "~ base" << endl;}
}
Class Derived: Public Base
{
PUBLIC:
Virtual ~ derived () {cout << "~ derived" << endl;}
}
Void main (void)
{
Base * pb = new deerived; // upcast
Delete PB;
}
The output is:
~ Derived
~ Base
If the destructor is not empty, then the output result is ~ base
Eighteen, # ifndef / # define / # ENDIF does what role
Inspector this header file is repeatedly referenced