<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Thatvamasi</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Thatvamasi"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Thatvamasi"/>
	<updated>2026-08-12T21:08:45Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=43472</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=43472"/>
		<updated>2010-12-13T00:06:13Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* '''Dynamic Dispatch''' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in OOLS (Object oriented Languages and Systems). Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
We performed a very small experiment to see if Dynamic dispatch is practically slower than static dispatch. We opted to choose C#, since this language provide both static binding and dynamic binding. We measured the time taken to execute 1000000000 dynamically dispatched functions and statically dispatched functions. Below are our observations.&lt;br /&gt;
&lt;br /&gt;
 Dynamic Dispatch:&lt;br /&gt;
 00:00:10.0995777&lt;br /&gt;
 00:00:10.4315966&lt;br /&gt;
 00:00:10.4675987&lt;br /&gt;
&lt;br /&gt;
 Static Dispatch:&lt;br /&gt;
 00:00:10.2565866&lt;br /&gt;
 00:00:10.3255906&lt;br /&gt;
 00:00:10.1575809&lt;br /&gt;
&lt;br /&gt;
It looks like the performance overhead of dynamic binding is negligible in today's high performing computer. Even if it Dynamic Binding needs few more instruction to perform function call, the overhead in terms of time and memory is not much of a concern, but effective software design is a matter of concern. So we can conclude that Dynamic Binding does not cause serious performance problem in present day Computers.&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt; and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would note the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called &amp;lt;b&amp;gt;Multiple Dispatch&amp;lt;/b&amp;gt; &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;. Multiple-polymorphism allows specialized functions or methods to be defined to handle various cases:&lt;br /&gt;
&lt;br /&gt;
  +(int, int)&lt;br /&gt;
  +(int, float)&lt;br /&gt;
  +(int, complex) .. etc&lt;br /&gt;
&lt;br /&gt;
The above functions are specialized to each of the cases required allowing single, highly cohesive and loosely coupled functions to be defined. This is also the true essence of object-oriented polymorphism, which allows objects to define methods for each specific case desired. In addition to better coupling and cohesion, multiple-polymorphism reduces program complexity by avoiding coding logic (switch statements) and because small methods further reduce complexity, as code complexity doesn't grow linearly with lines of code per method, but perhaps exponentially.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Hence from the above discussion, we can see that Dynamic Dispatch, though slower when compared to static dispatch is no longer slower due to the new generation of power packed computers. Also, dynamic dispatch can help in preserving the object-oriented concepts in highly complex software products. We have also looked up the differences between method Overloading and Overriding. We also had a sneak-view to Multiple Dispatch where more than one parameter can be used for Dynamic method lookup.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39720</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39720"/>
		<updated>2010-11-02T00:50:32Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called &amp;lt;b&amp;gt;Multiple Dispatch&amp;lt;/b&amp;gt; &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;. Multiple-polymorphism allows specialized functions or methods to be defined to handle various cases:&lt;br /&gt;
&lt;br /&gt;
  +(int, int)&lt;br /&gt;
  +(int, float)&lt;br /&gt;
  +(int, complex) .. etc&lt;br /&gt;
&lt;br /&gt;
The above functions are specialized to each of the cases required allowing single, highly cohesive and loosely coupled functions to be defined. This is also the true essence of object-oriented polymorphism, which allows objects to define methods for each specific case desired. In addition to better coupling and cohesion, multiple-polymorphism reduces program complexity by avoiding coding logic (switch statements) and because small methods further reduce complexity, as code complexity doesn't grow linearly with lines of code per method, but perhaps exponentially.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Hence from the above discussion, we can see that Dynamic Dispatch, though slower when compared to static dispatch is no longer slower due to the new generation of power packed computers. Also, dynamic dispatch can help in preserving the object-oriented concepts in highly complex software products. We have also looked up the differences between method Overloading and Overriding. We also had a sneak-view to Multiple Dispatch where more than one parameter can be used for Dynamic method lookup.&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39719</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39719"/>
		<updated>2010-11-02T00:43:52Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Advantages of Dynamic Binding */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called &amp;lt;b&amp;gt;Multiple Dispatch&amp;lt;/b&amp;gt; &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;. Multiple-polymorphism allows specialized functions or methods to be defined to handle various cases:&lt;br /&gt;
&lt;br /&gt;
  +(int, int)&lt;br /&gt;
  +(int, float)&lt;br /&gt;
  +(int, complex) .. etc&lt;br /&gt;
&lt;br /&gt;
The above functions are specialized to each of the cases required allowing single, highly cohesive and loosely coupled functions to be defined. This is also the true essence of object-oriented polymorphism, which allows objects to define methods for each specific case desired. In addition to better coupling and cohesion, multiple-polymorphism reduces program complexity by avoiding coding logic (switch statements) and because small methods further reduce complexity, as code complexity doesn't grow linearly with lines of code per method, but perhaps exponentially.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39718</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39718"/>
		<updated>2010-11-02T00:43:18Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Multiple Dispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called Multiple Dispatch &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;. Multiple-polymorphism allows specialized functions or methods to be defined to handle various cases:&lt;br /&gt;
&lt;br /&gt;
  +(int, int)&lt;br /&gt;
  +(int, float)&lt;br /&gt;
  +(int, complex) .. etc&lt;br /&gt;
&lt;br /&gt;
The above functions are specialized to each of the cases required allowing single, highly cohesive and loosely coupled functions to be defined. This is also the true essence of object-oriented polymorphism, which allows objects to define methods for each specific case desired. In addition to better coupling and cohesion, multiple-polymorphism reduces program complexity by avoiding coding logic (switch statements) and because small methods further reduce complexity, as code complexity doesn't grow linearly with lines of code per method, but perhaps exponentially.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39717</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39717"/>
		<updated>2010-11-02T00:40:35Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Multiple Dispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called Multiple Dispatch &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
The Multiple Dispatch is used in Common Lisp Object System (CLOS) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Common_Lisp_Object_System]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39716</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39716"/>
		<updated>2010-11-02T00:37:57Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called Multiple Dispatch &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch Multiple Dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39715</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39715"/>
		<updated>2010-11-02T00:37:33Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called Multiple Dispatch &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39714</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39714"/>
		<updated>2010-11-02T00:36:43Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Overriding and Overloading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overloading]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called Multiple Dispatch &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39713</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39713"/>
		<updated>2010-11-02T00:36:04Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Overriding and Overloading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt; are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called Multiple Dispatch &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39712</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39712"/>
		<updated>2010-11-02T00:35:15Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called Multiple Dispatch &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Multiple_dispatch]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39711</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39711"/>
		<updated>2010-11-02T00:34:30Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Advantages of Dynamic Binding */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called Multiple Dispatch &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Multiple_dispatch]&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39710</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39710"/>
		<updated>2010-11-02T00:32:48Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Multiple Dispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called Multiple Dispatch.&lt;br /&gt;
&lt;br /&gt;
==Multiple Dispatch==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39709</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39709"/>
		<updated>2010-11-02T00:32:33Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Advantages of Dynamic Binding */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
One other advantage of Dynamic binding based on parameter types is that more than one parameter can be used in the selection of a method. Methods that use dynamic binding in this way are called multi-methods and the concept is called Multiple Dispatch.&lt;br /&gt;
&lt;br /&gt;
=Multiple Dispatch=&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39708</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39708"/>
		<updated>2010-11-02T00:17:08Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Advantages of Dynamic Binding=&lt;br /&gt;
&lt;br /&gt;
Dynamic binding has several advantages. It provides tremendous flexibilities. Also, it allows the software to be malleable when requirements change as the system evolves. Because of dynamic binding, caller objects are not concerned how the invoked objects carry out their methods. All they need to know is that the invoked objects know how to carry out their responsibilities, but they themselves need to know only what the invoked objects can do for them. As a consequence of this, type dependencies (which are the bane of procedural programming) cannot have a ripple effect through the system, when system requirements change. The beauty is that such dependencies remain encapsulated within the objects. The flexibility that developers can derive out of this is enormous. For instance, one can install newer types without having to change or stopping the functioning of existing systems. This is something along the lines of &amp;quot;hot pluggable components&amp;quot; of the hardware cousins.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39707</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39707"/>
		<updated>2010-11-02T00:08:34Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Does Dynamic Dispatching hurts in today's power packed Computers? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Even though Dynamic Dispatching is done in run-time, and it takes good amount of memory, today's computers are power packed with good amount of memory. Hence efficiency concerns for implementing Dynamic Dispatching in OOL is of little concern.&lt;br /&gt;
&lt;br /&gt;
However, the method to be called cannot be chosen based on the actual arguments passed to the function, rather, the method is called based on the declared type of the parameters.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39702</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39702"/>
		<updated>2010-11-01T23:44:16Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Does Dynamic Dispatching hurts in today's power packed Computers? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
==Does Dynamic Dispatching hurts in today's power packed Computers?==&lt;br /&gt;
&lt;br /&gt;
Dynamic Dispatching takes&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39701</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39701"/>
		<updated>2010-11-01T23:43:46Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Overriding and Overloading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
==Overriding and Overloading==&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39699</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39699"/>
		<updated>2010-11-01T23:38:20Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Overriding and Overloading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime.&lt;br /&gt;
&lt;br /&gt;
Let us see an two examples which provides insight into Overriding and Overloading. &lt;br /&gt;
&lt;br /&gt;
Example - 1: Overloading&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    public boolean equals( A check){                  # Equals method of parameter type A&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a1);                                  # Object equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # Object equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Example - 2: Overriding&lt;br /&gt;
&lt;br /&gt;
  public class A{&lt;br /&gt;
    @Override&lt;br /&gt;
    public boolean equals( Object check){             # Equals method of parameter type Object&lt;br /&gt;
      System.out.println( &amp;quot;A.equals method called&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static void main(String [] args){          # Main method&lt;br /&gt;
      Object a1 = new A();                            # Instantiate class to get objects&lt;br /&gt;
      A a2 = new A();&lt;br /&gt;
      Object o1 = new Object();&lt;br /&gt;
 &lt;br /&gt;
      a1.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(a1);                                  # A equals method called&lt;br /&gt;
      a2.equals(a2);                                  # A equals method called&lt;br /&gt;
      a2.equals(o1);                                  # A equals method called&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you see the comments provided to the side of the method calls, you would notify the difference in the equals method called in case of both Overloading and Overriding. The decision which method to use, basically has two phases: First overload resolution, then method dispatch. Overload resolution happens at compile-time, method dispatch at runtime.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39689</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39689"/>
		<updated>2010-11-01T15:41:35Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* How Dynamic Dispatch works? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt; Fig.1 - VTable Working &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime. An interesting discussion about this fact is provided [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding here].&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39668</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39668"/>
		<updated>2010-11-01T01:09:56Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime. An interesting discussion about this fact is provided [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding here].&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding Java Dynamic Binding &amp;amp; Method Overriding]. stackoverflow.com. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39667</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39667"/>
		<updated>2010-11-01T01:09:40Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Overriding and Overloading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding [2] and Overloading [8] are two different features of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version of the function to call is deferred till runtime. An interesting discussion about this fact is provided [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding here].&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39666</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39666"/>
		<updated>2010-11-01T01:06:42Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overloading Method Overloading]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39665</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39665"/>
		<updated>2010-11-01T01:05:13Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Code_segment Code Segment]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_method_table Virtual Method Table]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Virtual_function Virtual Functions]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm Virtual Functions in C++]. publib.boulder.ibm.com. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39664</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39664"/>
		<updated>2010-11-01T01:02:52Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Performance Evaluation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function]     [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39663</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39663"/>
		<updated>2010-11-01T01:02:14Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Performance Evaluation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that Virtual functions &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_function][http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc14cplr139.htm]&amp;lt;/sup&amp;gt; are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the Virtual method table [5] and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reasons why not all functions are dynamically binded in C++. In C++, functions which are explicitly marked virtual are dynamically bound whereas remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, hence, all functions are dynamically bound. It is also reasonable to assume that virtual functions might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39661</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39661"/>
		<updated>2010-11-01T00:53:53Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* How Dynamic Dispatch works? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39660</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39660"/>
		<updated>2010-11-01T00:50:48Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* How Dynamic Dispatch works? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
  &lt;br /&gt;
 &lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
 &lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
 &lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39659</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39659"/>
		<updated>2010-11-01T00:49:25Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* How Dynamic Dispatch works? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image. For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal (not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
&lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39658</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39658"/>
		<updated>2010-11-01T00:48:20Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* How Dynamic Dispatch works? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Code_segment]&amp;lt;/sup&amp;gt; area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function 'print()', might be converted to something like 'jmp 0xFFDE123' (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown here) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementations are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to an equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable (Virtual Method Table &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Virtual_method_table]&amp;lt;/sup&amp;gt;) which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be changed like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
Virtual Method table or vtable or dispatch table [5] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39654</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39654"/>
		<updated>2010-10-31T22:19:41Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39653</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39653"/>
		<updated>2010-10-31T22:19:26Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
&lt;br /&gt;
=== References ===&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39652</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39652"/>
		<updated>2010-10-31T22:19:07Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39651</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39651"/>
		<updated>2010-10-31T22:18:31Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Performance Evaluation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=Performance Evaluation=&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39650</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39650"/>
		<updated>2010-10-31T22:18:11Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Performance Evaluation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
==Performance Evaluation==&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39649</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39649"/>
		<updated>2010-10-31T22:17:46Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* How Dynamic Dispatch works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works?==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39648</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39648"/>
		<updated>2010-10-31T22:17:15Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* How Dynamic Dispatch works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
==How Dynamic Dispatch works==&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39647</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39647"/>
		<updated>2010-10-31T22:16:47Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Why we need Dynamic Dispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Need for Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39644</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39644"/>
		<updated>2010-10-31T17:13:07Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Why we need Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39643</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39643"/>
		<updated>2010-10-31T17:12:25Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Why we need Dynamic Dispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
==Why we need Dynamic Dispatch==&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object-Oriented Language [1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39642</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39642"/>
		<updated>2010-10-31T17:09:35Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Method_overriding Method Overriding]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism]. en.wikipedia.org. Retrieved Oct 31, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39641</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39641"/>
		<updated>2010-10-31T17:03:13Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Dynamic Dispatch is the process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function. It is generally used in &amp;lt;b&amp;gt;Object-Oriented Language&amp;lt;/b&amp;gt; (OOL) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming]&amp;lt;/sup&amp;gt;. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called as &amp;lt;b&amp;gt;Method Overriding&amp;lt;/b&amp;gt;  &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Method_overriding]&amp;lt;/sup&amp;gt;). When a reference to the super class is used to execute that function, then which version of the function gets executed depends on the type, the reference to which it points to at that time rather than the type of the reference. In literature it also referred to in different names like &amp;lt;b&amp;gt;Runtime Binding&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;Dynamic Polymorphism&amp;lt;/b&amp;gt;. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_polymorphism]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39635</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39635"/>
		<updated>2010-10-31T16:25:51Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* '''Dynamic Dispatch''' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science. Here we will make an effort to address the efficiency considerations about Dynamic Dispatch, advantages of Dynamic Dispatch using parameter types and dynamic binding in CLOS (Common Lisp Object System).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39633</id>
		<title>CSC/ECE 517 Fall 2010/ch2 5c gn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_5c_gn&amp;diff=39633"/>
		<updated>2010-10-31T15:58:56Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Dynamic Dispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Dynamic Dispatch'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the concept of Dynamic Dispatch in Computer Science.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;br /&gt;
