RTTI

*Run Time Type Identification
*enables us to identify type an object during execution of program

*RTTI operators are runtime events for polymorphic classes and compile time events for all other types.
* two type of RTTI operators  are:
     1. typeid
      2. dynamic_cast

typeid
*typeid is an operator which returns refrence to object of type_info class
*Type_info class describers type of object

#include<type_info.h>
main()
{
int i,j;
float f;
char* p;
A a;
B b;

cout<<"type of i is "<<typeid(i).name();//int
cout<<"type of f is "<<typeid(i).name(); //float
cout<<"type of p is "<<typeid(i).name(); //char*
cout<<"type of a is "<<typeid(i).name();//class A
cout<<"type of b is "<<typeid(i).name(); //class B
}


casting operators
*dynamic_cast
*static_cast
*cont_cast
*reinterpret_cast

syntax:
cast_name<type>(expression)

 



Binding

*Binding is an association of function calls to an objects.
*The normal bidind of member function call with an object is referred as compile time or static type or early binding
*c++ also supports runtime binding or dynamic binding or late binding


function binding:
binding  refers to the connection between a function call and the actual code executed as the result of the call.

in the process of an .exe file ,wherever the Linker encounters the  function call statement,it links it with that function difinition.which function definition to link the function call with ,is resolved by the compiler by mapping the logical addresses of the definitions at the compile-time itself.this process is called compile-time or static or early binding.

whenever the generic pointer is holding the address of the object of a derived class,and any function is invoked using that pointer,the compiler is unable to resolve this call at compile time .this is because the compiler can not invoke the appropriate function since the generic pointer will actually hold the address or will be type cast at run-time ()thats why it uses the static type of the pointer to govern the method invocation )

but in order to invoke the approprate function (i.e of the derived class),the compiler needs  to bind that funcion call to the correct function definition.The decision of which function to be invoked is taken at a later stage(i.e at run time).hence this feature is referred to as late (i.e run time)binding.This pointer is capable of pointing to an object at run time.hence it can invoke any function dynamically depending upon the type of the object that it is pointing to or the type of the object that it is type cast to.thats why it is also referred to as dynamic binding.
C++ supports this feature of dynamic biniding ny allowing us to typecast the generic pointer to the type of the object that it is pointing to.

Dangling pointer

Arising due to having lost the memory to which a pointer is suppose to be pointing at.in this case if we try to use the pointer the program may give unpredictictable results.

Scope of an object

class Test
{
 public:
      Test()
         {
        cout<<"constructor invoked"<<endl;
          }
     ~Test()
         {
        cout<<"Destructor invoked"<<endl;
          }
};



Test t1;
 void main(void)
{
cout<<"main begins";
Test t2;
{
cout<<"Block begins" <<endl;
Test t3;
cout<<"block Ends"<<endl;
}
cout<<"main ends"<<endl;
}



Output:
constructor invoked//for t1
main begins
constructor invoked//for t2
block begins
constructor invoked//for t3
block ends
Destructor invoked//for t3
main ends
Destructor invoked//for t2
Destructor invoked//for t1


Memory Leakage

Memory  Leakage:
Arising due to having lost the pointer to some memory that is allocated on heap.in this case the memory remains allocated and can't be freed because it's base address is lost.it leads to substantial wastage of the free storage on the heap.


int *p=new int[1000];
----
----
delete []p;

4bytes
------------
4000bytes

if delete p
only first 4 bytes freed which results in memory leakage of 3996 bytes

Note:Also it is very important to note that whenever we have allocated memory for more than one element i.e using subscript notation [size],it becomes necessary to de-allocate the memory by using the subscript notation [] in the delete operator as shown above.if this is not done and a delete  statement without a pair of
 [ ] is used,then only the first element whose address is contained in the pointer is deallocated resulting in the problem of memory leakage.

