Friday, September 30, 2016

Bigdata sample project

I have created sample bigdata project and kept it in Github

https://github.com/gobis/BigDataProject


I'll be updating github link in this post. 

Reversing singly Linked List - Explanation

I have searched some sites to know how they are reversing linked list , yeah I found so many sites but none of them are explained . So , I conclude it  to do in my blog



Below are the working piece of code 


 #include <iostream.h>  
 #include <conio.h>  
 class Node  
 {  
  public:  
    int data ;  
    Node * next ;   
    Node(int in){data = in; }   // public param cons  
    };  
 Node* Reverse(Node * head )  
  {  
      Node * temp = NULL ;  
      Node * np = NULL;  
     while (head)  
      {  
       temp = head->next;  
       head->next = np;  
       np = head ;  
       head = temp;     
      }  
      return np ;  
  }  
  void Display(Node * head )  
   {  
    while (head)  
    {  
     printf("%d \t" , head->data);  
     head = head-> next ;    
    }  
   }  
 main()  
 {  
   Node * obj1= new Node(10) ;  
   Node * obj2= new Node(20) ;  
   Node * obj3= new Node(30) ;  
   Node * obj4= new Node(40) ;  
   obj1->next = obj2 ;  
   obj2->next = obj3 ;  
   obj3->next = obj4 ;  
   printf("Before reversing ");    
   Display(obj1);    
   Node * head = Reverse(obj1);  
   printf("\n After reversing ");    
   Display(head);  
   getch();    
 }  

Lets see the explanation of Important lines 
  before getting in to that , you need two variable
1. temp -> used to hold next element pointer and given back the address to head
2. np    -> Assign it to head->next

lets discuss the code line by line

step1 :        temp = head->next;   
 take the information of next , so that we can play with head->next

step2:        head->next = np ;
 yeah , In the next line we are changing head->next  ( anyway we are having this info intemp   for later use)
initially np  is null , but from the second iteration it has the info of prev element

step3:       np = head ; 
yeh , this step does the same what we want in step 2

step4:    head = temp ;

finally head should move to next element . fortunately this info is stored in temp 


thats all dudes , np will become your new head pointer .

 It may not be the great explanation , but it will be used to the beginners .




Tuesday, September 20, 2016

Sunday, September 11, 2016

AES 128 Encryption : padding bytes issue

Recently I have encountered the padding issue with AES 128 encryption.

Mobile app is connecting to the gateway (which is in C++)  and communicating with each other.We have planned to encrypt the data. in C++ they have standard way of doing encryption and Android also have Cipher class to do encryption.

the problem here is ,
     Gateway code, giving "00" as padding bytes.


In android, we have different variant that provides various padding bytes

 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
 This will give padding bytes as 0F  

 Cipher cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding", "BC");
 This will give padding bytes as 00  


based on the padding bytes, you can choose the variant.


Friday, September 2, 2016

Understanding of virtual function

It is a function or method can be overridden within an inheriting class by a function with the same signature ,In other words , the purpose of virtual function is , to allow customization of derived class implementation


   “Where there is a casting there is a virtual”

Base * ptr = new Base() ; //  No need of virtual
Derived * ptr = new Derived();  // No need of virtual
Base * ptr = new Derived(); // need virtual to call correct function

Key benefits of Virtual function :

     It is allowed us to do function over riding
       
    Class Base { virtual void fn1() ; };
    Class derived: public base {void fn1() ;}

   Base* ptr = new derived() ;
    Ptr->fn1() ; this calls the derived class function fn1()

Above class become ptr->_vptr->fn1() ;

Now , consider if fn1() is not declared as virtual , then   the above call would call the Base class function .

Best part of this concepts is , you can have an array ,type of  Base* . and you can append derived classes object without worrying about function call .

 It is avoiding memory leak
  
   lets look at the below code
  
    Class Base { void ~Base() ; };
    Class derived: public base {void ~derived() ;}
   
    Base* ptr = new derived() ;
    delete ptr ;
   oops , there is a memory leak , above call only call base class destructor  .
  
wanna avoid memory leak
  Class Base { virtual void ~Base() ; };

Now , it will use vptr , and it will call the derived class destructor and subsequently it will call base class destructor

Disadvantages or Cost of virtual function

1.      If a class contains virtual function , then vtable is created , which means additional memory space required for that
2.      virtual function are mapped at runtime through vtable . hence calling virtual function is slow compared to normal function
3. vptr (virtual pointer which points to vtable) is created for every object .Additional 4 byte required to store vptr


pictorial representation of virtual function  







At last ,  If a function declared as virtual in base class , then the function in all derived class are virtual only . No need to mention it explicitly using “virtual” keyword


Some FAQ:

  1. What is the size of an empty class
Ans : 1 byte , because compiler provides minimum 1 byte to the object

  1. what is the size of the class  class A { virtual void fn1();};
      Ans : 4 byte ,  now vptr (virtual pointer comes in to the picture)
      
  1. What is pure virtual function
 Ans : A virtual function doesn’t have body
         Virtual void fn1() = 0;

  1. Can we create object of the class which has pure virtual function
Ans : No , Compiler will check the body of the function while creating object of the class , since it doesn’t have the body we can’t create the object and compiler will throw an error at the time of compilation

Synchronous AsyncTask in Android

In Android , there are some APIs (specially network related ) can called from another thread. and there will be some situation you will be encountered that , the main thread needs result of the Asynctask thread .

well , it is quite easy in android

you have to use below piece of code

             new TimerTask().execute().get();


timertask is extends from AsyncTask .

ie Thread1 will wait for thread2's result .

JNI step by step

1. Add native method in your activity class 
    for instance : public native String display(boolean status);

2. open command prompt and go to bin/classes
    Example :~/workspace/test/bin/classes$ javah -jni com.example.test.MainActivity
    Abstract :~/workspace/test/bin/classes$ javah -jni <class name where u have added your native API> 

    as a result , a header file will be created in   "test/bin/classes/" 
3. Copy that header file and paste it in to jni folder (create jni folder in the project )

4. Using signature of the header , create the cpp file and have definition for that method

5. Create Android.mk file (add the cpp file and module name inside it)

6. go to the project 
    Example:~/workspace/test$ "ndk-build.cmd"
    result : this will create lib (under the libs folder)

7. now, you can call the native method from the application