help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: c++ name-mangling with gcc 3.2.0


From: Dirk Hoffmann
Subject: Re: c++ name-mangling with gcc 3.2.0
Date: Wed, 22 Dec 2004 10:28:28 +0100

On 21 Dec 2004, Chetan wrote:

> Can anyone specify me algorithm or psuedo code for c++ name-mangling for gcc 
> 3.2.0 .

Is "c++filt" what you need? 

 $ nm empty | grep Vacuum
 08048954 W _._12ActiveVacuum
 080488c0 W __12ActiveVacuum
 0804890c W __12ActiveVacuumi
 080489b8 W number__12ActiveVacuum

 $ nm empty | grep Vacuum | c++filt
 08048954 W ActiveVacuum::~ActiveVacuum(void)
 080488c0 W ActiveVacuum::ActiveVacuum(void)
 0804890c W ActiveVacuum::ActiveVacuum(int)
 080489b8 W ActiveVacuum::number(void)

As to your second question, 

> Why and how name-mangling is done in c++?

you may want to have a look at the source code below. It is obvious that the
two creators ActiveVacuum(void) and ActiveVacuum(int), which have different
signatures (i.e. argument lists) need to be different pieces of code in
binary code. Hence, if you want to identify them, you need to give them
different text symbols (i.e. names) in your binary file.

As you can also see from this simple example, the mangling is done by "just 
adding" a symbol for the arguments to the name. But that's only half the 
truth. Imagine what would happen, if you had another "class ActiveVacuumi" 
defined? Give it a try!

NB: Mangling algorithms are not (always) compatible across different g++
versions!

                                                                        Dirk


---------- example code ----------

#include <iostream>

class ActiveVacuum {
    private:
        int number_;
    public:
        ActiveVacuum(){
            number_ = 4711;
            cout << "Creation of ActiveVacuum "<<number_<<endl;}
        ActiveVacuum(const int id){
            number_ = id;
            cout << "Creation of ActiveVacuum "<<number_<<endl;}
        ~ActiveVacuum() {           // destructor!!
            cout << "Destruction of ActiveVacuum "<<number_<<endl; }
        void number() {
            cout << "Im am Vacuum numéro " << number_ << endl;
            }
    };

int main() {
    ActiveVacuum v(1);
    { /* Begin a new scope here */
        ActiveVacuum w(2);
        ActiveVacuum x;
        v.number();
        w.number();
        x.number();
    }
    v.number();

    ActiveVacuumi y(8);
    y.number();

    return 0;
    }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]