The process of identifying which version of the function to call based on the runtime type of the reference being used to execute the function.  Dynamic Dispatch generally happens in OOL. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type, the reference points to at that time, instead of type of the reference. In literature it also given different names like Runtime Binding or [http://en.wikipedia.org/wiki/Dynamic_polymorphism Dynamic Polymorphism ].&lt;br /&gt;
&lt;br /&gt;
To further understand what Dynamic Dispatch is, consider the below example:&lt;br /&gt;
&lt;br /&gt;
 class Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Animal::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class Dog extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Dog::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cat extends Animal {&lt;br /&gt;
     public void walk() {&lt;br /&gt;
         System.out.println(&amp;quot;Cat::walk&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 Animal a = new _______();&lt;br /&gt;
 a.walk();&lt;br /&gt;
&lt;br /&gt;
The compiler cannot decide which version of walk (Animal, Dog or Cat) during compile time. So the decision of which version of the walk function to call will be deferred till the runtime.&lt;br /&gt;
&lt;br /&gt;
=== Why we need Dynamic Dispatch ===&lt;br /&gt;
Dynamic Dispatch is one of the inherent feature of an Object Oriented Languauge [http://en.wikipedia.org/wiki/Object_oriented_language 1]. In a reasonably large software based on a procedural language like C, you will come across a bunch of switch or if statement which decides which version of the function to call. Ofcourse Dynamic Dispatch can be simulated in a procedural language like C, but it involves dangerous pointer manipulation, which is usually done well by the Compilers in case of OOL like C++. &lt;br /&gt;
&lt;br /&gt;
For example, consider a toll gate application which charges differently based on the car type like Sedan, SUV, Coupe etc., If we have to do this in a procedural language like C in which each classes of car represented by structure, we might have something like this.&lt;br /&gt;
&lt;br /&gt;
 float calculate_charge(void *car, int type)&lt;br /&gt;
 {&lt;br /&gt;
   switch(type)&lt;br /&gt;
   {&lt;br /&gt;
     case SUV:&lt;br /&gt;
         return suv_charge(car);&lt;br /&gt;
     case SEDAN:&lt;br /&gt;
         return sedan_charge(car);&lt;br /&gt;
     case COUPE:&lt;br /&gt;
         return coupe_charge(car);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
You can see that the code above will quickly becomes messy as we define new type of car. If we have done the same in an OOL language like Java the above statement could be replacement by a simple:&lt;br /&gt;
&lt;br /&gt;
 car.charge();&lt;br /&gt;
&lt;br /&gt;
The function charge will be overridden in each of the subclasses (Sedan, Suv, and Coupe). From the above example it is very evident that why we need dynamic dispatch and it is obviously useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How Dynamic Dispatch works ===&lt;br /&gt;
The implementation of Dynamic Dispatch varies significantly based on type language, in fact it changes even from Compiler to Compiler for a same language. We will consider how function are dispatch at runtime in both C and C++. We will try to understand if there is any overhead involved with virtual functions. &lt;br /&gt;
&lt;br /&gt;
In a machine compiled language like C, program logic is compiled into underlying processor instruction set by the compiler. Incase of x86 compiler, C code will be compiled into X86 instruction sets. The function will be compiled and placed in Code Segment area of the process image. So a function in C will just map to a memory location in Code Segment of the process image.&lt;br /&gt;
&lt;br /&gt;
For example calling a function print(), might be converted to something like jmp 0xFFDE123 (jmp is a branching instruction). So a function call is in fact as simple as executing a jump statement (of course you push the arguments into stack before you call jump).&lt;br /&gt;
&lt;br /&gt;
Now lets consider a OOL like C++ which too compiles to hardware instruction set on compilation. Consider the below example of two classes which extend from a common class called Animal(not shown) with one method talk. The below example just demonstrates how dynamic dispatch could be implemented by a compiler. &lt;br /&gt;
A word of caution before you proceed; the implementation is hypothetical and far from any practical use. The objective of the example shown below is just to give an idea of how Dynamic Dispatch might be implemented. Practical implementation are lot more complicated, so we have taken this simple example for easy explanation and understanding. &lt;br /&gt;
&lt;br /&gt;
 class Cat&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Meow!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 class Dog&lt;br /&gt;
 {&lt;br /&gt;
     public:&lt;br /&gt;
         void talk() &lt;br /&gt;
         {&lt;br /&gt;
              cout &amp;lt;&amp;lt; &amp;quot;Bark!!&amp;quot;;&lt;br /&gt;
         }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
The above C++ program might infact be converted to a equivalent C program by the compiler during pre-compilation. Each newly defined structure gets an __id__ and this id will be used to lookup vtable which maps id to the actual function memory location. One we get the memory location we will execute a jump instruction like we did before.&lt;br /&gt;
 &lt;br /&gt;
 struct Cat&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 0&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 struct Dog&lt;br /&gt;
 {&lt;br /&gt;
     int __id__; // 1&lt;br /&gt;
     char name[10];&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void cat_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;meow!&amp;quot;);    &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void dog_talk()&lt;br /&gt;
 {&lt;br /&gt;
     printf(&amp;quot;Bark!!&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void talk(void *object)&lt;br /&gt;
 {&lt;br /&gt;
     int id = *(int *)object;&lt;br /&gt;
     vtable[id](); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void (*vtable[2]);&lt;br /&gt;
 vtable[0] = cat_talk;&lt;br /&gt;
 vtable[1] = dog_talk;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // C++ function call like &lt;br /&gt;
 Animal *d = new Dog(); &lt;br /&gt;
 d-&amp;gt;talk();&lt;br /&gt;
&lt;br /&gt;
 // might be change like this by the compiler&lt;br /&gt;
&lt;br /&gt;
 Dog *d = malloc(sizeof(Dog));&lt;br /&gt;
 d-&amp;gt;__id__ = 1;&lt;br /&gt;
 talk(d);&lt;br /&gt;
&lt;br /&gt;
 // talk function can use the __id__ attribute to offset into the vtable to identify which version of function to call.&lt;br /&gt;
&lt;br /&gt;
[[Image:Vtable.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
virtual method table or [http://en.wikipedia.org/wiki/Virtual_method_table vtable] is the key to virtual function working. It is evident from the above that vtable infact makes the function calling little more expensive compared to normal function calls.&lt;br /&gt;
&lt;br /&gt;
=== Performance Evaluation ===&lt;br /&gt;
From the above explanation it is evident that virtual functions are more complicated that dispatching normal function calls. For example Virtual functions in C++ will need extra memory for each class to store the virtual method table and needs additional CPU cycles to perform vtable lookup, before it can make the actual function call. &lt;br /&gt;
&lt;br /&gt;
This is one of the important reason why not all functions are dynamic bind in C++. In C++, functions which are explicitly marked virtual are dynamically bound, remaining functions are statically bound during the compile time. When we say bound/bind we are actually talking about the mapping between the function name and memory location where it is stored for execution.&lt;br /&gt;
&lt;br /&gt;
In languages like Java, the creators thought dynamic binding being an essential feature of OOL, all functions are dynamically bound. It also reasonable to assume that virtual function might need 20 to 40 extra instruction sets to execute a function call. Given today's processors speed this might be very insignificant.&lt;br /&gt;
&lt;br /&gt;
--------try to find a performance comparison and discuss about that---------&lt;br /&gt;
&lt;br /&gt;
=== Overriding and Overloading ===&lt;br /&gt;
Overriding and Overloading are two different feature of OOL. In function overloading, which version of function to call is decided by the compiler during the compile time itself, where as when a function is overridden, which version to call is deferred till runtime. [http://stackoverflow.com/questions/321864/java-dynamic-binding-and-method-overriding This]provides an interesting discussion about this fact.&lt;br /&gt;
&lt;br /&gt;
=== Does Dynamic Dispatching hurts in today's power packed Computers? ===&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
=== References ===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_oriented_language 1. Wikipedia - Object Oriented Language]&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35449</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1f vn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35449"/>
		<updated>2010-09-18T01:51:30Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the different Unit-Testing Frameworks available for Ruby.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Unit testing is a method by which we can isolate and test a unit functionality of the program, typically individual methods during and long after the code is written. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing]&amp;lt;/sup&amp;gt; It helps to identify errors in the program even without running the entire program. It also helps to do regressing testing to identify buggy code additions in the future. Unit testing frameworks provides us with constructs which simplifies the process of unit testing. Using a standard unit test framework helps other developers to add test cases easily. &amp;lt;sup&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing]&amp;lt;/sup&amp;gt; This chapter walks through three different unit testing frameworks available for Ruby and explains how to use them with examples. The three commonly used unit testing frameworks for ruby are &lt;br /&gt;
&lt;br /&gt;
# Test::Unit&lt;br /&gt;
# Shoulda&lt;br /&gt;
# RSpec&lt;br /&gt;
&lt;br /&gt;
=Test::Unit=&lt;br /&gt;
&lt;br /&gt;
Now we shall consider Test::Unit framework&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
Ruby comes with an in-built, ready to use unit testing framework called Test::Unit. It is a XUnit type framework and typically have a setup method for initialization, a teardown method for cleanup and the actual test methods itself. The tests themselves are bundled separately in a test class from the code it is testing.&lt;br /&gt;
&lt;br /&gt;
==Test Fixture==&lt;br /&gt;
Test fixture represents the initial environment setup(eg. initialization data) and/or the expected outcome of the tests for that environment. This is typically done in the setup() and teardown() methods and it helps to separate test initialization and cleanup from the actual tests. It also helps to reuse the same fixture for more than one tests.&amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html]&amp;lt;/sup&amp;gt; &lt;br /&gt;
&lt;br /&gt;
For example, consider a method &amp;lt;i&amp;gt;prime_check(num)&amp;lt;/i&amp;gt; which takes an integer number as input and outputs whether it is prime number or not. In order to unit test this method we can create the following fixture containing a 2-dimensional array with a number and the expected output of whether it is prime or not.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @NUMBERS = [[3,true], [4,false], [7,true], [10,false]]    &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Assertions==&lt;br /&gt;
The core part of test::unit framework is the ability to assert a statement of expected outcome. If an assert statement is correct then the test will proceed, otherwise the test will fail. This feature helps us to verify the method under test with different types of inputs and track the results. Test::unit provides a bunch of assert methods for this purpose: &lt;br /&gt;
&lt;br /&gt;
{| border=1 cellspacing=0 cellpadding=5&lt;br /&gt;
| assert( boolean, [message] ) &lt;br /&gt;
| True if ''boolean''&lt;br /&gt;
|- &lt;br /&gt;
| assert_equal( expected, actual, [message] )&amp;lt;br&amp;gt;assert_not_equal( expected, actual, [message] )&lt;br /&gt;
| True if ''expected == actual''&lt;br /&gt;
|-&lt;br /&gt;
| assert_raise( Exception,... ) {block}&amp;lt;br&amp;gt;assert_nothing_raised( Exception,...) {block} &lt;br /&gt;
| True if the block raises (or doesn't) one of the listed exceptions.&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For the full list of assertion methods provided by test::unit refer to test::unit assertions. &amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
The test case class &amp;lt;i&amp;gt;BinarySearchTest&amp;lt;/i&amp;gt; subclasses the &amp;lt;i&amp;gt;Test::Unit::TestCase&amp;lt;/i&amp;gt; class and overrides &amp;lt;i&amp;gt;setup&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;teardown&amp;lt;/i&amp;gt; methods. The test methods should start with 'test_' prefix. This helps in isolating the test methods from the helper methods if any. The Test::Unit::TestCase class takes care of making the test methods into tests, wrapping them into a suite and running the individual tests. The test results are collected into &amp;lt;i&amp;gt;Test::Unit::TestResult&amp;lt;/i&amp;gt; object.&lt;br /&gt;
&lt;br /&gt;
    require 'test/unit'&lt;br /&gt;
    require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
    class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
 &lt;br /&gt;
      def setup&lt;br /&gt;
        @input_array = [1,2,3,4,5]      #The test fixture is initialized        &lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      def test_success_left_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,1),true)   #tests if the element present in left half of the array is found&lt;br /&gt;
        assert_equal(binary_search(@input_array,2),true)    &lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_right_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,5),true)   #tests if the element present in the right half of the array is found&lt;br /&gt;
        assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_middle                             #tests if the element present in the middle of the array is found&lt;br /&gt;
        assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_failure&lt;br /&gt;
        assert_equal(binary_search(@input_array,6),false)   #tests if an element not present in the array is not found&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def teardown&lt;br /&gt;
        #nothing to do here&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we have four test methods testing different logical paths of the binary search algorithm. Each test method can have one or more assert statements to test whether conditions are correct in each situation. To run the tests we simply have to run the file binary_search_test.rb and the output is as follows:&lt;br /&gt;
 &lt;br /&gt;
  Loaded suite binarysearch&lt;br /&gt;
  Started&lt;br /&gt;
  F...&lt;br /&gt;
  Finished in 0.372 seconds.&lt;br /&gt;
  &lt;br /&gt;
    1) Failure:&lt;br /&gt;
  test_failure(BinarySearchTest) [binarysearch.rb:25]:&lt;br /&gt;
  &amp;lt;true&amp;gt; expected but was&lt;br /&gt;
  &amp;lt;false&amp;gt;.&lt;br /&gt;
  &lt;br /&gt;
  4 tests, 6 assertions, 1 failures, 0 errors&lt;br /&gt;
&lt;br /&gt;
The results show that the last test case &amp;lt;i&amp;gt;test_failure&amp;lt;/i&amp;gt;, testing the negative scenario is failing. The reason is because the assert statement is expecting &amp;lt;i&amp;gt;false&amp;lt;/i&amp;gt; when number 6, which not present in the array is passed. But the binary_search method is returning true.&lt;br /&gt;
&lt;br /&gt;
==Test Suite==&lt;br /&gt;
Sometimes it is useful to combine a bunch of related test cases and run them as batch. Test::Unit provides a class called TestSuite for this purpose. The below example demonstrates how to bundle binary and sequential test case classes into a single search test suite.&lt;br /&gt;
&lt;br /&gt;
   require 'test/unit/testsuite'&lt;br /&gt;
   require 'binary_search_test'&lt;br /&gt;
   require 'sequential_search_test'&lt;br /&gt;
  &lt;br /&gt;
   class Search_Tests&lt;br /&gt;
     def self.suite&lt;br /&gt;
       suite = Test::Unit::TestSuite.new&lt;br /&gt;
       suite &amp;lt;&amp;lt; BinarySearchTest.suite&lt;br /&gt;
       suite &amp;lt;&amp;lt; SequentialSearchTest.suite&lt;br /&gt;
       return suite&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   Test::Unit::UI::Console::TestRunner.run(Search_Tests)&lt;br /&gt;
&lt;br /&gt;
=Shoulda=&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
One of the downsides of Test::Unit is we end up writing lots of code in order to test the actual code which is sometimes not easy to understand. Shoulda is a library that allows us to write better and more understandable tests for ruby application. Shoulda is not a testing framework by itself. It extends the Test::Unit framework with the idea of &amp;lt;i&amp;gt;context&amp;lt;/i&amp;gt;. We can mix Test::Unit test cases with Shoulda test cases. Shoulda allows us to provide context to the tests so that we can group the tests according to a specific feature or scenario. &amp;lt;sup&amp;gt;[http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
   require 'shoulda'&lt;br /&gt;
   require 'test/unit'&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
   class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
      &lt;br /&gt;
     context &amp;quot;Input array of size 5&amp;quot; do&lt;br /&gt;
      &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1,2,3,4,5]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the left half of the array&amp;quot; do    #tests if the element present in left half of the array is found&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the right half of the array&amp;quot; do   #tests if the element present in right half of the array is found&lt;br /&gt;
         assert_equal(binary_search(@input_array,5),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the middle of the array&amp;quot; do       #tests if the element present in middle of the array is found&lt;br /&gt;
         assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do                #tests if the element not present in the array is not found&lt;br /&gt;
         assert_equal(binary_search(@input_array,6),false)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
    &lt;br /&gt;
     context &amp;quot;Input array of size 1&amp;quot; do&lt;br /&gt;
     &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)           #tests if an element is found in the single element array&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do                 # tests if an element is not found in the single element array&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)         &lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Notice that we are still sub-classing the Test::Unit::TestCase class. In this example we have two contexts one for input array of size 5 and the other for input array of size 1. Each context has its own environment of setup/teardown methods. We can also create nested contexts - the outer setup gets run before the execution of each of the inner contexts. And the setup in the inner contexts gets run when running that context. Each &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; construct is converted into individual test methods and are run. If a test case fails we will get a better description of what that test case is doing from the &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; description.&lt;br /&gt;
&lt;br /&gt;
=RSpec=&lt;br /&gt;
&lt;br /&gt;
Now let us consider about &amp;lt;i&amp;gt;RSpec&amp;lt;/i&amp;gt; Testing Framework in detail.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Behaviour Driven Development&amp;lt;/b&amp;gt; (BDD) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Behavior_Driven_Development]&amp;lt;/sup&amp;gt; is an Agile development process &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Agile_software_development]&amp;lt;/sup&amp;gt; that comprises aspects of Acceptance Test Driven Planning &amp;lt;sup&amp;gt;[http://www.springerlink.com/content/978-3-540-22839-4/] [http://en.wikipedia.org/wiki/Acceptance_testing]&amp;lt;/sup&amp;gt;, Domain Driven Design &amp;lt;sup&amp;gt;[http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Domain-driven_design]&amp;lt;/sup&amp;gt; and Test Driven Development (TDD). &amp;lt;sup&amp;gt;[http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Test-driven_development]&amp;lt;/sup&amp;gt; &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;RSpec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; is a Behavioural Driven Development (BDD) tool aimed at Test Driven Development, originally created by Dave Astels and Steven Baker. However David Chelimsky &amp;lt;sup&amp;gt;[http://blog.davidchelimsky.net/]&amp;lt;/sup&amp;gt; is really the gatekeeper of the RSpec project. &amp;lt;sup&amp;gt;[http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Traditionally we use Unit Test frameworks like JUnit, NUnit or RUnit for writing Test cases. We spend a lot of time writing tests that test every unit of code in our software system. Instead we can shift our focus from Unit testing to Behaviour testing or Behaviour Driven Development (BDD) using RSpec. By focusing on the behaviour of the system it helps clarify in our minds what the system should actually be doing. It also helps us to perform more ‘useful’ tests. Useful tests, cover what the system should be doing and build in enough redundancy so that it should be easy to refactor our code without having to re-write every test.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
RSpec is really two projects merged into one. The RSpec project pages describes these merged projects as:&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;application level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Story Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;object level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Spec Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dan North created &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;rbehave&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; &amp;lt;sup&amp;gt;[http://blog.dannorth.net/2007/06/17/introducing-rbehave/]&amp;lt;/sup&amp;gt; which is the Story Framework and David Chelimsky created the &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;Spec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; Framework. By encompassing two frameworks RSpec equips a programmer with a thorough set of testing tools, allowing you to think about your software problem from a number of perspectives.&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
&lt;br /&gt;
The prerequisites are&lt;br /&gt;
&lt;br /&gt;
# Ruby 1.8.4 or later&lt;br /&gt;
# RSpec Gem (latest)&lt;br /&gt;
&lt;br /&gt;
To install Ruby, please visit official Ruby Website &amp;lt;sup&amp;gt;[http://www.ruby-lang.org/]&amp;lt;/sup&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
To install RSpec, open a command shell, go to /bin folder in Ruby directory and type&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;gt; gem install rspec&lt;br /&gt;
&lt;br /&gt;
==Terms &amp;amp; Definitions==&lt;br /&gt;
&lt;br /&gt;
Here are some terms which are used frequently while working with RSpec. &amp;lt;sup&amp;gt;[http://www.pragprog.com/titles/achbd/the-rspec-book/ ]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;b&amp;gt;subject code&amp;lt;/b&amp;gt; - The code whose behavior is specified using RSpec&lt;br /&gt;
# &amp;lt;b&amp;gt;expectation&amp;lt;/b&amp;gt; - The expected behavior of subject code is expressed using expectation (Similar to 'Assertions' statements used in Test::Unit or other tools in other languages)&lt;br /&gt;
# &amp;lt;b&amp;gt;code example&amp;lt;/b&amp;gt; - An executable example containing the subject code and the expectations (Similar to 'Test Method' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;example group&amp;lt;/b&amp;gt; - A group of code examples (Similar to 'Test Case' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;spec file&amp;lt;/b&amp;gt; - A file which contains one or more example groups&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Let us go through an example to be clear on the usage of RSpec.&lt;br /&gt;
&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
   &lt;br /&gt;
   describe BinarySearchTest do&lt;br /&gt;
     before(:all) do&lt;br /&gt;
       @input_array = [1, 2, 3, 4, 5] # The Input Array&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     after(:all) do&lt;br /&gt;
       # do nothing here&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the left-half of the array&amp;quot; do  # Test case for element to be present in left-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(1)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the right-half of the array&amp;quot; do  # Test case for element to be present in right-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(5)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the middle of the array&amp;quot; do  # Test case for element to be present in the middle of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(3)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should not be in the array&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here it is assumed that the method binary_search will return true/false based on whether the provided value exists in the array or not. It should produce the following output. The un-implemented tests are marked as Pending in the output.&lt;br /&gt;
&lt;br /&gt;
   BinarySearchTest&lt;br /&gt;
   - should be in the left-half of the array&lt;br /&gt;
   - should be in the right-half of the array&lt;br /&gt;
   - should be in the middle of the array&lt;br /&gt;
   - should not be in the array (PENDING: Not Yet Implemented)&lt;br /&gt;
 &lt;br /&gt;
   Pending:&lt;br /&gt;
   BinaryTestSearch should not be in the array (Not Yet Implemented)&lt;br /&gt;
     Called from binarysearch.rb:27&lt;br /&gt;
 &lt;br /&gt;
   Finished in 0.006682 seconds&lt;br /&gt;
 &lt;br /&gt;
   4 examples, 0 failures, 1 pending&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
====describe() method====&lt;br /&gt;
&lt;br /&gt;
The describe() method can take an arbitrary number of arguments and a block and returns a sub-class of Spec::Example::ExampleGroup. We generally use only one or two arguments which is used to describe the behavior. The first argument can be a reference to a Class or module or a string. The second argument is optional and should be a string when used.&lt;br /&gt;
&lt;br /&gt;
====it() method====&lt;br /&gt;
&lt;br /&gt;
Similar to the describe() method, the it() method takes a single String, an optional Hash and an optional block. The String expression within the it() should be such that it informs the behavior of the code within the block.&lt;br /&gt;
&lt;br /&gt;
==Expectations in RSpec==&lt;br /&gt;
&lt;br /&gt;
There are two methods available for checking expectations: should() and should_not(). Both the methods accept either an expression matcher or a Ruby expression using a specific subset of Ruby operators. An expression matcher is an objects that matches an expression.&lt;br /&gt;
&lt;br /&gt;
===Built-in Matchers===&lt;br /&gt;
&lt;br /&gt;
There are several matchers that can be used with should and should_not, which are divided into well-separated categories.&lt;br /&gt;
====Equality====&lt;br /&gt;
 subject.should == ece517&lt;br /&gt;
 subject.should === ece517&lt;br /&gt;
 subject.should eql(subject)&lt;br /&gt;
 subject.should equal(subject)&lt;br /&gt;
&lt;br /&gt;
The == method is used to express equivalence and equal is used when you want the receiver and the argument to be the same object. Instead of using !=, you should use the should_not method!&lt;br /&gt;
&lt;br /&gt;
====Floating Point Calculations====&lt;br /&gt;
 piValue.should be_close(3.14, 0.001593)&lt;br /&gt;
&lt;br /&gt;
Sometimes the values generated might be correct upto some fixed decimal positions, after that they may have slight variations. To avoid the test beings failed, we provide the (value, delta) to be_close method which passes the test if the obtained value lies within the range (value+delta).&lt;br /&gt;
&lt;br /&gt;
====Regular Expressions====&lt;br /&gt;
 resultExpression.should match(/this regular expression/)&lt;br /&gt;
 resultExpression.should =~ /this regular expression/&lt;br /&gt;
&lt;br /&gt;
This can be very useful when dealing with multiple-line expectations, instead of using the open file technique to compare contents.&lt;br /&gt;
&lt;br /&gt;
====Changes====&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.to(1)&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.from(0).to(1)&lt;br /&gt;
 &lt;br /&gt;
This is really useful when working with database changes or changes to objects. The matcher is change(), which takes a block and accepts the from(), to() or by() modifiers. &amp;lt;sup&amp;gt;[18]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Errors====&lt;br /&gt;
 field = CricketGround.new(:players =&amp;gt; 11)&lt;br /&gt;
 lambda {&lt;br /&gt;
  field.remove(:players, 15)&lt;br /&gt;
 }.should raise_error(NotEnoughPlayers,“attempted to remove more players than there is on cricket stadium”)&lt;br /&gt;
&lt;br /&gt;
Useful when needed to check for Exceptions. The matcher is raise_error and takes an ExceptionObject and/or a String/Regexp.&lt;br /&gt;
 &lt;br /&gt;
====Throw====&lt;br /&gt;
 speech = Speech.new(:seats =&amp;gt; 100)&lt;br /&gt;
 100.times { speech.register Person.new }&lt;br /&gt;
 lambda {&lt;br /&gt;
  speech.register Person.new&lt;br /&gt;
 }.should throw_symbol(:speech_full, 100)&lt;br /&gt;
&lt;br /&gt;
When dealing with “errors that are not really exceptions”, you use catch and throw. Rspec can check if a throw has been called by using the throw_symbol matcher. It accepts 0,1 or 2 arguments. The first argument needs to be a Symbol and the second can be any Object that is thrown along.&lt;br /&gt;
&lt;br /&gt;
===Predicate Matchers===&lt;br /&gt;
A Ruby predicate method is a method that ends with a “?” and returns a boolean value, like string.empty? or regexp.match? methods. Instead of writing:&lt;br /&gt;
 a_string.empty?.should == true&lt;br /&gt;
We can write using RSpec:&lt;br /&gt;
 a_string.should be_empty&lt;br /&gt;
&lt;br /&gt;
When using a be_something matcher, RSpec removes the “be_”, appends a “?” and calls the resulting method in the receiver. A very common construct of this method is be_true, which checks if the receiver is true (any object except false or nil) or false (false or nil).&lt;br /&gt;
&lt;br /&gt;
===Check Ownership===&lt;br /&gt;
Sometimes you will want to check something the object owns and not the object itself.&lt;br /&gt;
&lt;br /&gt;
====The have_something() method====&lt;br /&gt;
 security_access.has_key?(:id).should == true&lt;br /&gt;
is the same as&lt;br /&gt;
 security_access.should have_key(:id)&lt;br /&gt;
&lt;br /&gt;
RSpec uses method_missing to convert anything that begins with have_something to has_something? and performs the checking.&lt;br /&gt;
&lt;br /&gt;
====The have() method====&lt;br /&gt;
 field.players.select {|p| p.team == home_team }.length.should == 9&lt;br /&gt;
is the same as&lt;br /&gt;
 home_team.should have(9).players_on(field)&lt;br /&gt;
 &lt;br /&gt;
As have() does not respond to players_on(), it delegates to the receiver (home_team). It encourages the home_team object to have useful methods like players_on.&amp;lt;br&amp;gt;&lt;br /&gt;
You can get a NoMethodError if the players_on method doesn´t exist, you can get another NoMethodError if the result of the players_on method doesn´t respond to size() or length() and if the size of the collection doesn´t match the expected size, you will get a failed expectation. &amp;lt;sup&amp;gt;[http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Checking Collections Themselves===&lt;br /&gt;
Sometimes we create expectations about a collection itself and not about an owned collection. RSpec lets us use the have() method to express this as well, as in:&lt;br /&gt;
 basket_collection.should have(10).items&lt;br /&gt;
items is just providing some meaning to the expectation.&lt;br /&gt;
&lt;br /&gt;
====Strings====&lt;br /&gt;
Strings are not collections by definition but they respond to a lot of methods that collections do, like length() and size(). This allow us to use have() to expect a string of a specific length.&lt;br /&gt;
 “apple”.should have(5).characters&lt;br /&gt;
characters is just providing meaning to the expectation as well.&lt;br /&gt;
&lt;br /&gt;
====Have() modifiers for precision====&lt;br /&gt;
The have() method has some relatives that allow us to check for upper and lower conditions.&lt;br /&gt;
&lt;br /&gt;
 work.should have_exactly(8).hours&lt;br /&gt;
 classroom.should have_at_most(100).people&lt;br /&gt;
 bag.should have_at_least(5).items&lt;br /&gt;
&lt;br /&gt;
===Operator Expressions===&lt;br /&gt;
There may be sometimes when you want to expect a value to be not an exact amount but something like greater than or less than. RSpec allows you to do this by using the regular operators from Ruby!&lt;br /&gt;
&lt;br /&gt;
 number.should == 3&lt;br /&gt;
 number.should be &amp;gt;= 2&lt;br /&gt;
 number.should be &amp;lt;= 4&lt;br /&gt;
 number should be &amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Unit_testing Unit Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Programming &amp;amp; Unit Testing]. en.wikibooks.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Ruby Test::Unit]. ruby-doc.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Ruby Assertions]. ruby-doc.org. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html Shoulda Explained]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavior Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Agile_software_development Agile Software Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.springerlink.com/content/978-3-540-22839-4/ Proceedings of Extreme Programming and Agile Methods Conference] Carmen Zannier, Hakan Erdogmus and Lowell Lindstrom. &amp;lt;i&amp;gt;Extreme Programming and Agile Methods - XP/Agile Universe 2004&amp;lt;/i&amp;gt;. 4th Conference on Extreme Programming and Agile Methods, Calgary, Canada, August 15-18, 2004. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false Domain-driven design: tackling complexity in the heart of software]. By Eric Evans. books.google.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Domain-driven_design Domain Driven Design]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false Test-driven development: by example]. By Kent Beck. books. google.com. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Test-driven_development Test Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.davidchelimsky.net/ David Chelimsky Blog]. davidchelimsky.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/ Understanding RSpec Stories - A Tutorial]. emson.co.uk. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.dannorth.net/2007/06/17/introducing-rbehave/ rbehave]. dannorth.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-lang.org Ruby Website]. ruby-lang.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.pragprog.com/titles/achbd/the-rspec-book RSpec Book]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/ RSpec - Expectations]. wordpress.com. Retrieved Sep 17, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35442</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1f vn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35442"/>
		<updated>2010-09-18T01:49:57Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the different Unit-Testing Frameworks available for Ruby.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Unit testing is a method by which we can isolate and test a unit functionality of the program, typically individual methods during and long after the code is written. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing]&amp;lt;/sup&amp;gt; It helps to identify errors in the program even without running the entire program. It also helps to do regressing testing to identify buggy code additions in the future. Unit testing frameworks provides us with constructs which simplifies the process of unit testing. Using a standard unit test framework helps other developers to add test cases easily. &amp;lt;sup&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing]&amp;lt;/sup&amp;gt; This chapter walks through three different unit testing frameworks available for Ruby and explains how to use them with examples. The three commonly used unit testing frameworks for ruby are &lt;br /&gt;
&lt;br /&gt;
# Test::Unit&lt;br /&gt;
# Shoulda&lt;br /&gt;
# RSpec&lt;br /&gt;
&lt;br /&gt;
=Test::Unit=&lt;br /&gt;
&lt;br /&gt;
Now we shall consider Test::Unit framework&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
Ruby comes with an in-built, ready to use unit testing framework called Test::Unit. It is a XUnit type framework and typically have a setup method for initialization, a teardown method for cleanup and the actual test methods itself. The tests themselves are bundled separately in a test class from the code it is testing.&lt;br /&gt;
&lt;br /&gt;
==Test Fixture==&lt;br /&gt;
Test fixture represents the initial environment setup(eg. initialization data) and/or the expected outcome of the tests for that environment. This is typically done in the setup() and teardown() methods and it helps to separate test initialization and cleanup from the actual tests. It also helps to reuse the same fixture for more than one tests.&amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html]&amp;lt;/sup&amp;gt; &lt;br /&gt;
&lt;br /&gt;
For example, consider a method &amp;lt;i&amp;gt;prime_check(num)&amp;lt;/i&amp;gt; which takes an integer number as input and outputs whether it is prime number or not. In order to unit test this method we can create the following fixture containing a 2-dimensional array with a number and the expected output of whether it is prime or not.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @NUMBERS = [[3,true], [4,false], [7,true], [10,false]]    &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Assertions==&lt;br /&gt;
The core part of test::unit framework is the ability to assert a statement of expected outcome. If an assert statement is correct then the test will proceed, otherwise the test will fail. This feature helps us to verify the method under test with different types of inputs and track the results. Test::unit provides a bunch of assert methods for this purpose: &lt;br /&gt;
&lt;br /&gt;
{| border=1 cellspacing=0 cellpadding=5&lt;br /&gt;
| assert( boolean, [message] ) &lt;br /&gt;
| True if ''boolean''&lt;br /&gt;
|- &lt;br /&gt;
| assert_equal( expected, actual, [message] )&amp;lt;br&amp;gt;assert_not_equal( expected, actual, [message] )&lt;br /&gt;
| True if ''expected == actual''&lt;br /&gt;
|-&lt;br /&gt;
| assert_raise( Exception,... ) {block}&amp;lt;br&amp;gt;assert_nothing_raised( Exception,...) {block} &lt;br /&gt;
| True if the block raises (or doesn't) one of the listed exceptions.&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For the full list of assertion methods provided by test::unit refer to test::unit assertions. &amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
The test case class &amp;lt;i&amp;gt;BinarySearchTest&amp;lt;/i&amp;gt; subclasses the &amp;lt;i&amp;gt;Test::Unit::TestCase&amp;lt;/i&amp;gt; class and overrides &amp;lt;i&amp;gt;setup&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;teardown&amp;lt;/i&amp;gt; methods. The test methods should start with 'test_' prefix. This helps in isolating the test methods from the helper methods if any. The Test::Unit::TestCase class takes care of making the test methods into tests, wrapping them into a suite and running the individual tests. The test results are collected into &amp;lt;i&amp;gt;Test::Unit::TestResult&amp;lt;/i&amp;gt; object.&lt;br /&gt;
&lt;br /&gt;
    require 'test/unit'&lt;br /&gt;
    require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
    class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
 &lt;br /&gt;
      def setup&lt;br /&gt;
        @input_array = [1,2,3,4,5]      #The test fixture is initialized        &lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      def test_success_left_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,1),true)   #tests if the element present in left half of the array is found&lt;br /&gt;
        assert_equal(binary_search(@input_array,2),true)    &lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_right_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,5),true)   #tests if the element present in the right half of the array is found&lt;br /&gt;
        assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_middle                             #tests if the element present in the middle of the array is found&lt;br /&gt;
        assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_failure&lt;br /&gt;
        assert_equal(binary_search(@input_array,6),false)   #tests if an element not present in the array is not found&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def teardown&lt;br /&gt;
        #nothing to do here&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we have four test methods testing different logical paths of the binary search algorithm. Each test method can have one or more assert statements to test whether conditions are correct in each situation. To run the tests we simply have to run the file binary_search_test.rb and the output is as follows:&lt;br /&gt;
 &lt;br /&gt;
  Loaded suite binarysearch&lt;br /&gt;
  Started&lt;br /&gt;
  F...&lt;br /&gt;
  Finished in 0.372 seconds.&lt;br /&gt;
  &lt;br /&gt;
    1) Failure:&lt;br /&gt;
  test_failure(BinarySearchTest) [binarysearch.rb:25]:&lt;br /&gt;
  &amp;lt;true&amp;gt; expected but was&lt;br /&gt;
  &amp;lt;false&amp;gt;.&lt;br /&gt;
  &lt;br /&gt;
  4 tests, 6 assertions, 1 failures, 0 errors&lt;br /&gt;
&lt;br /&gt;
The results show that the last test case &amp;lt;i&amp;gt;test_failure&amp;lt;/i&amp;gt;, testing the negative scenario is failing. The reason is because the assert statement is expecting &amp;lt;i&amp;gt;false&amp;lt;/i&amp;gt; when number 6, which not present in the array is passed. But the binary_search method is returning true.&lt;br /&gt;
&lt;br /&gt;
==Test Suite==&lt;br /&gt;
Sometimes it is useful to combine a bunch of related test cases and run them as batch. Test::Unit provides a class called TestSuite for this purpose. The below example demonstrates how to bundle binary and sequential test case classes into a single search test suite.&lt;br /&gt;
&lt;br /&gt;
   require 'test/unit/testsuite'&lt;br /&gt;
   require 'binary_search_test'&lt;br /&gt;
   require 'sequential_search_test'&lt;br /&gt;
  &lt;br /&gt;
   class Search_Tests&lt;br /&gt;
     def self.suite&lt;br /&gt;
       suite = Test::Unit::TestSuite.new&lt;br /&gt;
       suite &amp;lt;&amp;lt; BinarySearchTest.suite&lt;br /&gt;
       suite &amp;lt;&amp;lt; SequentialSearchTest.suite&lt;br /&gt;
       return suite&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   Test::Unit::UI::Console::TestRunner.run(Search_Tests)&lt;br /&gt;
&lt;br /&gt;
=Shoulda=&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
One of the downsides of Test::Unit is we end up writing lots of code in order to test the actual code which is sometimes not easy to understand. Shoulda is a library that allows us to write better and more understandable tests for ruby application. Shoulda is not a testing framework by itself. It extends the Test::Unit framework with the idea of &amp;lt;i&amp;gt;context&amp;lt;/i&amp;gt;. We can mix Test::Unit test cases with Shoulda test cases. Shoulda allows us to provide context to the tests so that we can group the tests according to a specific feature or scenario. &amp;lt;sup&amp;gt;[http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
   require 'shoulda'&lt;br /&gt;
   require 'test/unit'&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
   class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
      &lt;br /&gt;
     context &amp;quot;Input array of size 5&amp;quot; do&lt;br /&gt;
      &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1,2,3,4,5]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the left half of the array&amp;quot; do    #tests if the element present in left half of the array is found&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the right half of the array&amp;quot; do   #tests if the element present in right half of the array is found&lt;br /&gt;
         assert_equal(binary_search(@input_array,5),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the middle of the array&amp;quot; do       #tests if the element present in middle of the array is found&lt;br /&gt;
         assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do                #tests if the element not present in the array is not found&lt;br /&gt;
         assert_equal(binary_search(@input_array,6),false)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
    &lt;br /&gt;
     context &amp;quot;Input array of size 1&amp;quot; do&lt;br /&gt;
     &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)           #tests if an element is found in the single element array&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do                 # tests if an element is not found in the single element array&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)         &lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Notice that we are still sub-classing the Test::Unit::TestCase class. In this example we have two contexts one for input array of size 5 and the other for input array of size 1. Each context has its own environment of setup/teardown methods. We can also create nested contexts - the outer setup gets run before the execution of each of the inner contexts. And the setup in the inner contexts gets run when running that context. Each &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; construct is converted into individual test methods and are run. If a test case fails we will get a better description of what that test case is doing from the &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; description.&lt;br /&gt;
&lt;br /&gt;
=RSpec=&lt;br /&gt;
&lt;br /&gt;
Now let us consider about &amp;lt;i&amp;gt;RSpec&amp;lt;/i&amp;gt; Testing Framework in detail.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Behaviour Driven Development&amp;lt;/b&amp;gt; (BDD) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Behavior_Driven_Development]&amp;lt;/sup&amp;gt; is an Agile development process &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Agile_software_development]&amp;lt;/sup&amp;gt; that comprises aspects of Acceptance Test Driven Planning &amp;lt;sup&amp;gt;[http://www.springerlink.com/content/978-3-540-22839-4/] [http://en.wikipedia.org/wiki/Acceptance_testing]&amp;lt;/sup&amp;gt;, Domain Driven Design &amp;lt;sup&amp;gt;[http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Domain-driven_design]&amp;lt;/sup&amp;gt; and Test Driven Development (TDD). &amp;lt;sup&amp;gt;[http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Test-driven_development]&amp;lt;/sup&amp;gt; &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;RSpec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; is a Behavioural Driven Development (BDD) tool aimed at Test Driven Development, originally created by Dave Astels and Steven Baker. However David Chelimsky &amp;lt;sup&amp;gt;[http://blog.davidchelimsky.net/]&amp;lt;/sup&amp;gt; is really the gatekeeper of the RSpec project. &amp;lt;sup&amp;gt;[http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Traditionally we use Unit Test frameworks like JUnit, NUnit or RUnit for writing Test cases. We spend a lot of time writing tests that test every unit of code in our software system. Instead we can shift our focus from Unit testing to Behaviour testing or Behaviour Driven Development (BDD) using RSpec. By focusing on the behaviour of the system it helps clarify in our minds what the system should actually be doing. It also helps us to perform more ‘useful’ tests. Useful tests, cover what the system should be doing and build in enough redundancy so that it should be easy to refactor our code without having to re-write every test.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
RSpec is really two projects merged into one. The RSpec project pages describes these merged projects as:&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;application level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Story Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;object level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Spec Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dan North created &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;rbehave&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; &amp;lt;sup&amp;gt;[http://blog.dannorth.net/2007/06/17/introducing-rbehave/]&amp;lt;/sup&amp;gt; which is the Story Framework and David Chelimsky created the &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;Spec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; Framework. By encompassing two frameworks RSpec equips a programmer with a thorough set of testing tools, allowing you to think about your software problem from a number of perspectives.&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
&lt;br /&gt;
The prerequisites are&lt;br /&gt;
&lt;br /&gt;
# Ruby 1.8.4 or later&lt;br /&gt;
# RSpec Gem (latest)&lt;br /&gt;
&lt;br /&gt;
To install Ruby, please visit official Ruby Website &amp;lt;sup&amp;gt;[http://www.ruby-lang.org/]&amp;lt;/sup&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
To install RSpec, open a command shell, go to /bin folder in Ruby directory and type&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;gt; gem install rspec&lt;br /&gt;
&lt;br /&gt;
==Terms &amp;amp; Definitions==&lt;br /&gt;
&lt;br /&gt;
Here are some terms which are used frequently while working with RSpec. &amp;lt;sup&amp;gt;[http://www.pragprog.com/titles/achbd/the-rspec-book/ ]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;b&amp;gt;subject code&amp;lt;/b&amp;gt; - The code whose behavior is specified using RSpec&lt;br /&gt;
# &amp;lt;b&amp;gt;expectation&amp;lt;/b&amp;gt; - The expected behavior of subject code is expressed using expectation (Similar to 'Assertions' statements used in Test::Unit or other tools in other languages)&lt;br /&gt;
# &amp;lt;b&amp;gt;code example&amp;lt;/b&amp;gt; - An executable example containing the subject code and the expectations (Similar to 'Test Method' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;example group&amp;lt;/b&amp;gt; - A group of code examples (Similar to 'Test Case' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;spec file&amp;lt;/b&amp;gt; - A file which contains one or more example groups&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Let us go through an example to be clear on the usage of RSpec.&lt;br /&gt;
&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
   &lt;br /&gt;
   describe BinarySearchTest do&lt;br /&gt;
     before(:all) do&lt;br /&gt;
       @input_array = [1, 2, 3, 4, 5] # The Input Array&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     after(:all) do&lt;br /&gt;
       # do nothing here&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the left-half of the array&amp;quot; do  # Test case for element to be present in left-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(1)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the right-half of the array&amp;quot; do  # Test case for element to be present in right-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(5)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the middle of the array&amp;quot; do  # Test case for element to be present in the middle of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(3)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should not be in the array&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here it is assumed that the method binary_search will return true/false based on whether the provided value exists in the array or not. It should produce the following output. The un-implemented tests are marked as Pending in the output.&lt;br /&gt;
&lt;br /&gt;
   BinarySearchTest&lt;br /&gt;
   - should be in the left-half of the array&lt;br /&gt;
   - should be in the right-half of the array&lt;br /&gt;
   - should be in the middle of the array&lt;br /&gt;
   - should not be in the array (PENDING: Not Yet Implemented)&lt;br /&gt;
 &lt;br /&gt;
   Pending:&lt;br /&gt;
   BinaryTestSearch should not be in the array (Not Yet Implemented)&lt;br /&gt;
     Called from binarysearch.rb:22&lt;br /&gt;
 &lt;br /&gt;
   Finished in 0.006682 seconds&lt;br /&gt;
 &lt;br /&gt;
   4 examples, 0 failures, 1 pending&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
====describe() method====&lt;br /&gt;
&lt;br /&gt;
The describe() method can take an arbitrary number of arguments and a block and returns a sub-class of Spec::Example::ExampleGroup. We generally use only one or two arguments which is used to describe the behavior. The first argument can be a reference to a Class or module or a string. The second argument is optional and should be a string when used.&lt;br /&gt;
&lt;br /&gt;
====it() method====&lt;br /&gt;
&lt;br /&gt;
Similar to the describe() method, the it() method takes a single String, an optional Hash and an optional block. The String expression within the it() should be such that it informs the behavior of the code within the block.&lt;br /&gt;
&lt;br /&gt;
==Expectations in RSpec==&lt;br /&gt;
&lt;br /&gt;
There are two methods available for checking expectations: should() and should_not(). Both the methods accept either an expression matcher or a Ruby expression using a specific subset of Ruby operators. An expression matcher is an objects that matches an expression.&lt;br /&gt;
&lt;br /&gt;
===Built-in Matchers===&lt;br /&gt;
&lt;br /&gt;
There are several matchers that can be used with should and should_not, which are divided into well-separated categories.&lt;br /&gt;
====Equality====&lt;br /&gt;
 subject.should == ece517&lt;br /&gt;
 subject.should === ece517&lt;br /&gt;
 subject.should eql(subject)&lt;br /&gt;
 subject.should equal(subject)&lt;br /&gt;
&lt;br /&gt;
The == method is used to express equivalence and equal is used when you want the receiver and the argument to be the same object. Instead of using !=, you should use the should_not method!&lt;br /&gt;
&lt;br /&gt;
====Floating Point Calculations====&lt;br /&gt;
 piValue.should be_close(3.14, 0.001593)&lt;br /&gt;
&lt;br /&gt;
Sometimes the values generated might be correct upto some fixed decimal positions, after that they may have slight variations. To avoid the test beings failed, we provide the (value, delta) to be_close method which passes the test if the obtained value lies within the range (value+delta).&lt;br /&gt;
&lt;br /&gt;
====Regular Expressions====&lt;br /&gt;
 resultExpression.should match(/this regular expression/)&lt;br /&gt;
 resultExpression.should =~ /this regular expression/&lt;br /&gt;
&lt;br /&gt;
This can be very useful when dealing with multiple-line expectations, instead of using the open file technique to compare contents.&lt;br /&gt;
&lt;br /&gt;
====Changes====&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.to(1)&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.from(0).to(1)&lt;br /&gt;
 &lt;br /&gt;
This is really useful when working with database changes or changes to objects. The matcher is change(), which takes a block and accepts the from(), to() or by() modifiers. &amp;lt;sup&amp;gt;[18]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Errors====&lt;br /&gt;
 field = CricketGround.new(:players =&amp;gt; 11)&lt;br /&gt;
 lambda {&lt;br /&gt;
  field.remove(:players, 15)&lt;br /&gt;
 }.should raise_error(NotEnoughPlayers,“attempted to remove more players than there is on cricket stadium”)&lt;br /&gt;
&lt;br /&gt;
Useful when needed to check for Exceptions. The matcher is raise_error and takes an ExceptionObject and/or a String/Regexp.&lt;br /&gt;
 &lt;br /&gt;
====Throw====&lt;br /&gt;
 speech = Speech.new(:seats =&amp;gt; 100)&lt;br /&gt;
 100.times { speech.register Person.new }&lt;br /&gt;
 lambda {&lt;br /&gt;
  speech.register Person.new&lt;br /&gt;
 }.should throw_symbol(:speech_full, 100)&lt;br /&gt;
&lt;br /&gt;
When dealing with “errors that are not really exceptions”, you use catch and throw. Rspec can check if a throw has been called by using the throw_symbol matcher. It accepts 0,1 or 2 arguments. The first argument needs to be a Symbol and the second can be any Object that is thrown along.&lt;br /&gt;
&lt;br /&gt;
===Predicate Matchers===&lt;br /&gt;
A Ruby predicate method is a method that ends with a “?” and returns a boolean value, like string.empty? or regexp.match? methods. Instead of writing:&lt;br /&gt;
 a_string.empty?.should == true&lt;br /&gt;
We can write using RSpec:&lt;br /&gt;
 a_string.should be_empty&lt;br /&gt;
&lt;br /&gt;
When using a be_something matcher, RSpec removes the “be_”, appends a “?” and calls the resulting method in the receiver. A very common construct of this method is be_true, which checks if the receiver is true (any object except false or nil) or false (false or nil).&lt;br /&gt;
&lt;br /&gt;
===Check Ownership===&lt;br /&gt;
Sometimes you will want to check something the object owns and not the object itself.&lt;br /&gt;
&lt;br /&gt;
====The have_something() method====&lt;br /&gt;
 security_access.has_key?(:id).should == true&lt;br /&gt;
is the same as&lt;br /&gt;
 security_access.should have_key(:id)&lt;br /&gt;
&lt;br /&gt;
RSpec uses method_missing to convert anything that begins with have_something to has_something? and performs the checking.&lt;br /&gt;
&lt;br /&gt;
====The have() method====&lt;br /&gt;
 field.players.select {|p| p.team == home_team }.length.should == 9&lt;br /&gt;
is the same as&lt;br /&gt;
 home_team.should have(9).players_on(field)&lt;br /&gt;
 &lt;br /&gt;
As have() does not respond to players_on(), it delegates to the receiver (home_team). It encourages the home_team object to have useful methods like players_on.&amp;lt;br&amp;gt;&lt;br /&gt;
You can get a NoMethodError if the players_on method doesn´t exist, you can get another NoMethodError if the result of the players_on method doesn´t respond to size() or length() and if the size of the collection doesn´t match the expected size, you will get a failed expectation. &amp;lt;sup&amp;gt;[http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Checking Collections Themselves===&lt;br /&gt;
Sometimes we create expectations about a collection itself and not about an owned collection. RSpec lets us use the have() method to express this as well, as in:&lt;br /&gt;
 basket_collection.should have(10).items&lt;br /&gt;
items is just providing some meaning to the expectation.&lt;br /&gt;
&lt;br /&gt;
====Strings====&lt;br /&gt;
Strings are not collections by definition but they respond to a lot of methods that collections do, like length() and size(). This allow us to use have() to expect a string of a specific length.&lt;br /&gt;
 “apple”.should have(5).characters&lt;br /&gt;
characters is just providing meaning to the expectation as well.&lt;br /&gt;
&lt;br /&gt;
====Have() modifiers for precision====&lt;br /&gt;
The have() method has some relatives that allow us to check for upper and lower conditions.&lt;br /&gt;
&lt;br /&gt;
 work.should have_exactly(8).hours&lt;br /&gt;
 classroom.should have_at_most(100).people&lt;br /&gt;
 bag.should have_at_least(5).items&lt;br /&gt;
&lt;br /&gt;
===Operator Expressions===&lt;br /&gt;
There may be sometimes when you want to expect a value to be not an exact amount but something like greater than or less than. RSpec allows you to do this by using the regular operators from Ruby!&lt;br /&gt;
&lt;br /&gt;
 number.should == 3&lt;br /&gt;
 number.should be &amp;gt;= 2&lt;br /&gt;
 number.should be &amp;lt;= 4&lt;br /&gt;
 number should be &amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Unit_testing Unit Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Programming &amp;amp; Unit Testing]. en.wikibooks.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Ruby Test::Unit]. ruby-doc.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Ruby Assertions]. ruby-doc.org. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html Shoulda Explained]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavior Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Agile_software_development Agile Software Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.springerlink.com/content/978-3-540-22839-4/ Proceedings of Extreme Programming and Agile Methods Conference] Carmen Zannier, Hakan Erdogmus and Lowell Lindstrom. &amp;lt;i&amp;gt;Extreme Programming and Agile Methods - XP/Agile Universe 2004&amp;lt;/i&amp;gt;. 4th Conference on Extreme Programming and Agile Methods, Calgary, Canada, August 15-18, 2004. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false Domain-driven design: tackling complexity in the heart of software]. By Eric Evans. books.google.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Domain-driven_design Domain Driven Design]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false Test-driven development: by example]. By Kent Beck. books. google.com. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Test-driven_development Test Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.davidchelimsky.net/ David Chelimsky Blog]. davidchelimsky.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/ Understanding RSpec Stories - A Tutorial]. emson.co.uk. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.dannorth.net/2007/06/17/introducing-rbehave/ rbehave]. dannorth.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-lang.org Ruby Website]. ruby-lang.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.pragprog.com/titles/achbd/the-rspec-book RSpec Book]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/ RSpec - Expectations]. wordpress.com. Retrieved Sep 17, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35436</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1f vn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35436"/>
		<updated>2010-09-18T01:47:43Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the different Unit-Testing Frameworks available for Ruby.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Unit testing is a method by which we can isolate and test a unit functionality of the program, typically individual methods during and long after the code is written. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing]&amp;lt;/sup&amp;gt; It helps to identify errors in the program even without running the entire program. It also helps to do regressing testing to identify buggy code additions in the future. Unit testing frameworks provides us with constructs which simplifies the process of unit testing. Using a standard unit test framework helps other developers to add test cases easily. &amp;lt;sup&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing]&amp;lt;/sup&amp;gt; This chapter walks through three different unit testing frameworks available for Ruby and explains how to use them with examples. The three commonly used unit testing frameworks for ruby are &lt;br /&gt;
&lt;br /&gt;
# Test::Unit&lt;br /&gt;
# Shoulda&lt;br /&gt;
# RSpec&lt;br /&gt;
&lt;br /&gt;
=Test::Unit=&lt;br /&gt;
&lt;br /&gt;
Now we shall consider Test::Unit framework&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
Ruby comes with an in-built, ready to use unit testing framework called Test::Unit. It is a XUnit type framework and typically have a setup method for initialization, a teardown method for cleanup and the actual test methods itself. The tests themselves are bundled separately in a test class from the code it is testing.&lt;br /&gt;
&lt;br /&gt;
==Test Fixture==&lt;br /&gt;
Test fixture represents the initial environment setup(eg. initialization data) and/or the expected outcome of the tests for that environment. This is typically done in the setup() and teardown() methods and it helps to separate test initialization and cleanup from the actual tests. It also helps to reuse the same fixture for more than one tests.&amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html]&amp;lt;/sup&amp;gt; &lt;br /&gt;
&lt;br /&gt;
For example, consider a method &amp;lt;i&amp;gt;prime_check(num)&amp;lt;/i&amp;gt; which takes an integer number as input and outputs whether it is prime number or not. In order to unit test this method we can create the following fixture containing a 2-dimensional array with a number and the expected output of whether it is prime or not.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @NUMBERS = [[3,true], [4,false], [7,true], [10,false]]    &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Assertions==&lt;br /&gt;
The core part of test::unit framework is the ability to assert a statement of expected outcome. If an assert statement is correct then the test will proceed, otherwise the test will fail. This feature helps us to verify the method under test with different types of inputs and track the results. Test::unit provides a bunch of assert methods for this purpose: &lt;br /&gt;
&lt;br /&gt;
{| border=1 cellspacing=0 cellpadding=5&lt;br /&gt;
| assert( boolean, [message] ) &lt;br /&gt;
| True if ''boolean''&lt;br /&gt;
|- &lt;br /&gt;
| assert_equal( expected, actual, [message] )&amp;lt;br&amp;gt;assert_not_equal( expected, actual, [message] )&lt;br /&gt;
| True if ''expected == actual''&lt;br /&gt;
|-&lt;br /&gt;
| assert_raise( Exception,... ) {block}&amp;lt;br&amp;gt;assert_nothing_raised( Exception,...) {block} &lt;br /&gt;
| True if the block raises (or doesn't) one of the listed exceptions.&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For the full list of assertion methods provided by test::unit refer to test::unit assertions. &amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
The test case class &amp;lt;i&amp;gt;BinarySearchTest&amp;lt;/i&amp;gt; subclasses the &amp;lt;i&amp;gt;Test::Unit::TestCase&amp;lt;/i&amp;gt; class and overrides &amp;lt;i&amp;gt;setup&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;teardown&amp;lt;/i&amp;gt; methods. The test methods should start with 'test_' prefix. This helps in isolating the test methods from the helper methods if any. The Test::Unit::TestCase class takes care of making the test methods into tests, wrapping them into a suite and running the individual tests. The test results are collected into &amp;lt;i&amp;gt;Test::Unit::TestResult&amp;lt;/i&amp;gt; object.&lt;br /&gt;
&lt;br /&gt;
    require 'test/unit'&lt;br /&gt;
    require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
    class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
 &lt;br /&gt;
      def setup&lt;br /&gt;
        @input_array = [1,2,3,4,5]      #The test fixture is initialized        &lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      def test_success_left_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,1),true)   #tests if the element present in left half of the array is found&lt;br /&gt;
        assert_equal(binary_search(@input_array,2),true)    &lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_right_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,5),true)   #tests if the element present in the right half of the array is found&lt;br /&gt;
        assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_middle                             #tests if the element present in the middle of the array is found&lt;br /&gt;
        assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_failure&lt;br /&gt;
        assert_equal(binary_search(@input_array,6),false)   #tests if an element not present in the array is not found&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def teardown&lt;br /&gt;
        #nothing to do here&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we have four test methods testing different logical paths of the binary search algorithm. Each test method can have one or more assert statements to test whether conditions are correct in each situation. To run the tests we simply have to run the file binary_search_test.rb and the output is as follows:&lt;br /&gt;
 &lt;br /&gt;
  Loaded suite binarysearch&lt;br /&gt;
  Started&lt;br /&gt;
  F...&lt;br /&gt;
  Finished in 0.372 seconds.&lt;br /&gt;
  &lt;br /&gt;
    1) Failure:&lt;br /&gt;
  test_failure(BinarySearchTest) [binarysearch.rb:25]:&lt;br /&gt;
  &amp;lt;true&amp;gt; expected but was&lt;br /&gt;
  &amp;lt;false&amp;gt;.&lt;br /&gt;
  &lt;br /&gt;
  4 tests, 6 assertions, 1 failures, 0 errors&lt;br /&gt;
&lt;br /&gt;
The results show that the last test case &amp;lt;i&amp;gt;test_failure&amp;lt;/i&amp;gt;, testing the negative scenario is failing. The reason is because the assert statement is expecting &amp;lt;i&amp;gt;false&amp;lt;/i&amp;gt; when number 6, which not present in the array is passed. But the binary_search method is returning true.&lt;br /&gt;
&lt;br /&gt;
==Test Suite==&lt;br /&gt;
Sometimes it is useful to combine a bunch of related test cases and run them as batch. Test::Unit provides a class called TestSuite for this purpose. The below example demonstrates how to bundle binary and sequential test case classes into a single search test suite.&lt;br /&gt;
&lt;br /&gt;
   require 'test/unit/testsuite'&lt;br /&gt;
   require 'binary_search_test'&lt;br /&gt;
   require 'sequential_search_test'&lt;br /&gt;
  &lt;br /&gt;
   class Search_Tests&lt;br /&gt;
     def self.suite&lt;br /&gt;
       suite = Test::Unit::TestSuite.new&lt;br /&gt;
       suite &amp;lt;&amp;lt; BinarySearchTest.suite&lt;br /&gt;
       suite &amp;lt;&amp;lt; SequentialSearchTest.suite&lt;br /&gt;
       return suite&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   Test::Unit::UI::Console::TestRunner.run(Search_Tests)&lt;br /&gt;
&lt;br /&gt;
=Shoulda=&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
One of the downsides of Test::Unit is we end up writing lots of code in order to test the actual code which is sometimes not easy to understand. Shoulda is a library that allows us to write better and more understandable tests for ruby application. Shoulda is not a testing framework by itself. It extends the Test::Unit framework with the idea of &amp;lt;i&amp;gt;context&amp;lt;/i&amp;gt;. We can mix Test::Unit test cases with Shoulda test cases. Shoulda allows us to provide context to the tests so that we can group the tests according to a specific feature or scenario. &amp;lt;sup&amp;gt;[http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
   require 'shoulda'&lt;br /&gt;
   require 'test/unit'&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
   class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
      &lt;br /&gt;
     context &amp;quot;Input array of size 5&amp;quot; do&lt;br /&gt;
      &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1,2,3,4,5]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the left half of the array&amp;quot; do    #tests if the element present in left half of the array is found&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the right half of the array&amp;quot; do   #tests if the element present in right half of the array is found&lt;br /&gt;
         assert_equal(binary_search(@input_array,5),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the middle of the array&amp;quot; do       #tests if the element present in middle of the array is found&lt;br /&gt;
         assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do                #tests if the element not present in the array is not found&lt;br /&gt;
         assert_equal(binary_search(@input_array,6),false)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
    &lt;br /&gt;
     context &amp;quot;Input array of size 1&amp;quot; do&lt;br /&gt;
     &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)           #tests if an element is found in the single element array&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do                 # tests if an element is not found in the single element array&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)         &lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Notice that we are still sub-classing the Test::Unit::TestCase class. In this example we have two contexts one for input array of size 5 and the other for input array of size 1. Each context has its own environment of setup/teardown methods. We can also create nested contexts - the outer setup gets run before the execution of each of the inner contexts. And the setup in the inner contexts gets run when running that context. Each &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; construct is converted into individual test methods and are run. If a test case fails we will get a better description of what that test case is doing from the &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; description.&lt;br /&gt;
&lt;br /&gt;
=RSpec=&lt;br /&gt;
&lt;br /&gt;
Now let us consider about &amp;lt;i&amp;gt;RSpec&amp;lt;/i&amp;gt; Testing Framework in detail.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Behaviour Driven Development&amp;lt;/b&amp;gt; (BDD) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Behavior_Driven_Development]&amp;lt;/sup&amp;gt; is an Agile development process &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Agile_software_development]&amp;lt;/sup&amp;gt; that comprises aspects of Acceptance Test Driven Planning &amp;lt;sup&amp;gt;[http://www.springerlink.com/content/978-3-540-22839-4/] [http://en.wikipedia.org/wiki/Acceptance_testing]&amp;lt;/sup&amp;gt;, Domain Driven Design &amp;lt;sup&amp;gt;[http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Domain-driven_design]&amp;lt;/sup&amp;gt; and Test Driven Development (TDD). &amp;lt;sup&amp;gt;[http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Test-driven_development]&amp;lt;/sup&amp;gt; &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;RSpec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; is a Behavioural Driven Development (BDD) tool aimed at Test Driven Development, originally created by Dave Astels and Steven Baker. However David Chelimsky &amp;lt;sup&amp;gt;[http://blog.davidchelimsky.net/]&amp;lt;/sup&amp;gt; is really the gatekeeper of the RSpec project. &amp;lt;sup&amp;gt;[http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Traditionally we use Unit Test frameworks like JUnit, NUnit or RUnit for writing Test cases. We spend a lot of time writing tests that test every unit of code in our software system. Instead we can shift our focus from Unit testing to Behaviour testing or Behaviour Driven Development (BDD) using RSpec. By focusing on the behaviour of the system it helps clarify in our minds what the system should actually be doing. It also helps us to perform more ‘useful’ tests. Useful tests, cover what the system should be doing and build in enough redundancy so that it should be easy to refactor our code without having to re-write every test.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
RSpec is really two projects merged into one. The RSpec project pages describes these merged projects as:&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;application level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Story Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;object level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Spec Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dan North created &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;rbehave&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; &amp;lt;sup&amp;gt;[http://blog.dannorth.net/2007/06/17/introducing-rbehave/]&amp;lt;/sup&amp;gt; which is the Story Framework and David Chelimsky created the &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;Spec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; Framework. By encompassing two frameworks RSpec equips a programmer with a thorough set of testing tools, allowing you to think about your software problem from a number of perspectives.&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
&lt;br /&gt;
The prerequisites are&lt;br /&gt;
&lt;br /&gt;
# Ruby 1.8.4 or later&lt;br /&gt;
# RSpec Gem (latest)&lt;br /&gt;
&lt;br /&gt;
To install Ruby, please visit official Ruby Website &amp;lt;sup&amp;gt;[http://www.ruby-lang.org/]&amp;lt;/sup&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
To install RSpec, open a command shell, go to /bin folder in Ruby directory and type&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;gt; gem install rspec&lt;br /&gt;
&lt;br /&gt;
==Terms &amp;amp; Definitions==&lt;br /&gt;
&lt;br /&gt;
Here are some terms which are used frequently while working with RSpec. &amp;lt;sup&amp;gt;[http://www.pragprog.com/titles/achbd/the-rspec-book/ ]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;b&amp;gt;subject code&amp;lt;/b&amp;gt; - The code whose behavior is specified using RSpec&lt;br /&gt;
# &amp;lt;b&amp;gt;expectation&amp;lt;/b&amp;gt; - The expected behavior of subject code is expressed using expectation (Similar to 'Assertions' statements used in Test::Unit or other tools in other languages)&lt;br /&gt;
# &amp;lt;b&amp;gt;code example&amp;lt;/b&amp;gt; - An executable example containing the subject code and the expectations (Similar to 'Test Method' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;example group&amp;lt;/b&amp;gt; - A group of code examples (Similar to 'Test Case' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;spec file&amp;lt;/b&amp;gt; - A file which contains one or more example groups&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Let us go through an example to be clear on the usage of RSpec.&lt;br /&gt;
&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
   &lt;br /&gt;
   describe BinarySearchTest do&lt;br /&gt;
     before(:all) do&lt;br /&gt;
       @input_array = [1, 2, 3, 4, 5] # The Input Array&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     after(:all) do&lt;br /&gt;
       # do nothing here&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the left-half of the array&amp;quot; do  # Test case for element to be present in left-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(1)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the right-half of the array&amp;quot; do  # Test case for element to be present in right-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(5)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the middle of the array&amp;quot; do  # Test case for element to be present in the middle of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(3)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should not be in the array&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here it is assumed that the method binary_search will return true/false based on whether the provided value exists in the array or not. It should produce the following output.&lt;br /&gt;
&lt;br /&gt;
   BinarySearchTest&lt;br /&gt;
   - should be in the left-half of the array&lt;br /&gt;
   - should be in the right-half of the array&lt;br /&gt;
   - should be in the middle of the array&lt;br /&gt;
   - should not be in the array (PENDING: Not Yet Implemented)&lt;br /&gt;
 &lt;br /&gt;
   Pending:&lt;br /&gt;
   BinaryTestSearch should not be in the array (Not Yet Implemented)&lt;br /&gt;
     Called from binarysearch.rb:22&lt;br /&gt;
 &lt;br /&gt;
   Finished in 0.006682 seconds&lt;br /&gt;
 &lt;br /&gt;
   4 examples, 0 failures, 1 pending&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
====describe() method====&lt;br /&gt;
&lt;br /&gt;
The describe() method can take an arbitrary number of arguments and a block and returns a sub-class of Spec::Example::ExampleGroup. We generally use only one or two arguments which is used to describe the behavior. The first argument can be a reference to a Class or module or a string. The second argument is optional and should be a string when used.&lt;br /&gt;
&lt;br /&gt;
====it() method====&lt;br /&gt;
&lt;br /&gt;
Similar to the describe() method, the it() method takes a single String, an optional Hash and an optional block. The String expression within the it() should be such that it informs the behavior of the code within the block.&lt;br /&gt;
&lt;br /&gt;
==Expectations in RSpec==&lt;br /&gt;
&lt;br /&gt;
There are two methods available for checking expectations: should() and should_not(). Both the methods accept either an expression matcher or a Ruby expression using a specific subset of Ruby operators. An expression matcher is an objects that matches an expression.&lt;br /&gt;
&lt;br /&gt;
===Built-in Matchers===&lt;br /&gt;
&lt;br /&gt;
There are several matchers that can be used with should and should_not, which are divided into well-separated categories.&lt;br /&gt;
====Equality====&lt;br /&gt;
 subject.should == ece517&lt;br /&gt;
 subject.should === ece517&lt;br /&gt;
 subject.should eql(subject)&lt;br /&gt;
 subject.should equal(subject)&lt;br /&gt;
&lt;br /&gt;
The == method is used to express equivalence and equal is used when you want the receiver and the argument to be the same object. Instead of using !=, you should use the should_not method!&lt;br /&gt;
&lt;br /&gt;
====Floating Point Calculations====&lt;br /&gt;
 piValue.should be_close(3.14, 0.001593)&lt;br /&gt;
&lt;br /&gt;
Sometimes the values generated might be correct upto some fixed decimal positions, after that they may have slight variations. To avoid the test beings failed, we provide the (value, delta) to be_close method which passes the test if the obtained value lies within the range (value+delta).&lt;br /&gt;
&lt;br /&gt;
====Regular Expressions====&lt;br /&gt;
 resultExpression.should match(/this regular expression/)&lt;br /&gt;
 resultExpression.should =~ /this regular expression/&lt;br /&gt;
&lt;br /&gt;
This can be very useful when dealing with multiple-line expectations, instead of using the open file technique to compare contents.&lt;br /&gt;
&lt;br /&gt;
====Changes====&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.to(1)&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.from(0).to(1)&lt;br /&gt;
 &lt;br /&gt;
This is really useful when working with database changes or changes to objects. The matcher is change(), which takes a block and accepts the from(), to() or by() modifiers. &amp;lt;sup&amp;gt;[18]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Errors====&lt;br /&gt;
 field = CricketGround.new(:players =&amp;gt; 11)&lt;br /&gt;
 lambda {&lt;br /&gt;
  field.remove(:players, 15)&lt;br /&gt;
 }.should raise_error(NotEnoughPlayers,“attempted to remove more players than there is on cricket stadium”)&lt;br /&gt;
&lt;br /&gt;
Useful when needed to check for Exceptions. The matcher is raise_error and takes an ExceptionObject and/or a String/Regexp.&lt;br /&gt;
 &lt;br /&gt;
====Throw====&lt;br /&gt;
 speech = Speech.new(:seats =&amp;gt; 100)&lt;br /&gt;
 100.times { speech.register Person.new }&lt;br /&gt;
 lambda {&lt;br /&gt;
  speech.register Person.new&lt;br /&gt;
 }.should throw_symbol(:speech_full, 100)&lt;br /&gt;
&lt;br /&gt;
When dealing with “errors that are not really exceptions”, you use catch and throw. Rspec can check if a throw has been called by using the throw_symbol matcher. It accepts 0,1 or 2 arguments. The first argument needs to be a Symbol and the second can be any Object that is thrown along.&lt;br /&gt;
&lt;br /&gt;
===Predicate Matchers===&lt;br /&gt;
A Ruby predicate method is a method that ends with a “?” and returns a boolean value, like string.empty? or regexp.match? methods. Instead of writing:&lt;br /&gt;
 a_string.empty?.should == true&lt;br /&gt;
We can write using RSpec:&lt;br /&gt;
 a_string.should be_empty&lt;br /&gt;
&lt;br /&gt;
When using a be_something matcher, RSpec removes the “be_”, appends a “?” and calls the resulting method in the receiver. A very common construct of this method is be_true, which checks if the receiver is true (any object except false or nil) or false (false or nil).&lt;br /&gt;
&lt;br /&gt;
===Check Ownership===&lt;br /&gt;
Sometimes you will want to check something the object owns and not the object itself.&lt;br /&gt;
&lt;br /&gt;
====The have_something() method====&lt;br /&gt;
 security_access.has_key?(:id).should == true&lt;br /&gt;
is the same as&lt;br /&gt;
 security_access.should have_key(:id)&lt;br /&gt;
&lt;br /&gt;
RSpec uses method_missing to convert anything that begins with have_something to has_something? and performs the checking.&lt;br /&gt;
&lt;br /&gt;
====The have() method====&lt;br /&gt;
 field.players.select {|p| p.team == home_team }.length.should == 9&lt;br /&gt;
is the same as&lt;br /&gt;
 home_team.should have(9).players_on(field)&lt;br /&gt;
 &lt;br /&gt;
As have() does not respond to players_on(), it delegates to the receiver (home_team). It encourages the home_team object to have useful methods like players_on.&amp;lt;br&amp;gt;&lt;br /&gt;
You can get a NoMethodError if the players_on method doesn´t exist, you can get another NoMethodError if the result of the players_on method doesn´t respond to size() or length() and if the size of the collection doesn´t match the expected size, you will get a failed expectation. &amp;lt;sup&amp;gt;[http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Checking Collections Themselves===&lt;br /&gt;
Sometimes we create expectations about a collection itself and not about an owned collection. RSpec lets us use the have() method to express this as well, as in:&lt;br /&gt;
 basket_collection.should have(10).items&lt;br /&gt;
items is just providing some meaning to the expectation.&lt;br /&gt;
&lt;br /&gt;
====Strings====&lt;br /&gt;
Strings are not collections by definition but they respond to a lot of methods that collections do, like length() and size(). This allow us to use have() to expect a string of a specific length.&lt;br /&gt;
 “apple”.should have(5).characters&lt;br /&gt;
characters is just providing meaning to the expectation as well.&lt;br /&gt;
&lt;br /&gt;
====Have() modifiers for precision====&lt;br /&gt;
The have() method has some relatives that allow us to check for upper and lower conditions.&lt;br /&gt;
&lt;br /&gt;
 work.should have_exactly(8).hours&lt;br /&gt;
 classroom.should have_at_most(100).people&lt;br /&gt;
 bag.should have_at_least(5).items&lt;br /&gt;
&lt;br /&gt;
===Operator Expressions===&lt;br /&gt;
There may be sometimes when you want to expect a value to be not an exact amount but something like greater than or less than. RSpec allows you to do this by using the regular operators from Ruby!&lt;br /&gt;
&lt;br /&gt;
 number.should == 3&lt;br /&gt;
 number.should be &amp;gt;= 2&lt;br /&gt;
 number.should be &amp;lt;= 4&lt;br /&gt;
 number should be &amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Unit_testing Unit Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Programming &amp;amp; Unit Testing]. en.wikibooks.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Ruby Test::Unit]. ruby-doc.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Ruby Assertions]. ruby-doc.org. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html Shoulda Explained]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavior Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Agile_software_development Agile Software Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.springerlink.com/content/978-3-540-22839-4/ Proceedings of Extreme Programming and Agile Methods Conference] Carmen Zannier, Hakan Erdogmus and Lowell Lindstrom. &amp;lt;i&amp;gt;Extreme Programming and Agile Methods - XP/Agile Universe 2004&amp;lt;/i&amp;gt;. 4th Conference on Extreme Programming and Agile Methods, Calgary, Canada, August 15-18, 2004. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false Domain-driven design: tackling complexity in the heart of software]. By Eric Evans. books.google.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Domain-driven_design Domain Driven Design]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false Test-driven development: by example]. By Kent Beck. books. google.com. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Test-driven_development Test Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.davidchelimsky.net/ David Chelimsky Blog]. davidchelimsky.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/ Understanding RSpec Stories - A Tutorial]. emson.co.uk. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.dannorth.net/2007/06/17/introducing-rbehave/ rbehave]. dannorth.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-lang.org Ruby Website]. ruby-lang.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.pragprog.com/titles/achbd/the-rspec-book RSpec Book]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/ RSpec - Expectations]. wordpress.com. Retrieved Sep 17, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35368</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1f vn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35368"/>
		<updated>2010-09-18T01:15:48Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the different Unit-Testing Frameworks available for Ruby.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Unit testing is a method by which we can isolate and test a unit functionality of the program, typically individual methods during and long after the code is written. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing]&amp;lt;/sup&amp;gt; It helps to identify errors in the program even without running the entire program. It also helps to do regressing testing to identify buggy code additions in the future. Unit testing frameworks provides us with constructs which simplifies the process of unit testing. Using a standard unit test framework helps other developers to add test cases easily. &amp;lt;sup&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing]&amp;lt;/sup&amp;gt; This chapter walks through three different unit testing frameworks available for Ruby and explains how to use them with examples. The three commonly used unit testing frameworks for ruby are &lt;br /&gt;
