Wednesday, October 27, 2010

Casting

 Casting Operators
There are several casting operators specific to the C++ language. These operators are intended to remove some of the ambiguity and danger inherent in old style C language casts. These operators are:
·         dynamic_cast   Used for conversion of polymorphic types.
·         static_cast   Used for conversion of nonpolymorphic types.
·         const_cast   Used to remove the const, volatile, and __unaligned attributes.
·         reinterpret_cast   Used for simple reinterpretation of bits.
·         safe_cast    Used to produce verifiable MSIL.
Use const_cast and reinterpret_cast as a last resort, since these operators present the same dangers as old style casts. However, they are still necessary in order to completely replace old style casts.

const_cast Operator

const_cast < type-id > ( expression )

 

A pointer to any object type or a pointer to a data member can be explicitly converted to a type that is identical except for the const, volatile, and __unalignedqualifiers. For pointers and references, the result will refer to the original object. For pointers to data members, the result will refer to the same member as the original (uncast) pointer to data member. Depending on the type of the referenced object, a write operation through the resulting pointer, reference, or pointer to data member might produce undefined behavior.
You cannot use the const_cast operator to directly override a constant variable's constant status.
The const_cast operator converts a null pointer value to the null pointer value of the destination type.
#include
using namespace std;
class CCTest {
public:
   void setNumber( int );
   void printNumber() const;
private:
   int number;
};

void CCTest::setNumber( int num ) { number = num; }

void CCTest::printNumber() const {
   cout << "\nBefore: " << number;
   const_cast< CCTest * >( this )->number--;   ****
   cout << "\nAfter: " << number;
}

int main() {
   CCTest X;
   X.setNumber( 8 );
   X.printNumber();
}
**** -- Here the const_cast, the data type of the this pointer is const CCTest *. The const_cast operator changes the data type of the this pointer toCCTest *, allowing the member number to be modified. The cast lasts only for the remainder of the line on which it appears.

dynamic_cast Operator

dynamic_cast < type-id > ( expression )

class B { };
class C : public B { };
class D : public C { };
 
void f(D* pd) {
   C* pc = dynamic_cast(pd);   // ok: C is a direct base class
                                   // pc points to C subobject of pd 
   B* pb = dynamic_cast(pd);   // ok: B is an indirect base class
                                   // pb points to B subobject of pd
}

This type of conversion is called an "upcast" because it moves a pointer up a class hierarchy, from a derived class to a class it is derived from. An upcast is an implicit conversion.
If type-id is void*, a run-time check is made to determine the actual type of expression. The result is a pointer to the complete object pointed to by expression. For example:

class B {virtual void f();};
class D : public B {virtual void f();};
 
void f() {
   B* pb = new D;   // unclear but ok
   B* pb2 = new B;
 
   D* pd = dynamic_cast(pb);   // ok: pb actually points to a D
   D* pd2 = dynamic_cast(pb2);   // pb2 points to a B not a D
}

This type of conversion is called a "downcast" because it moves a pointer down a class hierarchy, from a given class to a class derived from it.
In cases of multiple inheritance, possibilities for ambiguity are introduced. Consider the class hierarchy shown in the following figure.
For CLR types, dynamic_cast results in either a no-op if the conversion can be performed implicitly, or an MSIL isinst instruction, which performs a dynamic check and returns nullptr if the conversion fails.

static_cast Operator

static_cast  ( expression ) 

converts expression to the type of type-id based solely on the types present in the expression. No run-time type check is made to help ensure the safety of the conversion.
The static_cast operator can be used for operations such as converting a pointer to a base class to a pointer to a derived class. Such conversions are not always safe.
In general you use static_cast when you want to convert numeric data types such as enums to ints or ints to floats, and you are certain of the data types involved in the conversion. static_cast conversions are not as safe as dynamic_cast conversions, because static_cast does no run-time type check, while dynamic_cast does. Adynamic_cast to an ambiguous pointer will fail, while a static_cast returns as if nothing were wrong; this can be dangerous. Although dynamic_cast conversions are safer, dynamic_cast only works on pointers or references, and the run-time type check is an overhead. See dynamic_cast for more details.
class B {
public:
   virtual void Test(){}
};
class D : public B {};
 
void f(B* pb) {
   D* pd1 = dynamic_cast(pb);
   D* pd2 = static_cast(pb);
}

The static_cast operator can also be used to perform any implicit conversion, including standard conversions and user-defined conversions
typedef unsigned char BYTE;
 
void f() {
   char ch;
   int i = 65;
   float f = 2.5;
   double dbl;
 
   ch = static_cast(i);   // int to char
   dbl = static_cast(f);   // float to double
   i = static_cast(ch);
}

reinterpret_cast Operator

reinterpret_cast < type-id > ( expression )

The reinterpret_cast operator also allows any integral type to be converted into any pointer type and vice versa. Misuse of the reinterpret_cast operator can easily be unsafe. Unless the desired conversion is inherently low-level, you should use one of the other cast operators.
The reinterpret_cast operator can be used for conversions such as char* to int*, or One_class* to Unrelated_class*, which are inherently unsafe.
The result of a reinterpret_cast cannot safely be used for anything other than being cast back to its original type. Other uses are, at best, nonportable.
The reinterpret_cast operator cannot cast away the const, volatile, or __unaligned attributes. See const_cast Operator for information on removing these attributes.
The reinterpret_cast operator converts a null pointer value to the null pointer value of the destination type.
One practical use of reinterpret_cast is in a hash function, which maps a value to an index in such a way that two distinct values rarely end up with the same index.
#include 
 
