April 14, 2016

Pure Virtual Functions and Memory Issues ..Part II

Hello ..

Continuing from Part 1.

Let me give a brief information about pure virtual functions.

Virtual Function
Any function that is declared in the base class and later redefined in the derived class is called as a virtual function.


class Univeristy
{
   int a;
   public:
       StudentInfo()
       {
          a = 1;
       }
       virtual void Display()
       {
          cout <<a;
       }
};

class College: public Univeristy
{
   int b;
   public:
       TotalStudentInfo()
       {
          b = 2;
       }
       virtual void Display()
       {
          cout <<b;
       }
};

int main()
{
   University *pA;
   College oB;
   pA = &oB;
   pADisplay();
   return 0;
}
 


In the above example we see that the function Display() has been declared virtual and in the base class it is made to display the value of a. However, in the derived class we have redefined the function to display the value b.

We know that the virtual function is a type of dynamic polymorphism  where the function call is resolved at the run time (also known as the dynamic binding) unlike the non virtual functions that go through the static binding process.

Let me share my understanding of how the virtual functions go through the dynamic binding process.

C++ uses a form of late binding known as the virtual table. As the name suggests, virtual table is a form of lookup table . The contents of the lookup table are the virtual functions.
The other names of the virtual table can be “vtable”, “virtual function table”, “virtual method table”, or “dispatch table”.

Here are some key points on virtual table :

  • Every class that has a virtual function is given a virtual table
  • Also any class that derives another class having a virtual function has its own virtual table
  • The table can be visualized as an array that compiler sets up at the time of compilation.
  • The table contains entries of the virtual functions that can be called by the objects of the respective class
  • So, each entry in this table is basically a function pointer which points to the function that is accessible by the class.
In our example above
  • There are two virtual tables. i.e. one for the base class ( University ) and one for the derived class College.
  • The entry in each of the virtual table would be of the function Display()  as it was declared to be a virtual function.
  • The entry is the function pointer to Display which is accessible to the object of each class. 
  •  The function pointer of  class Univeristy  will point to the function Display()  and will print the value of  "a" which is 1.
  • The function pointer of the class College  will point to the function Display()  which is defined in its class and will print the value of "b"  which is 2.

I have tried making a very rough sketch of the above concept



 As we see that the Display() has separate pointer to the derived class and the base class.





Pure Virtual Function
 


Abstract Class
Any class that contains a pure virtual function is an Abstract Clss.

To be Continued...


January 14, 2016

Pure Virtual Function and Memory Issues. ..Part 1

Hello, ನಮಸ್ಕಾರ

This is my first technical blog.

I am working on a project that uses a model something like this

The Problem Statement: Image 1





Let me explain the problem.

We have a GPS module from a company lets say X . We have our processor and the board from another company A, which has given its Tool-chain .
Our processor primarily supports C .
We have taken the GPS chip from X, which has given its code in  CPP. X has given its library which it has generated using our toolchain.
X has created the project primarily for windows environment. Our project is completely linux. So usage of windows headers and several WIN APIs is not possible in our project.
Though our processor does not support CPP , the toolchain can compile CPP codes but it has its own share of issues.

  1. We have to rename all the CPP files with .CC extension
  2. We have to use a few flags in the Makefile 
  3. The funny part is , we are not in a position to call the CPP function in C directly.
For few weeks we worked on trying to find a way out.
One of the methods that we  tried first was to use the concept of wrappers.
We followed the method given here Wrapping C++ Class API

This honestly works fine theoretically. Given the quirkiness of our ToolChain ,   we were in for a surprise when we could not call the cpp function from our C program. That means, we could not use the functions of GPS module given by X , in our project.
Then , we were trying to search for other alternatives. Then we came across another concept, of Name Mangling.

For example, if my function is
 GetGpsData()

  It would be converted to
 _Z10GetGpsDatav

after creating the object file.
Quite weird isn't it? We found these in the relevant object files and then we could call the CPP function. We were very upbeat about the success and hoped that all the CPP issues would come to an end and can work on putting out API's in the relevant places in the code given by X such as 'read GPS Data ' , write to the UART buffer.
The joy was short lived as the library given by X , had lot of CPP files and they used our great "Tool Chain" . They had created a library with their CPP files with .CPP extension. When we linked that library and compiled our code, we had a great difficulty in counting the error. Successive increments of the "compiler" buffer size failed to yield the starting point of the errors.
Then we have to beg "X" to give a new library with .CC extensions.. It took couple of days for them to respond and we finally had that.

Now, the next challenge was to implement our APIs. in the code given by X. From now, all the code or the functions that I will refer to will be with reference to the code given by X. Our project code, just calls the main function of the project file of X.
As you can see the image, We have our function , we call it the main function. We have access to it and we can modify only the function definitions,

Incomplete...Will add a LOT MORE shortly,,

Adding Code in the Blog

Hi I was searching how to make the blog look more technical.
I have added a Syntax Highlighter JavaScript Library to the Blog Template.

It seems there are two ways of adding the code but I found this one to be the simplest one
I am checking this blog.
I have followed the steps as shown here
Using the tag "Pre" before the code
printf("Testing");
Though it prints in a different format, I wanted a proper coding background the way we see in the IDEs or other coding platforms. I wanted something like this which I got from a website that creates the HTML
  

1
2
3
4
5
6
int main

{
printf("Swagatha Ellarigoo");//testing
I am unable to use the option mention in the first link. SO kindly check the first link and modify the template accordingly, Or Please look out for the sites that give us a inbuilt options that we can use while blogging.
1
2
3
4
5
6
int main

{
printf("Swagatha Ellarigoo");//testing

} 
 
Below is the code that I entered using prettify.
main()
{

printf("Hello World");
/* Testing Comments */

}
So Finally I have one working solution. I used the prettify script and is working fine.
all we have to do is use
  <pre class ="prettyprint"> MY CODE HERE </PRE> "
It seems the language will be chosen automatically.The only criteria is to be in the HTML mode while adding the code and using the pre tags.

Test Blog from Sharath

This is only a test item

Added Contents :-Kedar 14/1