C++ Concepts

February 8, 2009 at 11:18 AM (Technical Articles)

Hi friends, this time i am discussing few C++ concepts, this is mainly for my juniors, who are eager to work in a software company, as they are in final year, very soon they will introduce them self to the world of technology.

Today I am discussing two main concepts, Virtual Functions and Name Mangling. Every one suggest us that, get the knowledge about the concept in depth. But there may be several mind storming questions arise in students, like how to go in depth? what is meant by depth? in which way i have to work?…such questions arise, right? so here I am trying to explore the things little bit in depth( Again the same word depth!!!), and i think it might help you.

OK THEN START…
1> Virtual Functions-
A simple web definition for Virtual function is a member function of a class , whose functionality can be overridden in its derived class. Here we come to the point of difference between overloading and overriding(Then search in google you will get enough information on that). With the concept of virtual functions in C++ a new mechanism evolved, that is called as dynamic binding. Dynamic binding is nothing but deciding which function to be called at run time. I don’t want to expose much about dynamic binding since text books have explained enough about that. Then you might thinking that, what Sadanand is going to discuss about virtual Functions? right??
Continued..
Then think, how exactly virtual functions are handled ? We have both
non-virtual and virtual functions in a class, then how exactly compiler deals with virtual functions? Till now i did not read any such aspects in text books, but Google know what the people are looking for! yes i used Internet to get such information. I am trying to discuss what exactly happens internally when we use virtual functions.

when you declare a virtual function in a class, an object in the memory looks more beautiful than the object without virtual function, then how objects looks in memory?
(Sorry friends i can’t attach image here, i tried but it was not attaching, so please mail me i will send the image indicating object in memory… steggi_cs@yahoo.co.in).

So an object contain a hidden VPTR (Virtual pointer) which is pointing to VTABLE (Virtual table). Assume that an object contain two data members X and Y, then along with these an extra hidden pointer is supplied by Compiler. Friends are you not getting a question in your mind ? that what the VTable contains? is VTABLE exist for all objects? here is the answer..

VTable contains addresses of all the virtual functions declared within a class so if a class contain two virtual functions then it contains addresses of both the virtual functions assume that two functions are f() and g(), then VTable contains addresses of both the functions . When a virtual function is called compiler will resolve the function call with the help of VPTR. VTable is global to a class, i mean to say all the objects will share the same VTable, but each object has its own VPtr.

since an object contain an extra hidden VPtr, then think of about the size, and check it by yourself, print the sizeof an object that contain virtual function, and remove the virtual function and then print the size, then think over that.

class X
{
int a; int b;
public:
virtual void func1();
};
void X:: func1()
{ cout<<“I am in Virtual Function”;
}
main()
{
X *Obj1 = new X();
cout<<“size of an object “<
}// it suppose to print 8bytes( size of int assumed 4 bytes), but it prints 12 bytes due to VPtr
how 12 bytes? size of obj = sizeof(a) + sizeof(b) + sizeof VPtr = 4 + 4 + 4 =12.

These are the very few points i discussed, but more & more you will get in Internet, so i would like to suggest you to read ,to think, to work in that way, think what happens internally, don’t just work blindly, yes even i did the same when i was in college, but once I joined Robosoft I learnt lot and lot, but i suggest you to read now only, specially one who recruited for Robosoft in my juniors batch.

2> Name Mangling-
It is the technique used to solve various problems caused by the need to resolve unique names for programming entities. It is useful to pass more semantic information from compiler to linker.
simple example to demonstrate name mangling.

void func();
class X
{
public:
void func();
};
see in this program, function func() appeared in two different scopes, one at global scope, another at class scope,but question is how compiler handle such things? Interesting thing is while function call there will not occur any name clashes, then ask your self Why there is no name clash? since both functions have same name, then how compiler handles?,
Compilers use the technique of name mangling to resolve such issues.

Dear friends we simply read the text, executed the programs, but we never thought of this, , so my dear friends start working now only, think over those things.

OK let me explain how name mangling done at compiler level? in the above example both the functions are assigned a unique name internally, the mangled names might be like this , _func() and _X_Func() this is not the exact one i specified, different compilers handle differently( link:http://en.wikipedia.org/wiki/Name_mangling ).

Finally i would like to say some words for my juniors, As you all know that C/C++ are the most important languages,whatever new language emerges into the market, internally that language make use of C/C++ concepts, so try to be depth in C/C++. search in Google, wikipedia you will get huge information over there. Here I am trying to show the way how we should read, how we should think, ok!! then I will give you some tips which help you to think over such things.

then How to work???
some tips from my side,

1> When you call a function-
Don’t blindly do the job, just think over the arguments, where they will be stored in memory? as we studied in Microprocessor memory is divided into several chunks, Data segment, text segment, heap etc right? then where the function arguments stored? where the static data members stored? where the local variables of functions stored? etc think in that way and search if you did’t get..

2> Return type –
In operator overloading , most of the operators return reference types, not an object type, then think why reference type return?
See this example.

int a[5] = {0}; //Global
int& funct(int index) //refernce type return value
{ return a[index];
}
main()
{
funct(3) = 10;
//Strange right? function call at the left side of assignment
// remove that reference type return and execute, you will get error Then think why error???
}

3>Static Data-
You all worked with static data members in a class, but have you tested them declaring in template classs? so what happens when you declare a static data members in template class?

 

4> What is the use of function pointers?
in college we just read about this but never tried right? so then try this.

Hope my words might touch your mind, i tried my level best to explore the concepts, this is not the final one, you think over them and do inform me if you get any mistakes. If you really feel that this article helped you, to improve your knowledge then please post me the comments and it would encourage me to write few more such articles.

 

Advertisements

1 Comment

  1. r4nj33t said,

    ok..thanks Sadanand for the article.. could you plz mail me that object’s memory side image on following id:

    r4nj33t@gmail.com

    Thanks 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: