What is name mangling?

Answers were Sorted based on User's Feedback



What is name mangling?..

Answer / lovly

Name mangling is the encoding of function and variable
names into unique names so that linkers can separate common
names in the language. Type names may also be mangled. The
compiler generates function names with an encoding of the
types of the function arguments when the module is
compiled. Name mangling is commonly used to facilitate the
overloading feature and visibility within different scopes.
Name mangling also applies to variable names. If a variable
is in a namespace, the name of the namespace is mangled
into the variable name so that the same variable name can
exist in more than one namespace. The C++ compiler also
mangles C variable names to identify the namespace in which
the C variable resides.

The scheme for producing a mangled name differs with the
object model used to compile the source code: the mangled
name of an object of a class compiled using one object
model will be different from that of an object of the same
class compiled using a different object model. The object
model is controlled by compiler option or by pragma.

Name mangling is not desirable when linking C modules with
libraries or object files compiled with a C++ compiler. To
prevent the C++ compiler from mangling the name of a
function, you can apply the extern "C" linkage specifier to
the declaration or declarations, as shown in the following
example:

extern "C" {
int f1(int);
int f2(int);
int f3(int);
};

This declaration tells the compiler that references to the
functions f1, f2, and f3 should not be mangled.

The extern "C" linkage specifier can also be used to
prevent mangling of functions that are defined in C++ so
that they can be called from C. For example,

extern "C" {
void p(int){
/* not mangled */
}
};

Is This Answer Correct ?    3 Yes 0 No

What is name mangling?..

Answer / som shekhar

In simple terms Name mangling is name decoration. Compiler
gives different names to the functions and that is why
function overloading is possible.

Is This Answer Correct ?    0 Yes 0 No

What is name mangling?..

Answer / atul shankhwar

Name mangling is the process through which your c++ compilers give each function in your program a unique name. In C++, all programs have at-least a few functions with the same name. Name mangling is a concession to the fact that linker always insists on all function names being unique.

Example:
In general, member names are made unique by concatenating the name of the member with that of the class e.g. given the declaration:
class Bar
{
public:
int ival;
...
};
ival becomes something like:
// a possible member name mangling
ival__3Bar
Consider this derivation:
class Foo : public Bar
{
public:
int ival;
...
}
The internal representation of a Foo object is the concatenation of its base and derived class members.
// Pseudo C++ code
// Internal representation of Foo
class Foo
{
public:
int ival__3Bar;
int ival__3Foo;
...
};
Unambiguous access of either ival members is achieved through name mangling. Member functions, because they can be overloaded, require an extensive mangling to provide each with a unique name. Here the compiler generates the same name for the two overloaded instances(Their argument lists make their instances unique).

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

Can a program run without main?

0 Answers  


Can a function take variable length arguments, if yes, how?

0 Answers  


why can't we declare data member of class auto register or extern

1 Answers  


What operator is used to access a struct through a pointer a) >> b) -> c) *

0 Answers  


how many controls can we place on single form.

1 Answers   Microsoft,






what is Loop function? What are different types of Loops?

0 Answers  


What can I safely assume about the initial values of variables which are not explicitly initialized?

0 Answers  


const char * char * const What is the differnce between the above two?

11 Answers   TCS,


Should I learn c or c++ first?

0 Answers  


What is the use of structure in c++?

0 Answers  


What is overloading unary operator?

0 Answers  


What is c++ programming language?

0 Answers  


Categories