&lt;br /&gt;
# Test::Unit&lt;br /&gt;
# Shoulda&lt;br /&gt;
# RSpec&lt;br /&gt;
&lt;br /&gt;
=Test::Unit=&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
Ruby comes with an in-built, ready to use unit testing framework called Test::Unit. It is a XUnit type framework and typically have a setup method for initialization, a teardown method for cleanup and the actual test methods itself. The tests themselves are bundled separately in a test class from the code it is testing.&lt;br /&gt;
&lt;br /&gt;
==Test Fixture==&lt;br /&gt;
Test fixture represents the initial environment setup(eg. initialization data) and/or the expected outcome of the tests for that environment. This is typically done in the setup() and teardown() methods and it helps to separate test initialization and cleanup from the actual tests. It also helps to reuse the same fixture for more than one tests.&amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html]&amp;lt;/sup&amp;gt; &lt;br /&gt;
&lt;br /&gt;
For example, consider a method &amp;lt;i&amp;gt;prime_check(num)&amp;lt;/i&amp;gt; which takes an integer number as input and outputs whether it is prime number or not. In order to unit test this method we can create the following fixture containing a 2-dimensional array with a number and the expected output of whether it is prime or not.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @NUMBERS = [[3,true], [4,false], [7,true], [10,false]]    &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Assertions==&lt;br /&gt;
The core part of test::unit framework is the ability to assert a statement of expected outcome. If an assert statement is correct then the test will proceed, otherwise the test will fail. This feature helps us to verify the method under test with different types of inputs and track the results. Test::unit provides a bunch of assert methods for this purpose: &lt;br /&gt;
&lt;br /&gt;
{| border=1 cellspacing=0 cellpadding=5&lt;br /&gt;
| assert( boolean, [message] ) &lt;br /&gt;
| True if ''boolean''&lt;br /&gt;
|- &lt;br /&gt;
| assert_equal( expected, actual, [message] )&amp;lt;br&amp;gt;assert_not_equal( expected, actual, [message] )&lt;br /&gt;
| True if ''expected == actual''&lt;br /&gt;
|-&lt;br /&gt;
| assert_raise( Exception,... ) {block}&amp;lt;br&amp;gt;assert_nothing_raised( Exception,...) {block} &lt;br /&gt;
| True if the block raises (or doesn't) one of the listed exceptions.&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For the full list of assertion methods provided by test::unit refer to test::unit assertions. &amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
The test case class &amp;lt;i&amp;gt;BinarySearchTest&amp;lt;/i&amp;gt; subclasses the &amp;lt;i&amp;gt;Test::Unit::TestCase&amp;lt;/i&amp;gt; class and overrides &amp;lt;i&amp;gt;setup&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;teardown&amp;lt;/i&amp;gt; methods. The test methods should start with 'test_' prefix. This helps in isolating the test methods from the helper methods if any. The Test::Unit::TestCase class takes care of making the test methods into tests, wrapping them into a suite and running the individual tests. The test results are collected into &amp;lt;i&amp;gt;Test::Unit::TestResult&amp;lt;/i&amp;gt; object.&lt;br /&gt;
&lt;br /&gt;
    require 'test/unit'&lt;br /&gt;
    require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
    class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
 &lt;br /&gt;
      def setup&lt;br /&gt;
        @input_array = [1,2,3,4,5]&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      def test_success_left_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,1),true)&lt;br /&gt;
        assert_equal(binary_search(@input_array,2),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_right_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,5),true)&lt;br /&gt;
        assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_middle&lt;br /&gt;
        assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_failure&lt;br /&gt;
        assert_equal(binary_search(@input_array,6),false)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def teardown&lt;br /&gt;
        #nothing to do here&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we have four test methods testing different logical paths of the binary search algorithm. Each test method can have one or more assert statements to test whether conditions are correct in each situation. To run the tests we simply have to run the file binary_search_test.rb and the output is as follows:&lt;br /&gt;
 &lt;br /&gt;
  Loaded suite binarysearch&lt;br /&gt;
  Started&lt;br /&gt;
  F...&lt;br /&gt;
  Finished in 0.372 seconds.&lt;br /&gt;
  &lt;br /&gt;
    1) Failure:&lt;br /&gt;
  test_failure(BinarySearchTest) [binarysearch.rb:25]:&lt;br /&gt;
  &amp;lt;true&amp;gt; expected but was&lt;br /&gt;
  &amp;lt;false&amp;gt;.&lt;br /&gt;
  &lt;br /&gt;
  4 tests, 6 assertions, 1 failures, 0 errors&lt;br /&gt;
&lt;br /&gt;
The results show that the last test case &amp;lt;i&amp;gt;test_failure&amp;lt;/i&amp;gt;, testing the negative scenario is failing. The reason is because the assert statement is expecting &amp;lt;i&amp;gt;false&amp;lt;/i&amp;gt; when number 6, which not present in the array is passed. But the binary_search method is returning true.&lt;br /&gt;
&lt;br /&gt;
==Test Suite==&lt;br /&gt;
Sometimes it is useful to combine a bunch of related test cases and run them as batch. Test::Unit provides a class called TestSuite for this purpose. The below example demonstrates how to bundle binary and sequential test case classes into a single search test suite.&lt;br /&gt;
&lt;br /&gt;
   require 'test/unit/testsuite'&lt;br /&gt;
   require 'binary_search_test'&lt;br /&gt;
   require 'sequential_search_test'&lt;br /&gt;
  &lt;br /&gt;
   class Search_Tests&lt;br /&gt;
     def self.suite&lt;br /&gt;
       suite = Test::Unit::TestSuite.new&lt;br /&gt;
       suite &amp;lt;&amp;lt; BinarySearchTest.suite&lt;br /&gt;
       suite &amp;lt;&amp;lt; SequentialSearchTest.suite&lt;br /&gt;
       return suite&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   Test::Unit::UI::Console::TestRunner.run(Search_Tests)&lt;br /&gt;
&lt;br /&gt;
=Shoulda=&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
One of the downsides of Test::Unit is we end up writing lots of code in order to test the actual code which is sometimes not easy to understand. Shoulda is a library that allows us to write better and more understandable tests for ruby application. Shoulda is not a testing framework by itself. It extends the Test::Unit framework with the idea of &amp;lt;i&amp;gt;context&amp;lt;/i&amp;gt;. We can mix Test::Unit test cases with Shoulda test cases. Shoulda allows us to provide context to the tests so that we can group the tests according to a specific feature or scenario. &amp;lt;sup&amp;gt;[http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
   require 'shoulda'&lt;br /&gt;
   require 'test/unit'&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
   class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
      &lt;br /&gt;
     context &amp;quot;Input array of size 5&amp;quot; do&lt;br /&gt;
      &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1,2,3,4,5]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the left half of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the right half of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,5),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the middle of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,6),false)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
    &lt;br /&gt;
     context &amp;quot;Input array of size 1&amp;quot; do&lt;br /&gt;
     &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)         &lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)         &lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Notice that we are still sub-classing the Test::Unit::TestCase class. In this example we have two contexts one for input array of size 5 and the other for input array of size 1. Each context has its own environment of setup/teardown methods. We can also create nested contexts - the outer setup gets run before the execution of each of the inner contexts. And the setup in the inner contexts gets run when running that context. Each &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; construct is converted into individual test methods and are run. If a test case fails we will get a better description of what that test case is doing from the &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; description.&lt;br /&gt;
&lt;br /&gt;
=RSpec=&lt;br /&gt;
&lt;br /&gt;
Now let us consider about &amp;lt;i&amp;gt;RSpec&amp;lt;/i&amp;gt; Testing Framework in detail.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Behaviour Driven Development&amp;lt;/b&amp;gt; (BDD) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Behavior_Driven_Development]&amp;lt;/sup&amp;gt; is an Agile development process &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Agile_software_development]&amp;lt;/sup&amp;gt; that comprises aspects of Acceptance Test Driven Planning &amp;lt;sup&amp;gt;[http://www.springerlink.com/content/978-3-540-22839-4/] [http://en.wikipedia.org/wiki/Acceptance_testing]&amp;lt;/sup&amp;gt;, Domain Driven Design &amp;lt;sup&amp;gt;[http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Domain-driven_design]&amp;lt;/sup&amp;gt; and Test Driven Development (TDD). &amp;lt;sup&amp;gt;[http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Test-driven_development]&amp;lt;/sup&amp;gt; &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;RSpec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; is a Behavioural Driven Development (BDD) tool aimed at Test Driven Development, originally created by Dave Astels and Steven Baker. However David Chelimsky &amp;lt;sup&amp;gt;[http://blog.davidchelimsky.net/]&amp;lt;/sup&amp;gt; is really the gatekeeper of the RSpec project. &amp;lt;sup&amp;gt;[http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Traditionally we use Unit Test frameworks like JUnit, NUnit or RUnit for writing Test cases. We spend a lot of time writing tests that test every unit of code in our software system. Instead we can shift our focus from Unit testing to Behaviour testing or Behaviour Driven Development (BDD) using RSpec. By focusing on the behaviour of the system it helps clarify in our minds what the system should actually be doing. It also helps us to perform more ‘useful’ tests. Useful tests, cover what the system should be doing and build in enough redundancy so that it should be easy to refactor our code without having to re-write every test.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
RSpec is really two projects merged into one. The RSpec project pages describes these merged projects as:&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;application level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Story Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;object level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Spec Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dan North created &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;rbehave&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; &amp;lt;sup&amp;gt;[http://blog.dannorth.net/2007/06/17/introducing-rbehave/]&amp;lt;/sup&amp;gt; which is the Story Framework and David Chelimsky created the &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;Spec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; Framework. By encompassing two frameworks RSpec equips a programmer with a thorough set of testing tools, allowing you to think about your software problem from a number of perspectives.&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
&lt;br /&gt;
The prerequisites are&lt;br /&gt;
&lt;br /&gt;
# Ruby 1.8.4 or later&lt;br /&gt;
# RSpec Gem (latest)&lt;br /&gt;
&lt;br /&gt;
To install Ruby, please visit official Ruby Website &amp;lt;sup&amp;gt;[http://www.ruby-lang.org/]&amp;lt;/sup&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
To install RSpec, open a command shell, go to /bin folder in Ruby directory and type&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;gt; gem install rspec&lt;br /&gt;
&lt;br /&gt;
==Terms &amp;amp; Definitions==&lt;br /&gt;
&lt;br /&gt;
Here are some terms which are used frequently while working with RSpec. &amp;lt;sup&amp;gt;[http://www.pragprog.com/titles/achbd/the-rspec-book/ ]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;b&amp;gt;subject code&amp;lt;/b&amp;gt; - The code whose behavior is specified using RSpec&lt;br /&gt;
# &amp;lt;b&amp;gt;expectation&amp;lt;/b&amp;gt; - The expected behavior of subject code is expressed using expectation (Similar to 'Assertions' statements used in Test::Unit or other tools in other languages)&lt;br /&gt;
# &amp;lt;b&amp;gt;code example&amp;lt;/b&amp;gt; - An executable example containing the subject code and the expectations (Similar to 'Test Method' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;example group&amp;lt;/b&amp;gt; - A group of code examples (Similar to 'Test Case' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;spec file&amp;lt;/b&amp;gt; - A file which contains one or more example groups&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Let us go through an example to be clear on the usage of RSpec.&lt;br /&gt;
&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
   &lt;br /&gt;
   describe BinarySearchTest do&lt;br /&gt;
     before(:all) do&lt;br /&gt;
       @input_array = [1, 2, 3, 4, 5] # The Input Array&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     after(:all) do&lt;br /&gt;
       # do nothing here&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the left-half of the array&amp;quot; do  # Test case for element to be present in left-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(1)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the right-half of the array&amp;quot; do  # Test case for element to be present in right-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(5)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the middle of the array&amp;quot; do  # Test case for element to be present in the middle of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(3)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should not be in the array&amp;quot; do  # Test case for element NOT to be present in given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should_not be_binary_search(7)&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here it is assumed that the method binary_search will return true/false based on whether the provided value exists in the array or not.&lt;br /&gt;
&lt;br /&gt;
====describe() method====&lt;br /&gt;
&lt;br /&gt;
The describe() method can take an arbitrary number of arguments and a block and returns a sub-class of Spec::Example::ExampleGroup. We generally use only one or two arguments which is used to describe the behavior. The first argument can be a reference to a Class or module or a string. The second argument is optional and should be a string when used.&lt;br /&gt;
&lt;br /&gt;
====it() method====&lt;br /&gt;
&lt;br /&gt;
Similar to the describe() method, the it() method takes a single String, an optional Hash and an optional block. The String expression within the it() should be such that it informs the behavior of the code within the block.&lt;br /&gt;
&lt;br /&gt;
==Expectations in RSpec==&lt;br /&gt;
&lt;br /&gt;
There are two methods available for checking expectations: should() and should_not(). Both the methods accept either an expression matcher or a Ruby expression using a specific subset of Ruby operators. An expression matcher is an objects that matches an expression.&lt;br /&gt;
&lt;br /&gt;
===Built-in Matchers===&lt;br /&gt;
&lt;br /&gt;
There are several matchers that can be used with should and should_not, which are divided into well-separated categories.&lt;br /&gt;
====Equality====&lt;br /&gt;
 subject.should == ece517&lt;br /&gt;
 subject.should === ece517&lt;br /&gt;
 subject.should eql(subject)&lt;br /&gt;
 subject.should equal(subject)&lt;br /&gt;
&lt;br /&gt;
The == method is used to express equivalence and equal is used when you want the receiver and the argument to be the same object. Instead of using !=, you should use the should_not method!&lt;br /&gt;
&lt;br /&gt;
====Floating Point Calculations====&lt;br /&gt;
 piValue.should be_close(3.14, 0.001593)&lt;br /&gt;
&lt;br /&gt;
Sometimes the values generated might be correct upto some fixed decimal positions, after that they may have slight variations. To avoid the test beings failed, we provide the (value, delta) to be_close method which passes the test if the obtained value lies within the range (value+delta).&lt;br /&gt;
&lt;br /&gt;
====Regular Expressions====&lt;br /&gt;
 resultExpression.should match(/this regular expression/)&lt;br /&gt;
 resultExpression.should =~ /this regular expression/&lt;br /&gt;
&lt;br /&gt;
This can be very useful when dealing with multiple-line expectations, instead of using the open file technique to compare contents.&lt;br /&gt;
&lt;br /&gt;
====Changes====&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.to(1)&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.from(0).to(1)&lt;br /&gt;
 &lt;br /&gt;
This is really useful when working with database changes or changes to objects. The matcher is change(), which takes a block and accepts the from(), to() or by() modifiers. &amp;lt;sup&amp;gt;[18]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Errors====&lt;br /&gt;
 field = CricketGround.new(:players =&amp;gt; 11)&lt;br /&gt;
 lambda {&lt;br /&gt;
  field.remove(:players, 15)&lt;br /&gt;
 }.should raise_error(NotEnoughPlayers,“attempted to remove more players than there is on cricket stadium”)&lt;br /&gt;
&lt;br /&gt;
Useful when needed to check for Exceptions. The matcher is raise_error and takes an ExceptionObject and/or a String/Regexp.&lt;br /&gt;
 &lt;br /&gt;
====Throw====&lt;br /&gt;
 speech = Speech.new(:seats =&amp;gt; 100)&lt;br /&gt;
 100.times { speech.register Person.new }&lt;br /&gt;
 lambda {&lt;br /&gt;
  speech.register Person.new&lt;br /&gt;
 }.should throw_symbol(:speech_full, 100)&lt;br /&gt;
&lt;br /&gt;
When dealing with “errors that are not really exceptions”, you use catch and throw. Rspec can check if a throw has been called by using the throw_symbol matcher. It accepts 0,1 or 2 arguments. The first argument needs to be a Symbol and the second can be any Object that is thrown along.&lt;br /&gt;
&lt;br /&gt;
===Predicate Matchers===&lt;br /&gt;
A Ruby predicate method is a method that ends with a “?” and returns a boolean value, like string.empty? or regexp.match? methods. Instead of writing:&lt;br /&gt;
 a_string.empty?.should == true&lt;br /&gt;
We can write using RSpec:&lt;br /&gt;
 a_string.should be_empty&lt;br /&gt;
&lt;br /&gt;
When using a be_something matcher, RSpec removes the “be_”, appends a “?” and calls the resulting method in the receiver. A very common construct of this method is be_true, which checks if the receiver is true (any object except false or nil) or false (false or nil).&lt;br /&gt;
&lt;br /&gt;
===Check Ownership===&lt;br /&gt;
Sometimes you will want to check something the object owns and not the object itself.&lt;br /&gt;
&lt;br /&gt;
====The have_something() method====&lt;br /&gt;
 security_access.has_key?(:id).should == true&lt;br /&gt;
is the same as&lt;br /&gt;
 security_access.should have_key(:id)&lt;br /&gt;
&lt;br /&gt;
RSpec uses method_missing to convert anything that begins with have_something to has_something? and performs the checking.&lt;br /&gt;
&lt;br /&gt;
====The have() method====&lt;br /&gt;
 field.players.select {|p| p.team == home_team }.length.should == 9&lt;br /&gt;
is the same as&lt;br /&gt;
 home_team.should have(9).players_on(field)&lt;br /&gt;
 &lt;br /&gt;
As have() does not respond to players_on(), it delegates to the receiver (home_team). It encourages the home_team object to have useful methods like players_on.&amp;lt;br&amp;gt;&lt;br /&gt;
You can get a NoMethodError if the players_on method doesn´t exist, you can get another NoMethodError if the result of the players_on method doesn´t respond to size() or length() and if the size of the collection doesn´t match the expected size, you will get a failed expectation. &amp;lt;sup&amp;gt;[http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Checking Collections Themselves===&lt;br /&gt;
Sometimes we create expectations about a collection itself and not about an owned collection. RSpec lets us use the have() method to express this as well, as in:&lt;br /&gt;
 basket_collection.should have(10).items&lt;br /&gt;
items is just providing some meaning to the expectation.&lt;br /&gt;
&lt;br /&gt;
====Strings====&lt;br /&gt;
Strings are not collections by definition but they respond to a lot of methods that collections do, like length() and size(). This allow us to use have() to expect a string of a specific length.&lt;br /&gt;
 “apple”.should have(5).characters&lt;br /&gt;
characters is just providing meaning to the expectation as well.&lt;br /&gt;
&lt;br /&gt;
====Have() modifiers for precision====&lt;br /&gt;
The have() method has some relatives that allow us to check for upper and lower conditions.&lt;br /&gt;
&lt;br /&gt;
 work.should have_exactly(8).hours&lt;br /&gt;
 classroom.should have_at_most(100).people&lt;br /&gt;
 bag.should have_at_least(5).items&lt;br /&gt;
&lt;br /&gt;
===Operator Expressions===&lt;br /&gt;
There may be sometimes when you want to expect a value to be not an exact amount but something like greater than or less than. RSpec allows you to do this by using the regular operators from Ruby!&lt;br /&gt;
&lt;br /&gt;
 number.should == 3&lt;br /&gt;
 number.should be &amp;gt;= 2&lt;br /&gt;
 number.should be &amp;lt;= 4&lt;br /&gt;
 number should be &amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Unit_testing Unit Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Programming &amp;amp; Unit Testing]. en.wikibooks.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Ruby Test::Unit]. ruby-doc.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Ruby Assertions]. ruby-doc.org. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html Shoulda Explained]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavior Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Agile_software_development Agile Software Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.springerlink.com/content/978-3-540-22839-4/ Proceedings of Extreme Programming and Agile Methods Conference] Carmen Zannier, Hakan Erdogmus and Lowell Lindstrom. &amp;lt;i&amp;gt;Extreme Programming and Agile Methods - XP/Agile Universe 2004&amp;lt;/i&amp;gt;. 4th Conference on Extreme Programming and Agile Methods, Calgary, Canada, August 15-18, 2004. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false Domain-driven design: tackling complexity in the heart of software]. By Eric Evans. books.google.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Domain-driven_design Domain Driven Design]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false Test-driven development: by example]. By Kent Beck. books. google.com. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Test-driven_development Test Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.davidchelimsky.net/ David Chelimsky Blog]. davidchelimsky.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/ Understanding RSpec Stories - A Tutorial]. emson.co.uk. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.dannorth.net/2007/06/17/introducing-rbehave/ rbehave]. dannorth.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-lang.org Ruby Website]. ruby-lang.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.pragprog.com/titles/achbd/the-rspec-book RSpec Book]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/ RSpec - Expectations]. wordpress.com. Retrieved Sep 17, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35365</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1f vn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35365"/>
		<updated>2010-09-18T01:15:23Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the different Unit-Testing Frameworks available for Ruby.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Unit testing is a method by which we can isolate and test a unit functionality of the program, typically individual methods during and long after the code is written. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing]&amp;lt;/sup&amp;gt; It helps to identify errors in the program even without running the entire program. It also helps to do regressing testing to identify buggy code additions in the future. Unit testing frameworks provides us with constructs which simplifies the process of unit testing. Using a standard unit test framework helps other developers to add test cases easily. &amp;lt;sup&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing]&amp;lt;/sup&amp;gt; This chapter walks through three different unit testing frameworks available for Ruby and explains how to use them with examples. The three commonly used unit testing frameworks for ruby are &lt;br /&gt;
