Can anyone elobrate me about Name Mangling?

i tried to get name mangling for the below,

struct sample
{
    int a;
    char b;
    int a[1];   
};
int myfun(int a, int b, int c[], struct a[])
{
    return 0;
}

int main()
{
    struct sample Mystruct[2];
    int a, b, c[9];

    //Trying to get mangling for variable c and MyStruct for how many are allocated other than a, b.
    d = myfun(a, b, c, Mystruct); 

    return 0;   
}

I can't get name mangling for variable 'c' index.
When i build the above code and found name mangling for single type only for int array and struct.

Recommended Answers

All 3 Replies

Um...what? What do you mean by "get name mangling". Name mangling is an internal method for compilers to differentiate between otherwise similarly named objects. It's not something you as the end programmer need to care about except as pertains to disabling it for C compatibility.

Your question makes very little sense to me. What do you mean by "trying to get mangling for variable .."?

Name mangling is not something that you should ever be able to see from within the code, because it is a feature used for linking (happens after compilation).

Name mangling in C++ is necessary because of features such as namespaces and function overloading. When you compile a piece of code in C++, the compiler must generate a symbol table of all the functions, types and global (extern) variables that can be found in that code. This is used later when the linker puts many pieces of compiled code (object files) together to produce the final binary (library or executable). In C, this is trivially done by using the actual names of functions and variables as the names of the corresponding entries in the symbol table (i.e., no name mangling in C). But, in C++, because functions, types, or variables with the same name can exist (either as different overloaded versions or in different namespaces), the compiler has to create new names which encode the identifier (name of function, or variable), the namespace (or other scope), and the parameter types (for function overloads). This is called name mangling, and it generates a weird-looking and unique name for functions. The only reason to be concerned with that is if you are trying to inter-operate between C++ code and C code (or others), in which case, you must disable name mangling with the specification extern "C" (which you would put in a header file that declares functions to be found in a C library). Otherwise, name mangling just happens under the hood.

As for your example code, because name mangling is a linking-related feature, it only applies to things that have linkage (i.e., can be visible from other compiled code). In this case, local variables do not have linkage (i.e., they are local to the function), and that includes local variables in main(). In your example, only the functions main() and myfun() would be names that would appear mangled in the resulting binary, and the type sample would appear as well. Nothing else has linkage, so nothing else will be mangled (in fact, local variable names will be nowhere to be found in the resulting compiled code, they will just disappear).

Sorry Deceptiokon and Mike for confusion happended. Actually i want to know the mangled string for a particular function. The below is my situation.
1.In one file say File1.cpp has the below code

struct sample
    {
    int a;
    char b;
    int a[1];
    };
    int myfun(int a, int b, int c[], struct a[])
    {
    return 0;
    }
    int main()
    {
    struct sample Mystruct[2];
    int a, b, c[9];
    //Trying to get mangling for variable c and MyStruct for how many are allocated other than a, b.
    d = myfun(a, b, c, Mystruct);
    return 0;
    }
  1. Compiling File1.cpp. Object file will generate with mangled name for myfun().
  2. In another file say file2.cpp i'm try to read the object file of file1.cpp and try to read mangled name for function myfun(). Mangling happens only for the type of variable passed.
    I want to get mangling name for variable int c[9] like array with the index count for int c[9] and Mystruct[2]. Is there any possible way to get like that??
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.