Wednesday, December 16, 2009

Static Variable

In C,
A variable declared as static in a function is initialized once, and retains its value between function calls.
It is saved in Data section, 
The default initial value of an uninitialized static variable is zero.
If a function or global variable is declared static, it can only be accessed in that file. IT Limits the scope to that file

In C++

Static member variable have only one copy per class Unlike other member variable which have Unique copies for every Object .We cannot initialize the static class member inside of the class.We cannot even initialize the static variable inside of the header file.
in .cpp we need to initialised it like this :
classname : : var_name = 0;

Static Functions are again independent of objects .


Main feature is "this" pointer is not passed to static functions because of that
1) A static member function Can't access NON STATIC MEMBERS can only access static member data
2)A static member function cannot be declared virtual, whereas a non-static member functions can be declared as virtual
3) Can't be declared const and volatile too.

4) Doesn't need Object to invoke .However if required can be invoked through object on need basis ,

Monday, December 14, 2009

Casting : Static , Dynamic, Reinterpret and Const cast

1) dynamic_cast :
  • It has a Run-time check Based on Run-Time Type Information (RTTI) Information type_info to keep track of dynamic types.Quite heavy ...
  • It  ensures that Result object will have all valid Data.
Upcast  : cast of Derived object to base Object is Allowed .
Downcast : cast of Base object to Derived Object is Not Allowed .
  • When dynamic_cast cannot cast a pointer because it is not a complete object of the required class it returns a null pointer to indicate the failure.
  • If dynamic_cast is used to convert to a reference type and the conversion is not possible, an exception of type bad_cast is thrown . As NULL assignment for Reference is not possible .

PS : It is Applicable only for Polymorphic classes.

Q  : What will happen when dynamic_cast is applied to a null pointer?
Ans :  It throws bad_typeid exception.


Q Where is  type_info (RTTI) is saved 
Ans VTABLE has a pointer (at begining ) pointing to type_info struct

2) Static_cast :
Wider than Dynamic (b2d : base to Derived , d2b : Derived to Base)
allows all d2b and b2d both Only condition is Pointer should be compatible type .
Pragremmer has to ensure result if b2d case if Pointer is Derefrenced ????

3) reinterpret_cast:
More wider than Static_cast :
anything to anything cast is allowed . Just do a memcpy bit by bit .
two reinterpret_cast s yields to same object as it was .

4) const_cast :
It changes the constness. Can remove or add constness for a variable .If we have to pass a const variable in Function who expects non-const  it can be used. To make it mutable

Example :

OUTPUT:
./a.out
Null pointer on second base to der DOWNCAST

Diffrence B/W delete and delete[]

Overview: delete is a operator in C++ , which can be overloaded .
What it does ??
IT frees the Dynamically Allocated Memory pointed by pointer and also calls Destructor for same pointer type .

Example :
*ptr=new ;
delete ptr;                     // frees and call desturctor also

delete [] is to free dynamically allocated Array , It also calls Destructor for all the objects contained by Array .It is used in conjunction of new[].

Example:
* ptr = new (10);                   //pointer to an array of 10 obj
then to free memory equal to 10 obj
delete []ptr;
NOTE: One can free the memory even by "delete ptr ;" But it
will free only the first element in Array memory.other Nine will stay as it is .
Also Desructor for the first Element will be called for sure .
 

Example
employee *arptr = new employee[3];

for (int j = 0 ;j < 3 ;j++) 


arptr[j].setvalue(j); 
}
//delete [] arptr;                              // frees and call all Dtors employee
* cloneptr = arptr; 
delete arptr;                                  // only delets First location and call Dtor for First obj 
cloneptr++;
cloneptr->gt;show(); // works fine able to see Variable of 2 nd OBJ NO core dumps

Verdict : This calling delete for array has different behaviour as per implementation   some times it corrupts HEAP and some time it jus call one destructor and free First Element . 
------------------------------------------------------------------------------------
FAQ on new and Delete 
Q what if we delete a ptr pointing to NULL? 
       No issues safe 
Q What if we call delete twice for same pointer ? 
     This is catastrophic . 
Q What is Placement new and placement delete  .
Q Why after Placement delete we need to call Destructor Explicilly?

Followers