&lt;br /&gt;
# Test::Unit&lt;br /&gt;
# Shoulda&lt;br /&gt;
# RSpec&lt;br /&gt;
&lt;br /&gt;
=Test::Unit=&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
Ruby comes with an in-built, ready to use unit testing framework called Test::Unit. It is a XUnit type framework and typically have a setup method for initialization, a teardown method for cleanup and the actual test methods itself. The tests themselves are bundled separately in a test class from the code it is testing.&lt;br /&gt;
&lt;br /&gt;
==Test Fixture==&lt;br /&gt;
Test fixture represents the initial environment setup(eg. initialization data) and/or the expected outcome of the tests for that environment. This is typically done in the setup() and teardown() methods and it helps to separate test initialization and cleanup from the actual tests. It also helps to reuse the same fixture for more than one tests.&amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html]&amp;lt;/sup&amp;gt; &lt;br /&gt;
&lt;br /&gt;
For example, consider a method &amp;lt;i&amp;gt;prime_check(num)&amp;lt;/i&amp;gt; which takes an integer number as input and outputs whether it is prime number or not. In order to unit test this method we can create the following fixture containing a 2-dimensional array with a number and the expected output of whether it is prime or not.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @NUMBERS = [[3,true], [4,false], [7,true], [10,false]]    &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Assertions==&lt;br /&gt;
The core part of test::unit framework is the ability to assert a statement of expected outcome. If an assert statement is correct then the test will proceed, otherwise the test will fail. This feature helps us to verify the method under test with different types of inputs and track the results. Test::unit provides a bunch of assert methods for this purpose: &lt;br /&gt;
&lt;br /&gt;
{| border=1 cellspacing=0 cellpadding=5&lt;br /&gt;
| assert( boolean, [message] ) &lt;br /&gt;
| True if ''boolean''&lt;br /&gt;
|- &lt;br /&gt;
| assert_equal( expected, actual, [message] )&amp;lt;br&amp;gt;assert_not_equal( expected, actual, [message] )&lt;br /&gt;
| True if ''expected == actual''&lt;br /&gt;
|-&lt;br /&gt;
| assert_raise( Exception,... ) {block}&amp;lt;br&amp;gt;assert_nothing_raised( Exception,...) {block} &lt;br /&gt;
| True if the block raises (or doesn't) one of the listed exceptions.&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For the full list of assertion methods provided by test::unit refer to test::unit assertions. &amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
The test case class &amp;lt;i&amp;gt;BinarySearchTest&amp;lt;/i&amp;gt; subclasses the &amp;lt;i&amp;gt;Test::Unit::TestCase&amp;lt;/i&amp;gt; class and overrides &amp;lt;i&amp;gt;setup&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;teardown&amp;lt;/i&amp;gt; methods. The test methods should start with 'test_' prefix. This helps in isolating the test methods from the helper methods if any. The Test::Unit::TestCase class takes care of making the test methods into tests, wrapping them into a suite and running the individual tests. The test results are collected into &amp;lt;i&amp;gt;Test::Unit::TestResult&amp;lt;/i&amp;gt; object.&lt;br /&gt;
&lt;br /&gt;
    require 'test/unit'&lt;br /&gt;
    require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
    class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
 &lt;br /&gt;
      def setup&lt;br /&gt;
        @input_array = [1,2,3,4,5]&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      def test_success_left_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,1),true)&lt;br /&gt;
        assert_equal(binary_search(@input_array,2),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_right_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,5),true)&lt;br /&gt;
        assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_middle&lt;br /&gt;
        assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_failure&lt;br /&gt;
        assert_equal(binary_search(@input_array,6),false)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def teardown&lt;br /&gt;
        #nothing to do here&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we have four test methods testing different logical paths of the binary search algorithm. Each test method can have one or more assert statements to test whether conditions are correct in each situation. To run the tests we simply have to run the file binary_search_test.rb and the output is as follows:&lt;br /&gt;
 &lt;br /&gt;
  Loaded suite binarysearch&lt;br /&gt;
  Started&lt;br /&gt;
  F...&lt;br /&gt;
  Finished in 0.372 seconds.&lt;br /&gt;
  &lt;br /&gt;
    1) Failure:&lt;br /&gt;
  test_failure(BinarySearchTest) [binarysearch.rb:25]:&lt;br /&gt;
  &amp;lt;true&amp;gt; expected but was&lt;br /&gt;
  &amp;lt;false&amp;gt;.&lt;br /&gt;
  &lt;br /&gt;
  4 tests, 6 assertions, 1 failures, 0 errors&lt;br /&gt;
&lt;br /&gt;
The results show that the last test case &amp;lt;i&amp;gt;test_failure&amp;lt;/i&amp;gt;, testing the negative scenario is failing. The reason is because the assert statement is expecting &amp;lt;i&amp;gt;false&amp;lt;/i&amp;gt; when number 6, which not present in the array is passed. But the binary_search method is returning true.&lt;br /&gt;
&lt;br /&gt;
==Test Suite==&lt;br /&gt;
Sometimes it is useful to combine a bunch of related test cases and run them as batch. Test::Unit provides a class called TestSuite for this purpose. The below example demonstrates how to bundle binary and sequential test case classes into a single search test suite.&lt;br /&gt;
&lt;br /&gt;
   require 'test/unit/testsuite'&lt;br /&gt;
   require 'binary_search_test'&lt;br /&gt;
   require 'sequential_search_test'&lt;br /&gt;
  &lt;br /&gt;
   class Search_Tests&lt;br /&gt;
     def self.suite&lt;br /&gt;
       suite = Test::Unit::TestSuite.new&lt;br /&gt;
       suite &amp;lt;&amp;lt; BinarySearchTest.suite&lt;br /&gt;
       suite &amp;lt;&amp;lt; SequentialSearchTest.suite&lt;br /&gt;
       return suite&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   Test::Unit::UI::Console::TestRunner.run(Search_Tests)&lt;br /&gt;
&lt;br /&gt;
=Shoulda=&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
One of the downsides of Test::Unit is we end up writing lots of code in order to test the actual code which is sometimes not easy to understand. Shoulda is a library that allows us to write better and more understandable tests for ruby application. Shoulda is not a testing framework by itself. It extends the Test::Unit framework with the idea of &amp;lt;i&amp;gt;context&amp;lt;/i&amp;gt;. We can mix Test::Unit test cases with Shoulda test cases. Shoulda allows us to provide context to the tests so that we can group the tests according to a specific feature or scenario. &amp;lt;sup&amp;gt;[http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
   require 'shoulda'&lt;br /&gt;
   require 'test/unit'&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
   class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
      &lt;br /&gt;
     context &amp;quot;Input array of size 5&amp;quot; do&lt;br /&gt;
      &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1,2,3,4,5]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the left half of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the right half of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,5),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the middle of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,6),false)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
    &lt;br /&gt;
     context &amp;quot;Input array of size 1&amp;quot; do&lt;br /&gt;
     &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)         &lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)         &lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Notice that we are still sub-classing the Test::Unit::TestCase class. In this example we have two contexts one for input array of size 5 and the other for input array of size 1. Each context has its own environment of setup/teardown methods. We can also create nested contexts - the outer setup gets run before the execution of each of the inner contexts. And the setup in the inner contexts gets run when running that context. Each &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; construct is converted into individual test methods and are run. If a test case fails we will get a better description of what that test case is doing from the &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; description.&lt;br /&gt;
&lt;br /&gt;
=RSpec=&lt;br /&gt;
&lt;br /&gt;
Now let us consider about &amp;lt;i&amp;gt;RSpec&amp;lt;/i&amp;gt; Testing Framework in detail.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Behaviour Driven Development&amp;lt;/b&amp;gt; (BDD) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Behavior_Driven_Development]&amp;lt;/sup&amp;gt; is an Agile development process &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Agile_software_development]&amp;lt;/sup&amp;gt; that comprises aspects of Acceptance Test Driven Planning &amp;lt;sup&amp;gt;[http://www.springerlink.com/content/978-3-540-22839-4/] [http://en.wikipedia.org/wiki/Acceptance_testing]&amp;lt;/sup&amp;gt;, Domain Driven Design &amp;lt;sup&amp;gt;[http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Domain-driven_design]&amp;lt;/sup&amp;gt; and Test Driven Development (TDD). &amp;lt;sup&amp;gt;[http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Test-driven_development]&amp;lt;/sup&amp;gt; &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;RSpec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; is a Behavioural Driven Development (BDD) tool aimed at Test Driven Development, originally created by Dave Astels and Steven Baker. However David Chelimsky &amp;lt;sup&amp;gt;[http://blog.davidchelimsky.net/]&amp;lt;/sup&amp;gt; is really the gatekeeper of the RSpec project. &amp;lt;sup&amp;gt;[http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Traditionally we use Unit Test frameworks like JUnit, NUnit or RUnit for writing Test cases. We spend a lot of time writing tests that test every unit of code in our software system. Instead we can shift our focus from Unit testing to Behaviour testing or Behaviour Driven Development (BDD) using RSpec. By focusing on the behaviour of the system it helps clarify in our minds what the system should actually be doing. It also helps us to perform more ‘useful’ tests. Useful tests, cover what the system should be doing and build in enough redundancy so that it should be easy to refactor our code without having to re-write every test.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
RSpec is really two projects merged into one. The RSpec project pages describes these merged projects as:&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;application level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Story Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;object level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Spec Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dan North created &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;rbehave&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; &amp;lt;sup&amp;gt;[http://blog.dannorth.net/2007/06/17/introducing-rbehave/]&amp;lt;/sup&amp;gt; which is the Story Framework and David Chelimsky created the &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;Spec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; Framework. By encompassing two frameworks RSpec equips a programmer with a thorough set of testing tools, allowing you to think about your software problem from a number of perspectives.&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
&lt;br /&gt;
The prerequisites are&lt;br /&gt;
&lt;br /&gt;
# Ruby 1.8.4 or later&lt;br /&gt;
# RSpec Gem (latest)&lt;br /&gt;
&lt;br /&gt;
To install Ruby, please visit official Ruby Website &amp;lt;sup&amp;gt;[http://www.ruby-lang.org/]&amp;lt;/sup&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
To install RSpec, open a command shell, go to /bin folder in Ruby directory and type&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;gt; gem install rspec&lt;br /&gt;
&lt;br /&gt;
==Terms &amp;amp; Definitions==&lt;br /&gt;
&lt;br /&gt;
Here are some terms which are used frequently while working with RSpec. &amp;lt;sup&amp;gt;[http://www.pragprog.com/titles/achbd/the-rspec-book/ ]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;b&amp;gt;subject code&amp;lt;/b&amp;gt; - The code whose behavior is specified using RSpec&lt;br /&gt;
# &amp;lt;b&amp;gt;expectation&amp;lt;/b&amp;gt; - The expected behavior of subject code is expressed using expectation (Similar to 'Assertions' statements used in Test::Unit or other tools in other languages)&lt;br /&gt;
# &amp;lt;b&amp;gt;code example&amp;lt;/b&amp;gt; - An executable example containing the subject code and the expectations (Similar to 'Test Method' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;example group&amp;lt;/b&amp;gt; - A group of code examples (Similar to 'Test Case' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;spec file&amp;lt;/b&amp;gt; - A file which contains one or more example groups&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Let us go through an example to be clear on the usage of RSpec.&lt;br /&gt;
&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
   &lt;br /&gt;
   describe BinarySearchTest do&lt;br /&gt;
     before(:all) do&lt;br /&gt;
       @input_array = [1, 2, 3, 4, 5] # The Input Array&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     after(:all) do&lt;br /&gt;
       # do nothing here&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the left-half of the array&amp;quot; do  # Test case for element to be present in left-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(1)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the right-half of the array&amp;quot; do  # Test case for element to be present in right-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(5)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the middle of the array&amp;quot; do  # Test case for element to be present in the middle of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(3)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should not be in the array&amp;quot; do  # Test case for element NOT to be present in given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should_not be_binary_search(7)&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here it is assumed that the method binary_search will return true/false based on whether the provided value exists in the array or not.&lt;br /&gt;
&lt;br /&gt;
====describe() method====&lt;br /&gt;
&lt;br /&gt;
The describe() method can take an arbitrary number of arguments and a block and returns a sub-class of Spec::Example::ExampleGroup. We generally use only one or two arguments which is used to describe the behavior. The first argument can be a reference to a Class or module or a string. The second argument is optional and should be a string when used.&lt;br /&gt;
&lt;br /&gt;
====it() method====&lt;br /&gt;
&lt;br /&gt;
Similar to the describe() method, the it() method takes a single String, an optional Hash and an optional block. The String expression within the it() should be such that it informs the behavior of the code within the block.&lt;br /&gt;
&lt;br /&gt;
==Expectations in RSpec==&lt;br /&gt;
&lt;br /&gt;
There are two methods available for checking expectations: should() and should_not(). Both the methods accept either an expression matcher or a Ruby expression using a specific subset of Ruby operators. An expression matcher is an objects that matches an expression.&lt;br /&gt;
&lt;br /&gt;
===Built-in Matchers===&lt;br /&gt;
&lt;br /&gt;
There are several matchers that can be used with should and should_not, which are divided into well-separated categories.&lt;br /&gt;
====Equality====&lt;br /&gt;
 subject.should == ece517&lt;br /&gt;
 subject.should === ece517&lt;br /&gt;
 subject.should eql(subject)&lt;br /&gt;
 subject.should equal(subject)&lt;br /&gt;
&lt;br /&gt;
The == method is used to express equivalence and equal is used when you want the receiver and the argument to be the same object. Instead of using !=, you should use the should_not method!&lt;br /&gt;
&lt;br /&gt;
====Floating Point Calculations====&lt;br /&gt;
 piValue.should be_close(3.14, 0.001593)&lt;br /&gt;
&lt;br /&gt;
Sometimes the values generated might be correct upto some fixed decimal positions, after that they may have slight variations. To avoid the test beings failed, we provide the (value, delta) to be_close method which passes the test if the obtained value lies within the range (value+delta).&lt;br /&gt;
&lt;br /&gt;
====Regular Expressions====&lt;br /&gt;
 resultExpression.should match(/this regular expression/)&lt;br /&gt;
 resultExpression.should =~ /this regular expression/&lt;br /&gt;
&lt;br /&gt;
This can be very useful when dealing with multiple-line expectations, instead of using the open file technique to compare contents.&lt;br /&gt;
&lt;br /&gt;
====Changes====&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.to(1)&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.from(0).to(1)&lt;br /&gt;
 &lt;br /&gt;
This is really useful when working with database changes or changes to objects. The matcher is change(), which takes a block and accepts the from(), to() or by() modifiers. &amp;lt;sup&amp;gt;[18]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Errors====&lt;br /&gt;
 field = CricketGround.new(:players =&amp;gt; 11)&lt;br /&gt;
 lambda {&lt;br /&gt;
  field.remove(:players, 15)&lt;br /&gt;
 }.should raise_error(NotEnoughPlayers,“attempted to remove more players than there is on cricket stadium”)&lt;br /&gt;
&lt;br /&gt;
Useful when needed to check for Exceptions. The matcher is raise_error and takes an ExceptionObject and/or a String/Regexp.&lt;br /&gt;
 &lt;br /&gt;
====Throw====&lt;br /&gt;
 speech = Speech.new(:seats =&amp;gt; 100)&lt;br /&gt;
 100.times { speech.register Person.new }&lt;br /&gt;
 lambda {&lt;br /&gt;
  speech.register Person.new&lt;br /&gt;
 }.should throw_symbol(:speech_full, 100)&lt;br /&gt;
&lt;br /&gt;
When dealing with “errors that are not really exceptions”, you use catch and throw. Rspec can check if a throw has been called by using the throw_symbol matcher. It accepts 0,1 or 2 arguments. The first argument needs to be a Symbol and the second can be any Object that is thrown along.&lt;br /&gt;
&lt;br /&gt;
===Predicate Matchers===&lt;br /&gt;
A Ruby predicate method is a method that ends with a “?” and returns a boolean value, like string.empty? or regexp.match? methods. Instead of writing:&lt;br /&gt;
 a_string.empty?.should == true&lt;br /&gt;
We can write using RSpec:&lt;br /&gt;
 a_string.should be_empty&lt;br /&gt;
&lt;br /&gt;
When using a be_something matcher, RSpec removes the “be_”, appends a “?” and calls the resulting method in the receiver. A very common construct of this method is be_true, which checks if the receiver is true (any object except false or nil) or false (false or nil).&lt;br /&gt;
&lt;br /&gt;
===Check Ownership===&lt;br /&gt;
Sometimes you will want to check something the object owns and not the object itself.&lt;br /&gt;
&lt;br /&gt;
====The have_something() method====&lt;br /&gt;
 security_access.has_key?(:id).should == true&lt;br /&gt;
is the same as&lt;br /&gt;
 security_access.should have_key(:id)&lt;br /&gt;
&lt;br /&gt;
RSpec uses method_missing to convert anything that begins with have_something to has_something? and performs the checking.&lt;br /&gt;
&lt;br /&gt;
====The have() method====&lt;br /&gt;
 field.players.select {|p| p.team == home_team }.length.should == 9&lt;br /&gt;
is the same as&lt;br /&gt;
 home_team.should have(9).players_on(field)&lt;br /&gt;
 &lt;br /&gt;
As have() does not respond to players_on(), it delegates to the receiver (home_team). It encourages the home_team object to have useful methods like players_on.&amp;lt;br&amp;gt;&lt;br /&gt;
You can get a NoMethodError if the players_on method doesn´t exist, you can get another NoMethodError if the result of the players_on method doesn´t respond to size() or length() and if the size of the collection doesn´t match the expected size, you will get a failed expectation. &amp;lt;sup&amp;gt;[http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Checking Collections Themselves===&lt;br /&gt;
Sometimes we create expectations about a collection itself and not about an owned collection. RSpec lets us use the have() method to express this as well, as in:&lt;br /&gt;
 basket_collection.should have(10).items&lt;br /&gt;
items is just providing some meaning to the expectation.&lt;br /&gt;
&lt;br /&gt;
====Strings====&lt;br /&gt;
Strings are not collections by definition but they respond to a lot of methods that collections do, like length() and size(). This allow us to use have() to expect a string of a specific length.&lt;br /&gt;
 “apple”.should have(5).characters&lt;br /&gt;
characters is just providing meaning to the expectation as well.&lt;br /&gt;
&lt;br /&gt;
====Have() modifiers for precision====&lt;br /&gt;
The have() method has some relatives that allow us to check for upper and lower conditions.&lt;br /&gt;
&lt;br /&gt;
 work.should have_exactly(8).hours&lt;br /&gt;
 classroom.should have_at_most(100).people&lt;br /&gt;
 bag.should have_at_least(5).items&lt;br /&gt;
&lt;br /&gt;
===Operator Expressions===&lt;br /&gt;
There may be sometimes when you want to expect a value to be not an exact amount but something like greater than or less than. RSpec allows you to do this by using the regular operators from Ruby!&lt;br /&gt;
&lt;br /&gt;
 number.should == 3&lt;br /&gt;
 number.should be &amp;gt;= 2&lt;br /&gt;
 number.should be &amp;lt;= 4&lt;br /&gt;
 number should be &amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Unit_testing Unit Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Programming &amp;amp; Unit Testing]. en.wikibooks.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Ruby Test::Unit]. ruby-doc.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Ruby Assertions]. ruby-doc.org. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html Shoulda Explained]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavior Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Agile_software_development Agile Software Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.springerlink.com/content/978-3-540-22839-4/ Proceedings of Extreme Programming and Agile Methods] Carmen Zannier, Hakan Erdogmus and Lowell Lindstrom. &amp;lt;i&amp;gt;Extreme Programming and Agile Methods - XP/Agile Universe 2004&amp;lt;/i&amp;gt;. 4th Conference on Extreme Programming and Agile Methods, Calgary, Canada, August 15-18, 2004. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false Domain-driven design: tackling complexity in the heart of software]. By Eric Evans. books.google.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Domain-driven_design Domain Driven Design]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false Test-driven development: by example]. By Kent Beck. books. google.com. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Test-driven_development Test Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.davidchelimsky.net/ David Chelimsky Blog]. davidchelimsky.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/ Understanding RSpec Stories - A Tutorial]. emson.co.uk. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.dannorth.net/2007/06/17/introducing-rbehave/ rbehave]. dannorth.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-lang.org Ruby Website]. ruby-lang.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.pragprog.com/titles/achbd/the-rspec-book RSpec Book]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/ RSpec - Expectations]. wordpress.com. Retrieved Sep 17, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35346</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1f vn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35346"/>
		<updated>2010-09-18T01:06:12Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* The have() method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the different Unit-Testing Frameworks available for Ruby.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Unit testing is a method by which we can isolate and test a unit functionality of the program, typically individual methods during and long after the code is written. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing]&amp;lt;/sup&amp;gt; It helps to identify errors in the program even without running the entire program. It also helps to do regressing testing to identify buggy code additions in the future. Unit testing frameworks provides us with constructs which simplifies the process of unit testing. Using a standard unit test framework helps other developers to add test cases easily. &amp;lt;sup&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing]&amp;lt;/sup&amp;gt; This chapter walks through three different unit testing frameworks available for Ruby and explains how to use them with examples. The three commonly used unit testing frameworks for ruby are &lt;br /&gt;
