C ++ compiler function name modification rules, compiler function name
We know that there is a function overload in C ++. When we define several functions with the same name and similar functions, as long as their parameter lists are different, compilation is acceptable, but not in C.
double add(double a, double b){ return a + b;}int add(int a, char b){ return a + b;}char add(char a, char b){ return a + b;}
If the code is written in this way, errorC2371 is reported in C, indicating that the add function is redefined. However, if this is allowed in the C ++ environment, it is called function overloading. As long as your functions meet the requirements of function overloading, there is no problem at all. This leads to the modification rules for writing numbers in C and C ++. Why is it impossible in C?
Double add (double a, double B );
//{
// Return a + B;
//}
I changed the definition of this function to Declaration, and then called in the main function, you can see
1. C compiler function name modification rules
The compiler reports a link error to us. We can see a function like _ add. This is the modifier of the compiler to our function name in the C environment, you can see that the function is preceded by an underscore. Of course, this modification is actually the default call Convention _ cdecl. If the _ stdcall call Convention is used, the compiler and the linker will add an underline prefix before the output function name, followed by a "@" symbol and the number of bytes of its parameters, __fastcall indicates that a "@" symbol is added before the output function name, followed by a "@" symbol and the number of bytes of its parameters.
We can modify the call conventions to see if this is the case.
After the modification, you can see that the add function is modified to _ add @ 16. Therefore, the modification rules vary with different call conventions.
1. C ++ compiler function name modification rules
Then, let's look at how the compiler modifies our functions in the C ++ environment:
We can see that the double add (double a, double B) function that I declare is modified (? Add @ YANNN @ Z). We can see that this is completely different from the C environment, so this is also the reason for function overloading in C ++, let's see how this is modified.
The function name modification rules of C ++ are complicated, but the information is more adequate. By analyzing and modifying the function name, we can not only know the function call method, return value type, number of parameters, or even parameter type. Whether _ cdecl ,__ fastcall or _ stdcall is called, the function modifier is a "?" Start, followed by the name of the function, followed by the START identifier of the parameter table and the parameter table spelled out according to the parameter type code. For the _ stdcall method, the start identifier of the parameter table is "@ YG". For the _ cdecl method, it is "@ YA ", the _ fastcall method is "@ YI ". The spelling code of the parameter table is as follows:
X -- void
D -- char
E -- unsigned char
F -- short
H -- int
I -- unsigned int
J -- long
K -- unsigned long (DWORD)
M -- float
N -- double
_ N -- bool
U -- struct
We can see that N represents the double type, then (? Add @ YANNN @ Z): Can you understand the function modifier ?, Followed by the function name, followed by the START identifier of the _ cdecl Convention @ YA, followed by the return value type. The parameter type of the parameter list is exactly the same, so double add (double, double B) is NNN, and the end of the name is marked with "@ Z" after the parameter table. If this function does not have a parameter, it ends with "Z.
In addition, when there is a pointer in the parameter list, the pointer method is somewhat special. PA is used to represent the pointer and PB is used to represent the const type pointer. The following code indicates the pointer type. If a pointer of the same type appears consecutively, it is replaced by "0". A "0" indicates a repetition.
A simple example: int fun (int * p1, int * p2 );
As you can see, my fun function is modified (? Fun @ YAHPAH0 @ Z ).
In addition, the modifier of the Public and Private member functions in the member functions in C ++ also has a corresponding identifier. All in all, when the function name is modified in the C ++ environment, there will be information about the parameter list and the returned value. Therefore, function overloading in C ++ is allowed, because it can select the corresponding function based on your parameter list, it is obviously not allowed in our C environment.