What is Name Mangling
Modern programming languages consist with user friendly and function rich features like overloading, overriding, namespace, class etc.. Also most of the OOP languages allow programmer to do logical grouping of coding than writing the whole code in one global space. So when modern programming languages need to support to features like above mentions, compile makers get trouble and the compiling and validation logic may get more complex. As examples Different classes can be have functions/member variables with same name
Same class can be have same function name with different arguments(function overloading)
Base class and child can have same function name (function overriding)
Different Namespaces can have same name for function, structure, class or another datatype.
So when compiler processing the situation not like old structural language, naming is quite complex. So there should be a way to make above each names unique. Then linker can uniquely identify the correct function and call it.
So because of the above reason and few of other reason compiler makers introduce a concept call "Name Mangling" what here doing is make a function name by adding some prefix or post-fix to the function name when compiling the program. as example
class Test() { int add(int a, int b); int add(int a, int b, int c); }
Here is two functions with same name in same class, but number of argument is different. So If I make my own compiler I can purpose a Name Mangling style
_functionName@numberOfArguments@FirstCharactorOfArgumentsType
This is my own Name Mangling (unique function name generation) style (No real style like this). So the above two function Name Mangling will be
_add@2@i _add@3@i
So the linker can differentiate the function now and both functions have unique names. This is looks like decorating the function names. So Name Mangling also called as "Name Decoration".
Other Than Name Decoration
Other than name decoration, Name Mangling use to pass some information from compiler to linker, such as argument type, length of the argument, calling conventions. Because the Mangling name can be consist with some information other than function name. So this also a extra benefit of Name Mangling. in our abouve example linker can extract the number of argument ( two or three) and argument type (integer) from the unique function name. So even the languages like C that doesn't exist function overloading and overriding kind of advance features, also using Name Mangling for pass these extra information from compiler to linker or platform.
C++ Name Mangling
Various C++ compilers also implemented their own Name Mangling styles. So compiler to compiler generated name for same function would be different. Some time version to version of same compiler also may be use different Name Mangling styles. So As you can understand the object code compiled by different compilers could not be link together because of their different Name Mangling. But Some of the compilers vendors fallow the same Name Mangling styles. But we cannot expect 100% those will be same.
Actually C++ having quite complex features like template, operator overloading. So the Name Mangling styles of C++ compilers are more complex than other languages.
Stranded for C++ Name Mangling
There is not a standers for C++ Name Mangling. If there is a standers for Name Mangling, the object code compiled by different compilers could be link together using one linker. But when we come to linker story, Name of the function is not only the matter for compatibility there are some few things need to consider when linking the functions object codes together such as exception handling, virtual table structure etc... These binary interfaces are called Application Binary Interfaces. So Name of the function (Name Mangling) is only one ABI. there are few more. So if there is a standers Name Mangling for C++, that would not be enough for link the different compiler binaries together. And also If there is a slandered for Name Mangling it would be a limitation for the compiler vendors too. Other than that C++ standers encourage to keep different Name Mangling sachems to prevent linking with incompatible codes just only because of their same names .
c++ sample program codes for students
ReplyDeletecplusplus code generic bubble sort