// Returns a hash code based on an address
unsigned short Hash( void *p ) {
   unsigned int val = reinterpret_cast( p );
   return ( unsigned short )( val ^ (val >> 16));
}
 
using namespace std;
int main() {
   int a[20];
   for ( int i = 0; i < 20; i++ )
      cout << Hash( a + i ) << endl;
}
The reinterpret_cast allows the pointer to be treated as an integral type. The result is then bit-shifted and XORed with itself to produce a unique index (unique to a high degree of probability). The index is then truncated by a standard C-style cast to the return type of the function.
The C++ language provides that if a class is derived from a base class containing virtual functions, a pointer to that base class type can be used to call the implementations of the virtual functions residing in the derived class object. A class containing virtual functions is sometimes called a "polymorphic class."
Since a derived class completely contains the definitions of all the base classes from which it is derived, it is safe to cast a pointer up the class hierarchy to any of these base classes. Given a pointer to a base class, it might be safe to cast the pointer down the hierarchy. It is safe if the object being pointed to is actually of a type derived from the base class. In this case, the actual object is said to be the "complete object." The pointer to the base class is said to point to a "subobject" of the complete object. For example, consider the class hierarchy shown in the following figure.



Conditional Compilation

. They include #if, #elif, #else, #ifdef, and #ifndef. An #if or #if/#elif/#else block or a #ifdef or #ifndef block must be terminated with a closing #endif. 


#if 0
/* comment ...
*/

// code

/* comment */
#endif

Avoiding Including Files Multiple Times (idempotency)

#ifndef _FILE_NAME_H_
#define _FILE_NAME_H_

/* code */

#endif // #ifndef _FILE_NAME_H_

#ifndef

#ifndef 
/* code */
#else
/* code to include if the token is defined */
#endif

#ifndef checks whether the given token has been #defined earlier in the file or in an included file; if not, it includes the code between it and the closing #else or, if no #else is present, #endif statement. #ifndef is often used to make header files idempotent by defining a token once the file has been included and checking that the token was not set at the top of that file.

#ifndef _INCL_GUARD
#define _INCL_GUARD
#endif


#define SWAP(a, b)  {a ^= b; b ^= a; a ^= b;} 

Multiline macros

For instance, we could rewrite swap as
#define SWAP(a, b)  {                   \
                        a ^= b;         \
                        b ^= a;         \ 
                        a ^= b;         \
                    } 

What does Quarter (Q1, Q2, Q3, Q4) mean


what does it mean by (Quarter 1, Quarter 2, Quarter 3, Quarter 4)



A 3 month period for a financial year (total four quarters)will serve as a basis for reporting the profits and losses.
Most companies including all public companies report profit and loss reports on a quarterly basis. 


Q1 - January, February, and March
Q2 - April, May, and June 
Q3 - July, August, and September
Q4 - October, November, December 


Friday, October 22, 2010

How to add a user to the sudoers list

How to add a user to the sudoers list:
  1. Open a Root Terminal and type visudo (to access and edit the list)
  2. Using the up/down arrows, navigate to the bottom of the sudoers file that is now displayed in the terminal
  3. Just under the line that looks like the following:
  4. root ALL=(ALL) ALL
  5. Add the following (replacing user with your actual username):
  6. user ALL=(ALL) ALL
  7. Now press Ctrl+X and press Y when promted to save
That's it, your new user now has root privileges!
Example Sudoers Screenshot:
Adding a new sudoer

 If you have this in the /etc/sudoers
man ALL=(ALL) ALL
woman ALL=(ALL) ALL
Then man can even change woman's password and then woman can’t even login anymore.:-) cool uuhhh.

Wednesday, October 20, 2010

what is the differnce between driving and riding

When you are on top of something... like a horse,bike you take the control of the thing,your whole body moves left right up and down,backward and forward in riding.that is riding.
When you are behind the wheel of a car or some thing that is driving.
both are different,unless you are drunk.....:)

Tuesday, October 12, 2010

Free code coverage analyzer tools

gcov opensource code coverage for gcc compilers.


Use gcov in concert with GCC to analyze your programs to help create more efficient, faster running code and to discover untested parts of your program. 


How to install gcov and lcov
------------------------------
lcov- apt-get install lcov


gcov - apt-get install gcov (or) from synaptic package manager,look for ggcov.


lcov is an extension of GCOV,


gcov is a test coverage program.



Profiling tools help you analyze your code's performance. Using a profiler such as gcovor gprof, you can find out some basic performance statistics, such as:


  • * how often each line of code executes
  • * what lines of code are actually executed
  • *how much computing time each section of code uses
gcov creates a logfile called sourcefile.gcov which indicates how many times each line of a source file sourcefile.c has executed. You can use these logfiles along with gprof to aid in fine-tuning the performance of your programs. gprof gives timing information you can use along with the information you get from gcov. gcov works only on code compiled with GCC . It is not compatible with any other profiling or test coverage mechanism. For Qt projects declare in the .pro file
QMAKE_LFLAGS_DEBUG += -lgcov -fprofile-arcs -ftest-coverage
QMAKE_CXXFLAGS += -fprofile-arcs -ftest-coverage
QMAKE_LIBS     += -lgcov -Wl,-rpath
QMAKE_CLEAN    += *.gcno *.gcda
LIBS  += -lgcov