&lt;br /&gt;
# Test::Unit&lt;br /&gt;
# Shoulda&lt;br /&gt;
# RSpec&lt;br /&gt;
&lt;br /&gt;
=Test::Unit=&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
Ruby comes with an in-built, ready to use unit testing framework called Test::Unit. It is a XUnit type framework and typically have a setup method for initialization, a teardown method for cleanup and the actual test methods itself. The tests themselves are bundled separately in a test class from the code it is testing.&lt;br /&gt;
&lt;br /&gt;
==Test Fixture==&lt;br /&gt;
Test fixture represents the initial environment setup(eg. initialization data) and/or the expected outcome of the tests for that environment. This is typically done in the setup() and teardown() methods and it helps to separate test initialization and cleanup from the actual tests. It also helps to reuse the same fixture for more than one tests.&amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html]&amp;lt;/sup&amp;gt; &lt;br /&gt;
&lt;br /&gt;
For example, consider a method &amp;lt;i&amp;gt;prime_check(num)&amp;lt;/i&amp;gt; which takes an integer number as input and outputs whether it is prime number or not. In order to unit test this method we can create the following fixture containing a 2-dimensional array with a number and the expected output of whether it is prime or not.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @NUMBERS = [[3,true], [4,false], [7,true], [10,false]]    &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Assertions==&lt;br /&gt;
The core part of test::unit framework is the ability to assert a statement of expected outcome. If an assert statement is correct then the test will proceed, otherwise the test will fail. This feature helps us to verify the method under test with different types of inputs and track the results. Test::unit provides a bunch of assert methods for this purpose: &lt;br /&gt;
&lt;br /&gt;
{| border=1 cellspacing=0 cellpadding=5&lt;br /&gt;
| assert( boolean, [message] ) &lt;br /&gt;
| True if ''boolean''&lt;br /&gt;
|- &lt;br /&gt;
| assert_equal( expected, actual, [message] )&amp;lt;br&amp;gt;assert_not_equal( expected, actual, [message] )&lt;br /&gt;
| True if ''expected == actual''&lt;br /&gt;
|-&lt;br /&gt;
| assert_raise( Exception,... ) {block}&amp;lt;br&amp;gt;assert_nothing_raised( Exception,...) {block} &lt;br /&gt;
| True if the block raises (or doesn't) one of the listed exceptions.&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For the full list of assertion methods provided by test::unit refer to test::unit assertions. &amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
The test case class &amp;lt;i&amp;gt;BinarySearchTest&amp;lt;/i&amp;gt; subclasses the &amp;lt;i&amp;gt;Test::Unit::TestCase&amp;lt;/i&amp;gt; class and overrides &amp;lt;i&amp;gt;setup&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;teardown&amp;lt;/i&amp;gt; methods. The test methods should start with 'test_' prefix. This helps in isolating the test methods from the helper methods if any. The Test::Unit::TestCase class takes care of making the test methods into tests, wrapping them into a suite and running the individual tests. The test results are collected into &amp;lt;i&amp;gt;Test::Unit::TestResult&amp;lt;/i&amp;gt; object.&lt;br /&gt;
&lt;br /&gt;
    require 'test/unit'&lt;br /&gt;
    require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
    class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
 &lt;br /&gt;
      def setup&lt;br /&gt;
        @input_array = [1,2,3,4,5]&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      def test_success_left_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,1),true)&lt;br /&gt;
        assert_equal(binary_search(@input_array,2),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_right_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,5),true)&lt;br /&gt;
        assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_middle&lt;br /&gt;
        assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_failure&lt;br /&gt;
        assert_equal(binary_search(@input_array,6),false)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def teardown&lt;br /&gt;
        #nothing to do here&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we have four test methods testing different logical paths of the binary search algorithm. Each test method can have one or more assert statements to test whether conditions are correct in each situation. To run the tests we simply have to run the file binary_search_test.rb and the output is as follows:&lt;br /&gt;
 &lt;br /&gt;
  Loaded suite binarysearch&lt;br /&gt;
  Started&lt;br /&gt;
  F...&lt;br /&gt;
  Finished in 0.372 seconds.&lt;br /&gt;
  &lt;br /&gt;
    1) Failure:&lt;br /&gt;
  test_failure(BinarySearchTest) [binarysearch.rb:25]:&lt;br /&gt;
  &amp;lt;true&amp;gt; expected but was&lt;br /&gt;
  &amp;lt;false&amp;gt;.&lt;br /&gt;
  &lt;br /&gt;
  4 tests, 6 assertions, 1 failures, 0 errors&lt;br /&gt;
&lt;br /&gt;
The results show that the last test case &amp;lt;i&amp;gt;test_failure&amp;lt;/i&amp;gt;, testing the negative scenario is failing. The reason is because the assert statement is expecting &amp;lt;i&amp;gt;false&amp;lt;/i&amp;gt; when number 6, which not present in the array is passed. But the binary_search method is returning true.&lt;br /&gt;
&lt;br /&gt;
==Test Suite==&lt;br /&gt;
Sometimes it is useful to combine a bunch of related test cases and run them as batch. Test::Unit provides a class called TestSuite for this purpose. The below example demonstrates how to bundle binary and sequential test case classes into a single search test suite.&lt;br /&gt;
&lt;br /&gt;
   require 'test/unit/testsuite'&lt;br /&gt;
   require 'binary_search_test'&lt;br /&gt;
   require 'sequential_search_test'&lt;br /&gt;
  &lt;br /&gt;
   class Search_Tests&lt;br /&gt;
     def self.suite&lt;br /&gt;
       suite = Test::Unit::TestSuite.new&lt;br /&gt;
       suite &amp;lt;&amp;lt; BinarySearchTest.suite&lt;br /&gt;
       suite &amp;lt;&amp;lt; SequentialSearchTest.suite&lt;br /&gt;
       return suite&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   Test::Unit::UI::Console::TestRunner.run(Search_Tests)&lt;br /&gt;
&lt;br /&gt;
=Shoulda=&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
One of the downsides of Test::Unit is we end up writing lots of code in order to test the actual code which is sometimes not easy to understand. Shoulda is a library that allows us to write better and more understandable tests for ruby application. Shoulda is not a testing framework by itself. It extends the Test::Unit framework with the idea of &amp;lt;i&amp;gt;context&amp;lt;/i&amp;gt;. We can mix Test::Unit test cases with Shoulda test cases. Shoulda allows us to provide context to the tests so that we can group the tests according to a specific feature or scenario. &amp;lt;sup&amp;gt;[http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
   require 'shoulda'&lt;br /&gt;
   require 'test/unit'&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
   class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
      &lt;br /&gt;
     context &amp;quot;Input array of size 5&amp;quot; do&lt;br /&gt;
      &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1,2,3,4,5]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the left half of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the right half of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,5),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the middle of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,6),false)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
    &lt;br /&gt;
     context &amp;quot;Input array of size 1&amp;quot; do&lt;br /&gt;
     &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)         &lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)         &lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Notice that we are still sub-classing the Test::Unit::TestCase class. In this example we have two contexts one for input array of size 5 and the other for input array of size 1. Each context has its own environment of setup/teardown methods. We can also create nested contexts - the outer setup gets run before the execution of each of the inner contexts. And the setup in the inner contexts gets run when running that context. Each &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; construct is converted into individual test methods and are run. If a test case fails we will get a better description of what that test case is doing from the &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; description.&lt;br /&gt;
&lt;br /&gt;
=RSpec=&lt;br /&gt;
&lt;br /&gt;
Now let us consider about &amp;lt;i&amp;gt;RSpec&amp;lt;/i&amp;gt; Testing Framework in detail.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Behaviour Driven Development&amp;lt;/b&amp;gt; (BDD) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Behavior_Driven_Development]&amp;lt;/sup&amp;gt; is an Agile development process &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Agile_software_development]&amp;lt;/sup&amp;gt; that comprises aspects of Acceptance Test Driven Planning &amp;lt;sup&amp;gt;[http://www.springerlink.com/content/978-3-540-22839-4/] [http://en.wikipedia.org/wiki/Acceptance_testing]&amp;lt;/sup&amp;gt;, Domain Driven Design &amp;lt;sup&amp;gt;[http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Domain-driven_design]&amp;lt;/sup&amp;gt; and Test Driven Development (TDD). &amp;lt;sup&amp;gt;[http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Test-driven_development]&amp;lt;/sup&amp;gt; &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;RSpec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; is a Behavioural Driven Development (BDD) tool aimed at Test Driven Development, originally created by Dave Astels and Steven Baker. However David Chelimsky &amp;lt;sup&amp;gt;[http://blog.davidchelimsky.net/]&amp;lt;/sup&amp;gt; is really the gatekeeper of the RSpec project. &amp;lt;sup&amp;gt;[http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Traditionally we use Unit Test frameworks like JUnit, NUnit or RUnit for writing Test cases. We spend a lot of time writing tests that test every unit of code in our software system. Instead we can shift our focus from Unit testing to Behaviour testing or Behaviour Driven Development (BDD) using RSpec. By focusing on the behaviour of the system it helps clarify in our minds what the system should actually be doing. It also helps us to perform more ‘useful’ tests. Useful tests, cover what the system should be doing and build in enough redundancy so that it should be easy to refactor our code without having to re-write every test.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
RSpec is really two projects merged into one. The RSpec project pages describes these merged projects as:&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;application level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Story Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;object level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Spec Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dan North created &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;rbehave&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; &amp;lt;sup&amp;gt;[http://blog.dannorth.net/2007/06/17/introducing-rbehave/]&amp;lt;/sup&amp;gt; which is the Story Framework and David Chelimsky created the &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;Spec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; Framework. By encompassing two frameworks RSpec equips a programmer with a thorough set of testing tools, allowing you to think about your software problem from a number of perspectives.&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
&lt;br /&gt;
The prerequisites are&lt;br /&gt;
&lt;br /&gt;
# Ruby 1.8.4 or later&lt;br /&gt;
# RSpec Gem (latest)&lt;br /&gt;
&lt;br /&gt;
To install Ruby, please visit official Ruby Website &amp;lt;sup&amp;gt;[http://www.ruby-lang.org/]&amp;lt;/sup&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
To install RSpec, open a command shell, go to /bin folder in Ruby directory and type&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;gt; gem install rspec&lt;br /&gt;
&lt;br /&gt;
==Terms &amp;amp; Definitions==&lt;br /&gt;
&lt;br /&gt;
Here are some terms which are used frequently while working with RSpec. &amp;lt;sup&amp;gt;[http://www.pragprog.com/titles/achbd/the-rspec-book/ ]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;b&amp;gt;subject code&amp;lt;/b&amp;gt; - The code whose behavior is specified using RSpec&lt;br /&gt;
# &amp;lt;b&amp;gt;expectation&amp;lt;/b&amp;gt; - The expected behavior of subject code is expressed using expectation (Similar to 'Assertions' statements used in Test::Unit or other tools in other languages)&lt;br /&gt;
# &amp;lt;b&amp;gt;code example&amp;lt;/b&amp;gt; - An executable example containing the subject code and the expectations (Similar to 'Test Method' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;example group&amp;lt;/b&amp;gt; - A group of code examples (Similar to 'Test Case' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;spec file&amp;lt;/b&amp;gt; - A file which contains one or more example groups&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Let us go through an example to be clear on the usage of RSpec.&lt;br /&gt;
&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
   &lt;br /&gt;
   describe BinarySearchTest do&lt;br /&gt;
     before(:all) do&lt;br /&gt;
       @input_array = [1, 2, 3, 4, 5] # The Input Array&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     after(:all) do&lt;br /&gt;
       # do nothing here&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the left-half of the array&amp;quot; do  # Test case for element to be present in left-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(1)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the right-half of the array&amp;quot; do  # Test case for element to be present in right-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(5)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the middle of the array&amp;quot; do  # Test case for element to be present in the middle of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(3)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should not be in the array&amp;quot; do  # Test case for element NOT to be present in given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should_not be_binary_search(7)&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here it is assumed that the method binary_search will return true/false based on whether the provided value exists in the array or not.&lt;br /&gt;
&lt;br /&gt;
====describe() method====&lt;br /&gt;
&lt;br /&gt;
The describe() method can take an arbitrary number of arguments and a block and returns a sub-class of Spec::Example::ExampleGroup. We generally use only one or two arguments which is used to describe the behavior. The first argument can be a reference to a Class or module or a string. The second argument is optional and should be a string when used.&lt;br /&gt;
&lt;br /&gt;
====it() method====&lt;br /&gt;
&lt;br /&gt;
Similar to the describe() method, the it() method takes a single String, an optional Hash and an optional block. The String expression within the it() should be such that it informs the behavior of the code within the block.&lt;br /&gt;
&lt;br /&gt;
==Expectations in RSpec==&lt;br /&gt;
&lt;br /&gt;
There are two methods available for checking expectations: should() and should_not(). Both the methods accept either an expression matcher or a Ruby expression using a specific subset of Ruby operators. An expression matcher is an objects that matches an expression.&lt;br /&gt;
&lt;br /&gt;
===Built-in Matchers===&lt;br /&gt;
&lt;br /&gt;
There are several matchers that can be used with should and should_not, which are divided into well-separated categories.&lt;br /&gt;
====Equality====&lt;br /&gt;
 subject.should == ece517&lt;br /&gt;
 subject.should === ece517&lt;br /&gt;
 subject.should eql(subject)&lt;br /&gt;
 subject.should equal(subject)&lt;br /&gt;
&lt;br /&gt;
The == method is used to express equivalence and equal is used when you want the receiver and the argument to be the same object. Instead of using !=, you should use the should_not method!&lt;br /&gt;
&lt;br /&gt;
====Floating Point Calculations====&lt;br /&gt;
 piValue.should be_close(3.14, 0.001593)&lt;br /&gt;
&lt;br /&gt;
Sometimes the values generated might be correct upto some fixed decimal positions, after that they may have slight variations. To avoid the test beings failed, we provide the (value, delta) to be_close method which passes the test if the obtained value lies within the range (value+delta).&lt;br /&gt;
&lt;br /&gt;
====Regular Expressions====&lt;br /&gt;
 resultExpression.should match(/this regular expression/)&lt;br /&gt;
 resultExpression.should =~ /this regular expression/&lt;br /&gt;
&lt;br /&gt;
This can be very useful when dealing with multiple-line expectations, instead of using the open file technique to compare contents.&lt;br /&gt;
&lt;br /&gt;
====Changes====&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.to(1)&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.from(0).to(1)&lt;br /&gt;
 &lt;br /&gt;
This is really useful when working with database changes or changes to objects. The matcher is change(), which takes a block and accepts the from(), to() or by() modifiers. &amp;lt;sup&amp;gt;[18]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Errors====&lt;br /&gt;
 field = CricketGround.new(:players =&amp;gt; 11)&lt;br /&gt;
 lambda {&lt;br /&gt;
  field.remove(:players, 15)&lt;br /&gt;
 }.should raise_error(NotEnoughPlayers,“attempted to remove more players than there is on cricket stadium”)&lt;br /&gt;
&lt;br /&gt;
Useful when needed to check for Exceptions. The matcher is raise_error and takes an ExceptionObject and/or a String/Regexp.&lt;br /&gt;
 &lt;br /&gt;
====Throw====&lt;br /&gt;
 speech = Speech.new(:seats =&amp;gt; 100)&lt;br /&gt;
 100.times { speech.register Person.new }&lt;br /&gt;
 lambda {&lt;br /&gt;
  speech.register Person.new&lt;br /&gt;
 }.should throw_symbol(:speech_full, 100)&lt;br /&gt;
&lt;br /&gt;
When dealing with “errors that are not really exceptions”, you use catch and throw. Rspec can check if a throw has been called by using the throw_symbol matcher. It accepts 0,1 or 2 arguments. The first argument needs to be a Symbol and the second can be any Object that is thrown along.&lt;br /&gt;
&lt;br /&gt;
===Predicate Matchers===&lt;br /&gt;
A Ruby predicate method is a method that ends with a “?” and returns a boolean value, like string.empty? or regexp.match? methods. Instead of writing:&lt;br /&gt;
 a_string.empty?.should == true&lt;br /&gt;
We can write using RSpec:&lt;br /&gt;
 a_string.should be_empty&lt;br /&gt;
&lt;br /&gt;
When using a be_something matcher, RSpec removes the “be_”, appends a “?” and calls the resulting method in the receiver. A very common construct of this method is be_true, which checks if the receiver is true (any object except false or nil) or false (false or nil).&lt;br /&gt;
&lt;br /&gt;
===Check Ownership===&lt;br /&gt;
Sometimes you will want to check something the object owns and not the object itself.&lt;br /&gt;
&lt;br /&gt;
====The have_something() method====&lt;br /&gt;
 security_access.has_key?(:id).should == true&lt;br /&gt;
is the same as&lt;br /&gt;
 security_access.should have_key(:id)&lt;br /&gt;
&lt;br /&gt;
RSpec uses method_missing to convert anything that begins with have_something to has_something? and performs the checking.&lt;br /&gt;
&lt;br /&gt;
====The have() method====&lt;br /&gt;
 field.players.select {|p| p.team == home_team }.length.should == 9&lt;br /&gt;
is the same as&lt;br /&gt;
 home_team.should have(9).players_on(field)&lt;br /&gt;
 &lt;br /&gt;
As have() does not respond to players_on(), it delegates to the receiver (home_team). It encourages the home_team object to have useful methods like players_on.&amp;lt;br&amp;gt;&lt;br /&gt;
You can get a NoMethodError if the players_on method doesn´t exist, you can get another NoMethodError if the result of the players_on method doesn´t respond to size() or length() and if the size of the collection doesn´t match the expected size, you will get a failed expectation. &amp;lt;sup&amp;gt;[http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Checking Collections Themselves===&lt;br /&gt;
Sometimes we create expectations about a collection itself and not about an owned collection. RSpec lets us use the have() method to express this as well, as in:&lt;br /&gt;
 basket_collection.should have(10).items&lt;br /&gt;
items is just providing some meaning to the expectation.&lt;br /&gt;
&lt;br /&gt;
====Strings====&lt;br /&gt;
Strings are not collections by definition but they respond to a lot of methods that collections do, like length() and size(). This allow us to use have() to expect a string of a specific length.&lt;br /&gt;
 “apple”.should have(5).characters&lt;br /&gt;
characters is just providing meaning to the expectation as well.&lt;br /&gt;
&lt;br /&gt;
====Have() modifiers for precision====&lt;br /&gt;
The have() method has some relatives that allow us to check for upper and lower conditions.&lt;br /&gt;
&lt;br /&gt;
 work.should have_exactly(8).hours&lt;br /&gt;
 classroom.should have_at_most(100).people&lt;br /&gt;
 bag.should have_at_least(5).items&lt;br /&gt;
&lt;br /&gt;
===Operator Expressions===&lt;br /&gt;
There may be sometimes when you want to expect a value to be not an exact amount but something like greater than or less than. RSpec allows you to do this by using the regular operators from Ruby!&lt;br /&gt;
&lt;br /&gt;
 number.should == 3&lt;br /&gt;
 number.should be &amp;gt;= 2&lt;br /&gt;
 number.should be &amp;lt;= 4&lt;br /&gt;
 number should be &amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Unit_testing Unit Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Programming &amp;amp; Unit Testing]. en.wikibooks.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Ruby Test::Unit]. ruby-doc.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Ruby Assertions]. ruby-doc.org. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html Shoulda Explained]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavior Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Agile_software_development Agile Software Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.springerlink.com/content/978-3-540-22839-4/ Proceedings] Carmen Zannier, Hakan Erdogmus and Lowell Lindstrom. &amp;lt;i&amp;gt;Extreme Programming and Agile Methods - XP/Agile Universe 2004&amp;lt;/i&amp;gt;. 4th Conference on Extreme Programming and Agile Methods, Calgary, Canada, August 15-18, 2004. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false Domain-driven design: tackling complexity in the heart of software]. By Eric Evans. books.google.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Domain-driven_design Domain Driven Design]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false Test-driven development: by example]. By Kent Beck. books. google.com. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Test-driven_development Test Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.davidchelimsky.net/ David Chelimsky Blog]. davidchelimsky.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/ Understanding RSpec Stories - A Tutorial]. emson.co.uk. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.dannorth.net/2007/06/17/introducing-rbehave/ rbehave]. dannorth.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-lang.org Ruby Website]. ruby-lang.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.pragprog.com/titles/achbd/the-rspec-book RSpec Book]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/ RSpec - Expectations]. wordpress.com. Retrieved Sep 17, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35344</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1f vn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1f_vn&amp;diff=35344"/>
		<updated>2010-09-18T01:05:41Z</updated>

		<summary type="html">&lt;p&gt;Thatvamasi: /* Prerequisites */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding the different Unit-Testing Frameworks available for Ruby.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Unit testing is a method by which we can isolate and test a unit functionality of the program, typically individual methods during and long after the code is written. &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing]&amp;lt;/sup&amp;gt; It helps to identify errors in the program even without running the entire program. It also helps to do regressing testing to identify buggy code additions in the future. Unit testing frameworks provides us with constructs which simplifies the process of unit testing. Using a standard unit test framework helps other developers to add test cases easily. &amp;lt;sup&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing]&amp;lt;/sup&amp;gt; This chapter walks through three different unit testing frameworks available for Ruby and explains how to use them with examples. The three commonly used unit testing frameworks for ruby are &lt;br /&gt;
