What is name mangling, and how does it work?

Please explain what is name mangling, how it works, what problem it solves, and in which contexts and languages is used. Name mangling strategies (e.g. what name is chosen by the compiler and why) a plus.


Solution 1:

In the programming language of your choice, if an identifier is exported from a separately compiled unit, it needs a name by which it is known at link time. Name mangling solves the problem of overloaded identifiers in programming languages. (An identifier is "overloaded" if the same name is used in more than one context or with more than one meaning.)

Some examples:

  • In C++, function or method get may be overloaded at multiple types.

  • In Ada or Modula-3, function get may appear in multiple modules.

Multiple types and multiple modules cover the usual contexts.

Typical strategies:

  • Map each type to a string and use the combined high-level identifier and "type string" as the link-time name. Common in C++ (especially easy since overloading is permitted only for functions/methods and only on argument types) and Ada (where you can overload result types as well).

  • If an identifier is used in more than one module or namespace, join the name of the module with the name of the identifier, e.g., List_get instead of List.get.

Depending on what characters are legal in link-time names, you may have to do additional mangling; for example, it may be necessary to use the underscore as an 'escape' character, so you can distinguish

  • List_my.get -> List__my_get

from

  • List.my_get -> List_my__get

(Admittedly this example is reaching, but as a compiler writer, I have to guarantee that distinct identifiers in the source code map to distinct link-time names. That's the whole reason and purpose for name mangling.)

Solution 2:

Simply put, name-mangling is a process by which compilers changes the names of identifiers in your source code in order to aid the linker in disambiguating between those identifiers.

Wikipedia has a wonderful article on this subject with several great examples.

Solution 3:

Name mangling is a means by which compilers modify the "compiled" name of an object, to make it different than what you specified in a consistent manner.

This allows a programming language the flexibility to provide the same name to multiple, compiled objects, and have a consistent way to lookup the appropriate object. For example, this allows multiple classes with the same name to exist in different namespaces (often by prepending the namespace into the class name, etc).

Operator and method overloading in many languages take this a step further - each method ends up with a "mangled" name in the compiled library in order to allow multiple methods on one type to exist with the same name.

Solution 4:

In python, name-mangling is a system by which class variables have different names inside and outside the class. The programmer "activates" it by putting two underscores at the start of the variable name.

For example, I can define a simple class with some members:

>>> class Foo(object):
...  def __init__(self):
...   self.x = 3
...   self._y = 4
...   self.__z = 5
... 

In python practice, a variable name starting with an underscore is "internal" and not part of the class interface, and so programmers should not rely on it. However, it is still visible:

>>> f = Foo()
>>> f.x
3
>>> f._y
4

A variable name starting with two underscores is still public, but it is name-mangled and thus harder to access:

>>> f.__z  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute '__z'

If we know how the name-mangling works, however, we can get at it:

>>> f._Foo__z
5

i.e. the classname is prepended to the variable name with an extra underscore.

Python has no concept of 'private' versus 'public' members; everything is public. Name-mangling is the strongest-possible signal a programmer can send that the variable should not be accessed from outside the class.

Solution 5:

Source:http://sickprogrammersarea.blogspot.in/2014/03/technical-interview-questions-on-c_6.html

Name mangling is the process used by C++ compilers give each function in your program a unique name. In C++, generally programs have at-least a few functions with the same name. Thus name mangling can be considered as an important aspect in C++.

Example: Commonly, member names are uniquely generated by concatenating the name of the member with that of the class e.g. given the declaration:

class Class1
 {
        public:
            int val;
            ...
  };

val becomes something like:

  // a possible member name mangling
     val__11Class1