Linkers, Loaders & Executables
What is the main purpose of name mangling, in which a C++ compiler encodes argument types and other information into symbol names?
- ATo make function names unreadable so the program cannot be stolen
- BTo shorten function names and make the executable smaller
- CTo let the linker distinguish overloaded functions with the same name but different argument types, and namespaces
Show answer and explanation
Answer: C. To let the linker distinguish overloaded functions with the same name but different argument types, and namespaces
The linker matches symbols purely by their name strings. C++ allows functions with the same name, such as print(int) and print(double), which would otherwise be indistinguishable. So the compiler embeds the argument types and more into the name, as in "_Z5printi". C functions are not mangled, so when C++ calls a C function, it is declared extern "C" to turn mangling off.
- A. It is not a protection measure.
- B. Names actually get longer.
- C. Correct. The linker matches by name alone.