Memory leak
Memory leaks can be really annoying. The following list describes some scenarios that result in memory leaks.
  • Reassignment I'll use an example to explain reassignment.
    char *memoryArea = malloc(10);
    char *newArea = malloc(10);

    This assigns values to the memory locations shown in Figure 4 below.


    Figure 4. Memory locations


    memoryArea and newArea have been allocated 10 bytes each and their respective contents are shown in Figure 4. If somebody executes the statement shown below (pointer reassignment )…
    memoryArea = newArea; 

    then it will surely take you into tough times in the later stages of this module development.In the code statement above, the developer has assigned the memoryArea pointer to the newArea pointer. As a result, the memory location to which memoryArea was pointing to earlier becomes an orphan, as shown in Figure 5 below. It cannot be freed, as there is no reference to this location. This will result in a memory leak of 10 bytes.


    Figure 5. Memory leak


    Before assigning the pointers, make sure memory locations are not becoming orphaned.
  • Freeing the parent block first Suppose there is a pointer memoryArea pointing to a memory location of 10 bytes. The third byte of this memory location further points to some other dynamically allocated memory location of 10 bytes, as shown in Figure 6.


    Figure 6. Dynamically allocated memory


    free(memoryArea)

    If memoryArea is freed by making a call to free, then as a result the newArea pointer also will become invalid. The memory location to which newArea was pointing cannot be freed, as there is no pointer left pointing to that location. In other words, the memory location pointed by newArea becomes an orphan and results in memory leak.
    Whenever freeing the structured element, which in turn contains the pointer to dynamically allocated memory location, first traverse to the child memory location (newArea in the example) and start freeing from there, traversing back to the parent node.
    The correct implementation here will be:
    free( memoryArea->newArea);
    free(memoryArea);
  • Improper handling of return values At time, some functions return the reference to dynamically allocated memory. It becomes the responsibility of the calling function to keep track of this memory location and handle it properly.
    char *func ( )
    {
      return malloc(20); // make sure to memset this location to ‘\0’…
    }
    
    void callingFunc ( )
    {
      func ( ); // Problem lies here
    }

    In the example above, the call to the func() function inside the callingFunc() function is not handling the return address of the memory location. As a result, the 20 byte block allocated by the func() function is lost and results in a memory leak.


C++: Reference Variable:



C++:
Reference Variable:
Reference is an alias created for the variable.
Anallogy:Nickname

Important  example:
int  a=10;
  int &b=a;
  cout<<"value a="<<a;
  cout<<"value b="<<b; 
O/P:
Value a=10
Value b=10

& is a reference operator.syntax  int  &b=a; creatres another name b for the variable a.  i.e we can say b is a reference to a.
If we manipulate the value of b e.g b=10  then the value of a gets changed.
int a=10;
  int &b=a;
  b=20;
  cout<<"valu a"<<a;
  cout<<"valu b"<<b; 
o/p:
Value a=20
Value b=20

WHILE USING REFERENCE WE SHOULD KNOW……..
Reference have to be initialized
No memory is allocated to reference
Difference between pointer and reference
Pointer is flexible collaction
Reference is a rgid collection
Int *p=&I;
Int k;
P=&k;

A pointer has to be de-referenced before you can access the value at the addres contained in it. A reference is moreover a direct connection as it’s just another name for the same memory location.
We can have an arry of pointers whereas we can not have an aray of reference.

Const keyword
Constants should be initialized
Int i=10;
Const int*p=&I;
P=&k(correct)                                                                     (*p)++       incorrect
P++(correct)                                                                         *p=20 incorrect

Here,the pointer itself is not a constant and neither is the integer that it is pointing tp, a constant.we can change the address in the pointer by assigning some other address to it.but we cannot modify the contgents of the variable that it is pointing to,through that pointer.

Note:See some other cases like
1.pointer to a constant integer
2.contant pointer to an integer
3.constant pointer to a constant integer








                                               

   
     

Counters