&lt;br /&gt;
# Test::Unit&lt;br /&gt;
# Shoulda&lt;br /&gt;
# RSpec&lt;br /&gt;
&lt;br /&gt;
=Test::Unit=&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
Ruby comes with an in-built, ready to use unit testing framework called Test::Unit. It is a XUnit type framework and typically have a setup method for initialization, a teardown method for cleanup and the actual test methods itself. The tests themselves are bundled separately in a test class from the code it is testing.&lt;br /&gt;
&lt;br /&gt;
==Test Fixture==&lt;br /&gt;
Test fixture represents the initial environment setup(eg. initialization data) and/or the expected outcome of the tests for that environment. This is typically done in the setup() and teardown() methods and it helps to separate test initialization and cleanup from the actual tests. It also helps to reuse the same fixture for more than one tests.&amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html]&amp;lt;/sup&amp;gt; &lt;br /&gt;
&lt;br /&gt;
For example, consider a method &amp;lt;i&amp;gt;prime_check(num)&amp;lt;/i&amp;gt; which takes an integer number as input and outputs whether it is prime number or not. In order to unit test this method we can create the following fixture containing a 2-dimensional array with a number and the expected output of whether it is prime or not.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @NUMBERS = [[3,true], [4,false], [7,true], [10,false]]    &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Assertions==&lt;br /&gt;
The core part of test::unit framework is the ability to assert a statement of expected outcome. If an assert statement is correct then the test will proceed, otherwise the test will fail. This feature helps us to verify the method under test with different types of inputs and track the results. Test::unit provides a bunch of assert methods for this purpose: &lt;br /&gt;
&lt;br /&gt;
{| border=1 cellspacing=0 cellpadding=5&lt;br /&gt;
| assert( boolean, [message] ) &lt;br /&gt;
| True if ''boolean''&lt;br /&gt;
|- &lt;br /&gt;
| assert_equal( expected, actual, [message] )&amp;lt;br&amp;gt;assert_not_equal( expected, actual, [message] )&lt;br /&gt;
| True if ''expected == actual''&lt;br /&gt;
|-&lt;br /&gt;
| assert_raise( Exception,... ) {block}&amp;lt;br&amp;gt;assert_nothing_raised( Exception,...) {block} &lt;br /&gt;
| True if the block raises (or doesn't) one of the listed exceptions.&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For the full list of assertion methods provided by test::unit refer to test::unit assertions. &amp;lt;sup&amp;gt;[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
The test case class &amp;lt;i&amp;gt;BinarySearchTest&amp;lt;/i&amp;gt; subclasses the &amp;lt;i&amp;gt;Test::Unit::TestCase&amp;lt;/i&amp;gt; class and overrides &amp;lt;i&amp;gt;setup&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;teardown&amp;lt;/i&amp;gt; methods. The test methods should start with 'test_' prefix. This helps in isolating the test methods from the helper methods if any. The Test::Unit::TestCase class takes care of making the test methods into tests, wrapping them into a suite and running the individual tests. The test results are collected into &amp;lt;i&amp;gt;Test::Unit::TestResult&amp;lt;/i&amp;gt; object.&lt;br /&gt;
&lt;br /&gt;
    require 'test/unit'&lt;br /&gt;
    require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
    class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
 &lt;br /&gt;
      def setup&lt;br /&gt;
        @input_array = [1,2,3,4,5]&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      def test_success_left_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,1),true)&lt;br /&gt;
        assert_equal(binary_search(@input_array,2),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_right_half&lt;br /&gt;
        assert_equal(binary_search(@input_array,5),true)&lt;br /&gt;
        assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_success_middle&lt;br /&gt;
        assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def test_failure&lt;br /&gt;
        assert_equal(binary_search(@input_array,6),false)&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      def teardown&lt;br /&gt;
        #nothing to do here&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we have four test methods testing different logical paths of the binary search algorithm. Each test method can have one or more assert statements to test whether conditions are correct in each situation. To run the tests we simply have to run the file binary_search_test.rb and the output is as follows:&lt;br /&gt;
 &lt;br /&gt;
  Loaded suite binarysearch&lt;br /&gt;
  Started&lt;br /&gt;
  F...&lt;br /&gt;
  Finished in 0.372 seconds.&lt;br /&gt;
  &lt;br /&gt;
    1) Failure:&lt;br /&gt;
  test_failure(BinarySearchTest) [binarysearch.rb:25]:&lt;br /&gt;
  &amp;lt;true&amp;gt; expected but was&lt;br /&gt;
  &amp;lt;false&amp;gt;.&lt;br /&gt;
  &lt;br /&gt;
  4 tests, 6 assertions, 1 failures, 0 errors&lt;br /&gt;
&lt;br /&gt;
The results show that the last test case &amp;lt;i&amp;gt;test_failure&amp;lt;/i&amp;gt;, testing the negative scenario is failing. The reason is because the assert statement is expecting &amp;lt;i&amp;gt;false&amp;lt;/i&amp;gt; when number 6, which not present in the array is passed. But the binary_search method is returning true.&lt;br /&gt;
&lt;br /&gt;
==Test Suite==&lt;br /&gt;
Sometimes it is useful to combine a bunch of related test cases and run them as batch. Test::Unit provides a class called TestSuite for this purpose. The below example demonstrates how to bundle binary and sequential test case classes into a single search test suite.&lt;br /&gt;
&lt;br /&gt;
   require 'test/unit/testsuite'&lt;br /&gt;
   require 'binary_search_test'&lt;br /&gt;
   require 'sequential_search_test'&lt;br /&gt;
  &lt;br /&gt;
   class Search_Tests&lt;br /&gt;
     def self.suite&lt;br /&gt;
       suite = Test::Unit::TestSuite.new&lt;br /&gt;
       suite &amp;lt;&amp;lt; BinarySearchTest.suite&lt;br /&gt;
       suite &amp;lt;&amp;lt; SequentialSearchTest.suite&lt;br /&gt;
       return suite&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   Test::Unit::UI::Console::TestRunner.run(Search_Tests)&lt;br /&gt;
&lt;br /&gt;
=Shoulda=&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
One of the downsides of Test::Unit is we end up writing lots of code in order to test the actual code which is sometimes not easy to understand. Shoulda is a library that allows us to write better and more understandable tests for ruby application. Shoulda is not a testing framework by itself. It extends the Test::Unit framework with the idea of &amp;lt;i&amp;gt;context&amp;lt;/i&amp;gt;. We can mix Test::Unit test cases with Shoulda test cases. Shoulda allows us to provide context to the tests so that we can group the tests according to a specific feature or scenario. &amp;lt;sup&amp;gt;[http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
   require 'shoulda'&lt;br /&gt;
   require 'test/unit'&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
 &lt;br /&gt;
   class BinarySearchTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
      &lt;br /&gt;
     context &amp;quot;Input array of size 5&amp;quot; do&lt;br /&gt;
      &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1,2,3,4,5]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the left half of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the right half of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,5),true)&lt;br /&gt;
         assert_equal(binary_search(@input_array,4),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;have the number in the middle of the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,3),true)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,6),false)&lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
    &lt;br /&gt;
     context &amp;quot;Input array of size 1&amp;quot; do&lt;br /&gt;
     &lt;br /&gt;
       def setup&lt;br /&gt;
         @input_array = [1]&lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       should &amp;quot;have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,1),true)         &lt;br /&gt;
       end&lt;br /&gt;
 &lt;br /&gt;
       should &amp;quot;not have the number in the array&amp;quot; do&lt;br /&gt;
         assert_equal(binary_search(@input_array,2),true)         &lt;br /&gt;
       end&lt;br /&gt;
      &lt;br /&gt;
       def teardown&lt;br /&gt;
         #nothing to do here&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Notice that we are still sub-classing the Test::Unit::TestCase class. In this example we have two contexts one for input array of size 5 and the other for input array of size 1. Each context has its own environment of setup/teardown methods. We can also create nested contexts - the outer setup gets run before the execution of each of the inner contexts. And the setup in the inner contexts gets run when running that context. Each &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; construct is converted into individual test methods and are run. If a test case fails we will get a better description of what that test case is doing from the &amp;lt;i&amp;gt;should&amp;lt;/i&amp;gt; description.&lt;br /&gt;
&lt;br /&gt;
=RSpec=&lt;br /&gt;
&lt;br /&gt;
Now let us consider about &amp;lt;i&amp;gt;RSpec&amp;lt;/i&amp;gt; Testing Framework in detail.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Behaviour Driven Development&amp;lt;/b&amp;gt; (BDD) &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Behavior_Driven_Development]&amp;lt;/sup&amp;gt; is an Agile development process &amp;lt;sup&amp;gt;[http://en.wikipedia.org/wiki/Agile_software_development]&amp;lt;/sup&amp;gt; that comprises aspects of Acceptance Test Driven Planning &amp;lt;sup&amp;gt;[http://www.springerlink.com/content/978-3-540-22839-4/] [http://en.wikipedia.org/wiki/Acceptance_testing]&amp;lt;/sup&amp;gt;, Domain Driven Design &amp;lt;sup&amp;gt;[http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Domain-driven_design]&amp;lt;/sup&amp;gt; and Test Driven Development (TDD). &amp;lt;sup&amp;gt;[http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false] [http://en.wikipedia.org/wiki/Test-driven_development]&amp;lt;/sup&amp;gt; &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;RSpec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; is a Behavioural Driven Development (BDD) tool aimed at Test Driven Development, originally created by Dave Astels and Steven Baker. However David Chelimsky &amp;lt;sup&amp;gt;[http://blog.davidchelimsky.net/]&amp;lt;/sup&amp;gt; is really the gatekeeper of the RSpec project. &amp;lt;sup&amp;gt;[http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Traditionally we use Unit Test frameworks like JUnit, NUnit or RUnit for writing Test cases. We spend a lot of time writing tests that test every unit of code in our software system. Instead we can shift our focus from Unit testing to Behaviour testing or Behaviour Driven Development (BDD) using RSpec. By focusing on the behaviour of the system it helps clarify in our minds what the system should actually be doing. It also helps us to perform more ‘useful’ tests. Useful tests, cover what the system should be doing and build in enough redundancy so that it should be easy to refactor our code without having to re-write every test.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
RSpec is really two projects merged into one. The RSpec project pages describes these merged projects as:&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;application level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Story Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;object level&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; behaviour described by a &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Spec Framework&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dan North created &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;rbehave&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; &amp;lt;sup&amp;gt;[http://blog.dannorth.net/2007/06/17/introducing-rbehave/]&amp;lt;/sup&amp;gt; which is the Story Framework and David Chelimsky created the &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;Spec&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; Framework. By encompassing two frameworks RSpec equips a programmer with a thorough set of testing tools, allowing you to think about your software problem from a number of perspectives.&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
&lt;br /&gt;
The prerequisites are&lt;br /&gt;
&lt;br /&gt;
# Ruby 1.8.4 or later&lt;br /&gt;
# RSpec Gem (latest)&lt;br /&gt;
&lt;br /&gt;
To install Ruby, please visit official Ruby Website &amp;lt;sup&amp;gt;[http://www.ruby-lang.org/]&amp;lt;/sup&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
To install RSpec, open a command shell, go to /bin folder in Ruby directory and type&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;gt; gem install rspec&lt;br /&gt;
&lt;br /&gt;
==Terms &amp;amp; Definitions==&lt;br /&gt;
&lt;br /&gt;
Here are some terms which are used frequently while working with RSpec. &amp;lt;sup&amp;gt;[http://www.pragprog.com/titles/achbd/the-rspec-book/ ]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;b&amp;gt;subject code&amp;lt;/b&amp;gt; - The code whose behavior is specified using RSpec&lt;br /&gt;
# &amp;lt;b&amp;gt;expectation&amp;lt;/b&amp;gt; - The expected behavior of subject code is expressed using expectation (Similar to 'Assertions' statements used in Test::Unit or other tools in other languages)&lt;br /&gt;
# &amp;lt;b&amp;gt;code example&amp;lt;/b&amp;gt; - An executable example containing the subject code and the expectations (Similar to 'Test Method' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;example group&amp;lt;/b&amp;gt; - A group of code examples (Similar to 'Test Case' terminology used elsewhere)&lt;br /&gt;
# &amp;lt;b&amp;gt;spec file&amp;lt;/b&amp;gt; - A file which contains one or more example groups&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Let us go through an example to be clear on the usage of RSpec.&lt;br /&gt;
&lt;br /&gt;
   require 'binarysearch'&lt;br /&gt;
   &lt;br /&gt;
   describe BinarySearchTest do&lt;br /&gt;
     before(:all) do&lt;br /&gt;
       @input_array = [1, 2, 3, 4, 5] # The Input Array&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     after(:all) do&lt;br /&gt;
       # do nothing here&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the left-half of the array&amp;quot; do  # Test case for element to be present in left-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(1)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the right-half of the array&amp;quot; do  # Test case for element to be present in right-half of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(5)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should be in the middle of the array&amp;quot; do  # Test case for element to be present in the middle of given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should be_binary_search(3)&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     it &amp;quot;should not be in the array&amp;quot; do  # Test case for element NOT to be present in given array&lt;br /&gt;
       bst = BinarySearch.new&lt;br /&gt;
       bst.should_not be_binary_search(7)&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here it is assumed that the method binary_search will return true/false based on whether the provided value exists in the array or not.&lt;br /&gt;
&lt;br /&gt;
====describe() method====&lt;br /&gt;
&lt;br /&gt;
The describe() method can take an arbitrary number of arguments and a block and returns a sub-class of Spec::Example::ExampleGroup. We generally use only one or two arguments which is used to describe the behavior. The first argument can be a reference to a Class or module or a string. The second argument is optional and should be a string when used.&lt;br /&gt;
&lt;br /&gt;
====it() method====&lt;br /&gt;
&lt;br /&gt;
Similar to the describe() method, the it() method takes a single String, an optional Hash and an optional block. The String expression within the it() should be such that it informs the behavior of the code within the block.&lt;br /&gt;
&lt;br /&gt;
==Expectations in RSpec==&lt;br /&gt;
&lt;br /&gt;
There are two methods available for checking expectations: should() and should_not(). Both the methods accept either an expression matcher or a Ruby expression using a specific subset of Ruby operators. An expression matcher is an objects that matches an expression.&lt;br /&gt;
&lt;br /&gt;
===Built-in Matchers===&lt;br /&gt;
&lt;br /&gt;
There are several matchers that can be used with should and should_not, which are divided into well-separated categories.&lt;br /&gt;
====Equality====&lt;br /&gt;
 subject.should == ece517&lt;br /&gt;
 subject.should === ece517&lt;br /&gt;
 subject.should eql(subject)&lt;br /&gt;
 subject.should equal(subject)&lt;br /&gt;
&lt;br /&gt;
The == method is used to express equivalence and equal is used when you want the receiver and the argument to be the same object. Instead of using !=, you should use the should_not method!&lt;br /&gt;
&lt;br /&gt;
====Floating Point Calculations====&lt;br /&gt;
 piValue.should be_close(3.14, 0.001593)&lt;br /&gt;
&lt;br /&gt;
Sometimes the values generated might be correct upto some fixed decimal positions, after that they may have slight variations. To avoid the test beings failed, we provide the (value, delta) to be_close method which passes the test if the obtained value lies within the range (value+delta).&lt;br /&gt;
&lt;br /&gt;
====Regular Expressions====&lt;br /&gt;
 resultExpression.should match(/this regular expression/)&lt;br /&gt;
 resultExpression.should =~ /this regular expression/&lt;br /&gt;
&lt;br /&gt;
This can be very useful when dealing with multiple-line expectations, instead of using the open file technique to compare contents.&lt;br /&gt;
&lt;br /&gt;
====Changes====&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.to(1)&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 lambda {&lt;br /&gt;
  User.create!(:role =&amp;gt; &amp;quot;admin&amp;quot; )&lt;br /&gt;
 }.should change{ User.admins.count }.from(0).to(1)&lt;br /&gt;
 &lt;br /&gt;
This is really useful when working with database changes or changes to objects. The matcher is change(), which takes a block and accepts the from(), to() or by() modifiers. &amp;lt;sup&amp;gt;[18]&amp;lt;/sup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Errors====&lt;br /&gt;
 field = CricketGround.new(:players =&amp;gt; 11)&lt;br /&gt;
 lambda {&lt;br /&gt;
  field.remove(:players, 15)&lt;br /&gt;
 }.should raise_error(NotEnoughPlayers,“attempted to remove more players than there is on cricket stadium”)&lt;br /&gt;
&lt;br /&gt;
Useful when needed to check for Exceptions. The matcher is raise_error and takes an ExceptionObject and/or a String/Regexp.&lt;br /&gt;
 &lt;br /&gt;
====Throw====&lt;br /&gt;
 speech = Speech.new(:seats =&amp;gt; 100)&lt;br /&gt;
 100.times { speech.register Person.new }&lt;br /&gt;
 lambda {&lt;br /&gt;
  speech.register Person.new&lt;br /&gt;
 }.should throw_symbol(:speech_full, 100)&lt;br /&gt;
&lt;br /&gt;
When dealing with “errors that are not really exceptions”, you use catch and throw. Rspec can check if a throw has been called by using the throw_symbol matcher. It accepts 0,1 or 2 arguments. The first argument needs to be a Symbol and the second can be any Object that is thrown along.&lt;br /&gt;
&lt;br /&gt;
===Predicate Matchers===&lt;br /&gt;
A Ruby predicate method is a method that ends with a “?” and returns a boolean value, like string.empty? or regexp.match? methods. Instead of writing:&lt;br /&gt;
 a_string.empty?.should == true&lt;br /&gt;
We can write using RSpec:&lt;br /&gt;
 a_string.should be_empty&lt;br /&gt;
&lt;br /&gt;
When using a be_something matcher, RSpec removes the “be_”, appends a “?” and calls the resulting method in the receiver. A very common construct of this method is be_true, which checks if the receiver is true (any object except false or nil) or false (false or nil).&lt;br /&gt;
&lt;br /&gt;
===Check Ownership===&lt;br /&gt;
Sometimes you will want to check something the object owns and not the object itself.&lt;br /&gt;
&lt;br /&gt;
====The have_something() method====&lt;br /&gt;
 security_access.has_key?(:id).should == true&lt;br /&gt;
is the same as&lt;br /&gt;
 security_access.should have_key(:id)&lt;br /&gt;
&lt;br /&gt;
RSpec uses method_missing to convert anything that begins with have_something to has_something? and performs the checking.&lt;br /&gt;
&lt;br /&gt;
====The have() method====&lt;br /&gt;
 field.players.select {|p| p.team == home_team }.length.should == 9&lt;br /&gt;
is the same as&lt;br /&gt;
 home_team.should have(9).players_on(field)&lt;br /&gt;
 &lt;br /&gt;
As have() does not respond to players_on(), it delegates to the receiver (home_team). It encourages the home_team object to have useful methods like players_on.&amp;lt;br&amp;gt;&lt;br /&gt;
You can get a NoMethodError if the players_on method doesn´t exist, you can get another NoMethodError if the result of the players_on method doesn´t respond to size() or length() and if the size of the collection doesn´t match the expected size, you will get a failed expectation. [http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/]&lt;br /&gt;
&lt;br /&gt;
===Checking Collections Themselves===&lt;br /&gt;
Sometimes we create expectations about a collection itself and not about an owned collection. RSpec lets us use the have() method to express this as well, as in:&lt;br /&gt;
 basket_collection.should have(10).items&lt;br /&gt;
items is just providing some meaning to the expectation.&lt;br /&gt;
&lt;br /&gt;
====Strings====&lt;br /&gt;
Strings are not collections by definition but they respond to a lot of methods that collections do, like length() and size(). This allow us to use have() to expect a string of a specific length.&lt;br /&gt;
 “apple”.should have(5).characters&lt;br /&gt;
characters is just providing meaning to the expectation as well.&lt;br /&gt;
&lt;br /&gt;
====Have() modifiers for precision====&lt;br /&gt;
The have() method has some relatives that allow us to check for upper and lower conditions.&lt;br /&gt;
&lt;br /&gt;
 work.should have_exactly(8).hours&lt;br /&gt;
 classroom.should have_at_most(100).people&lt;br /&gt;
 bag.should have_at_least(5).items&lt;br /&gt;
&lt;br /&gt;
===Operator Expressions===&lt;br /&gt;
There may be sometimes when you want to expect a value to be not an exact amount but something like greater than or less than. RSpec allows you to do this by using the regular operators from Ruby!&lt;br /&gt;
&lt;br /&gt;
 number.should == 3&lt;br /&gt;
 number.should be &amp;gt;= 2&lt;br /&gt;
 number.should be &amp;lt;= 4&lt;br /&gt;
 number should be &amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Unit_testing Unit Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Programming &amp;amp; Unit Testing]. en.wikibooks.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Ruby Test::Unit]. ruby-doc.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Ruby Assertions]. ruby-doc.org. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://pragdave.blogs.pragprog.com/pragdave/2008/04/shoulda-used-th.html Shoulda Explained]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavior Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Agile_software_development Agile Software Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.springerlink.com/content/978-3-540-22839-4/ Proceedings] Carmen Zannier, Hakan Erdogmus and Lowell Lindstrom. &amp;lt;i&amp;gt;Extreme Programming and Agile Methods - XP/Agile Universe 2004&amp;lt;/i&amp;gt;. 4th Conference on Extreme Programming and Agile Methods, Calgary, Canada, August 15-18, 2004. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Testing]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=7dlaMs0SECsC&amp;amp;dq=domain+driven+design&amp;amp;printsec=frontcover&amp;amp;source=bn&amp;amp;hl=en&amp;amp;ei=ZPaTTJvIDIKB8gaykp2NDA&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;sqi=2&amp;amp;ved=0CCwQ6AEwAw#v=onepage&amp;amp;q&amp;amp;f=false Domain-driven design: tackling complexity in the heart of software]. By Eric Evans. books.google.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Domain-driven_design Domain Driven Design]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://books.google.com/books?id=gFgnde_vwMAC&amp;amp;printsec=frontcover&amp;amp;dq=test+driven+development&amp;amp;source=bl&amp;amp;ots=enLsruWrsF&amp;amp;sig=9pEP988f2rJQUmDd73Ka_3jrcCQ&amp;amp;hl=en&amp;amp;ei=IfeTTIcMwoHyBtXe8ZEM&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=3&amp;amp;sqi=2&amp;amp;ved=0CD8Q6AEwAg#v=onepage&amp;amp;q&amp;amp;f=false Test-driven development: by example]. By Kent Beck. books. google.com. Retrieved Sep 17, 2010. &lt;br /&gt;
# [http://en.wikipedia.org/wiki/Test-driven_development Test Driven Development]. en.wikipedia.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.davidchelimsky.net/ David Chelimsky Blog]. davidchelimsky.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/ Understanding RSpec Stories - A Tutorial]. emson.co.uk. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://blog.dannorth.net/2007/06/17/introducing-rbehave/ rbehave]. dannorth.net. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.ruby-lang.org Ruby Website]. ruby-lang.org. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://www.pragprog.com/titles/achbd/the-rspec-book RSpec Book]. pragprog.com. Retrieved Sep 17, 2010.&lt;br /&gt;
# [http://rubynoobie.wordpress.com/2010/01/27/rspec-expectations/ RSpec - Expectations]. wordpress.com. Retrieved Sep 17, 2010.&lt;/div&gt;</summary>
		<author><name>Thatvamasi</name></author>
	</entry>
</feed>