Showing posts with label Tech. Show all posts
Showing posts with label Tech. Show all posts

Friday, November 13, 2009

Accessing the Private Member outside the Class (without Friend)

The following article discusses the dangers of returning a reference to private data of a class.

First, it is shown how a reference to private data member is returned from a member function and then the consequences of doing so are discussed.

Returning a Reference to Private Data in a Class :

The example below shows how a reference is returned to a private data member in a class :

class ATestClass
{
private:
int x ; // these are private
char y; // data members of the class

public:
AtestClass () // default constructor of class
{
--------------------------------
---body of constructor----
--------------------------------
}

int &retReference() // this is the bad function that returns reference to private member x
{
x = 10;
return x ; // dangerous !! (reference to private data x is returned)
}
};

The catch in the above code snippet is that the function retReference() can be used as an lvalue in any assignment statement. The code snippet below shows how :

int main()
{
ATestClass testObject; // creates an object of ATestClass
testObject.retReference() ; // sets the value of x in testObject as 10
int z = testObject.retReference() ; // z becomes 10
testObject.retReference() = 20 ; // dangerous !! the private data member of ATestClass is directly accessed and modified !
int &a = testObject.retReference() ; //again dangerous !! a becomes a reference to private data x in object testObject
a = 100 ; // dangerous !! private data member x in object testObject

return 0 ;

}

In the above code snippet, the statement

testObject.retReference() = 20 ;
is a perfectly valid C++ statement because the function retReference() returns a reference to int x, and hence it can be used as an lvalue in any assignment statement. Thus, even if data member x is private in class ATestClass, it is directly accessed through object testObject and is modified.

Similarly, the statement

int &a = testObject.retReference();
declares a as a reference to private data member x in testObject. Then, the following statement
a = 100 ;
uses a as an alias (reference) to private data x and modifies its value.

This breaks-down the data hiding and encapsulation features of a class and defeats the purpose of Object OrientedProgramming in the following ways :

  • The private data in the class no longer remain “private” as they can be directly accessed through any variable that is a reference to the private data.
  • The encapsulation feature is also destroyed as data and its functions are no longer bound together. Data can be accessed without the help of the member functions of the class.

Thus, we have seen how a simple thing such as returning references to private data members of a class destroy Object Oriented Programming concepts. This is an example of a poor programming practice, and should be avoided.

Friend Function & Class

Any data which is declared private inside a class is not accessible from outside the class. A function which is not a member or an external class can never access such private data. But there may be some cases, where a programmer will need need access to the private data from non-member functions and external classes. C++ offers some exceptions in such cases.

A class can allow non-member functions and other classes to access its own private data, by making them as friends. This part of C++ tutorial essentially gives two important points.

  • Once a non-member function is declared as a friend, it can access the private data of the class
  • similarly when a class is declared as a friend, the friend class can have access to the private data of the class which made this a friend
Let's see a sample in this C++ tutorial for each of the above cases.

C++ Tutorial - Friend function sample:

#include
//Declaration of the function to be made as friend for the C++ Tutorial sample
int AddToFriend(int x);
class CPP_Tutorial
{
int private_data;
friend int AddToFriend(int x);
public:
CPP_Tutorial()
{
private_data = 5;
}
};
int AddToFriend(int x)
{
CPP_Tutorial var1;
return var1.private_data + x;
}
int main()
{
cout << "Added Result for this C++ tutorial: "<< AddToFriend(4)< }

The output of the above C++ Tutorial sample will be
Added Result for this C++ tutorial: 9

C++ tutorial - friend class:

Declaration of a friend class is also similar. Only thing is a class definition is slightly different.

C++ Tutorial - Friend function:

#include <>
class CPP_Tutorial
{
int private_data;
friend class friendclass;
public:
CPP_Tutorial()
{
private_data = 5;
}
};
class friendclass
{
public:
int subtractfrom(int x)
{
CPP_Tutorial var2;
return var2.private_data - x;
}
};
int main()
{
friendclass var3;
cout << "Added Result for this C++ tutorial: "<<>
}

The output of the above C++ Tutorial sample will be
Subtracted Result for this C++ tutorial: 3

This is a good way out given by C++ to avoid restrictions on private variables. But this should be used with caution though. If all the functions and classes are declared as friends, then the concept of encapsulation and data security will go for a toss.

That is why the concept of friend functions and classes should be used with proper judgment.

Static Functions

Static data types can be accessed without instantiation of the class in C++. This is applicable for static functions also.

The differences between a static member function and non-static member functions are as follows.
  • A static member function can access only static member data, static member functions and data and functions outside the class. A non-static member function can access all of the above including the static data member.
  • A static member function can be called, even when a class is not instantiated, a non-static member function can be called only after instantiating the class as an object.
  • A static member function cannot be declared virtual, whereas a non-static member functions can be declared as virtual
  • A static member function cannot have access to the 'this' pointer of the class.
The static member functions are not used very frequently in programs. But nevertheless, they become useful whenever we need to have functions which are accessible even when the class is not instantiated.

Copy constructor

Copy constructor is
  • a constructor function with the same name as the class
  • used to make deep copy of objects.

There are 3 important places where a copy constructor is called.

  1. When an object is created from another object of the same type
  2. When an object is passed by value as a parameter to a function
  3. When an object is returned from a function

If a copy constructor is not defined in a class, the compiler itself defines one. This will ensure a shallow copy. If the class does not have pointer variables with dynamically allocated memory, then one need not worry about defining a copy constructor. It can be left to the compiler's discretion.

But if the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor.


For ex:
class A //Without copy constructor
{
private:
int x;
public:
A() {A = 10;}
~A() {}
}

class B //With copy constructor
{
private:
char *name;
public:
B()
{
name = new char[20];
}
~B()
{
delete name[];
}
//Copy constructor
B(const B &b)
{
name = new char[20];
strcpy(name, b.name);
}
};

Let us Imagine if you don't have a copy constructor for the class B. At the first place, if an object is created from some existing object, we cannot be sure that the memory is allocated. Also, if the memory is deleted in destructor, the delete operator might be called twice for the same memory location.

This is a major risk. One happy thing is, if the class is not so complex this will come to the fore during development itself. But if the class is very complicated, then these kind of errors will be difficult to track.

In Windows this will lead to an application popup and unix will issue a core dump. A careful handling of this will avoid a lot of nuisance

Virtual Functions

What is a Virtual Function?

A virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. It is one that is declared as virtual in the base class using the virtual keyword. The virtual nature is inherited in the subsequent derived classes and the virtual keyword need not be re-stated there. The whole function body can be replaced with a new set of implementation in the derived class.

What is Binding?

Binding refers to the act of associating an object or a class with its member. If we can call a method fn() on an object o of a class c, we say that the object o is binded with the method fn(). This happens at compile time and is known as static or compile - time binding. The calls to the virtual member functions are resolved during run-time. This mechanism is known as dynamic binding. The most prominent reason why a virtual function will be used is to have a different functionality in the derived class. The difference between a non-virtual member function and a virtual member function is, the non-virtual member functions are resolved at compile time.

How does a Virtual Function work?

Whenever a program has a virtual function declared, a v - table is constructed for the class. The v-table consists of addresses to the virtual functions for classes that contain one or more virtual functions. The object of the class containing the virtual function contains a virtual pointer that points to the base address of the virtual table in memory. Whenever there is a virtual function call, the v-table is used to resolve to the function address. An object of the class that contains one or more virtual functions contains a virtual pointer called the vptr at the very beginning of the object in the memory. Hence the size of the object in this case increases by the size of the pointer. This vptr contains the base address of the virtual table in memory. Note that virtual tables are class specific, i.e., there is only one virtual table for a class irrespective of the number of virtual functions it contains. This virtual table in turn contains the base addresses of one or more virtual functions of the class. At the time when a virtual function is called on an object, the vptr of that object provides the base address of the virtual table for that class in memory. This table is used to resolve the function call as it contains the addresses of all the virtual functions of that class. This is how dynamic binding is resolved during a virtual function call.

The following code shows how we can write a virtual function in C++ and then use the same to achieve dynamic or runtime polymorphism.

#include
class base
{
public:
virtual void display()
{
cout<<”\nBase”;
}
};
class derived : public base
{
public:
void display()
{
cout<<”\nDerived”;
}
};

void main()
{

base *ptr = new derived();
ptr->display();
}

In the above example, the pointer is of type base but it points to the derived class object. The method display() is virtual in nature. Hence in order to resolve the virtual method call, the context of the pointer is considered, i.e., the display method of the derived class is called and not that of the base. If the method was non virtual in nature, the display() method of the base class would have been called.

Virtual Constructors and Destructors

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.

A virtual destructor is one that is declared as virtual in the base class and is used to ensure that destructors are called in the proper order. It is to be remembered that destructors are called in the reverse order of inheritance. If a base class pointer points to a derived class object and we some time later use the delete operator to delete the object, then the derived class destructor is not called. Refer to the code that follows:

#include
class base
{
public:
~base()
{

}
};

class derived : public base
{
public:
~derived()
{

}
};

void main()
{

base *ptr = new derived();
// some code
delete ptr;
}

In this case the type of the pointer would be considered. Hence as the pointer is of type base, the base class destructor would be called but the derived class destructor would not be called at all. The result is memory leak. In order to avoid this, we have to make the destructor virtual in the base class. This is shown in the example below:

#include
class base
{
public:
virtual ~base()
{

}
};

class derived : public base
{
public:
~derived()
{

}

};

void main()
{

base *ptr = new derived();
// some code
delete ptr;
}

Conclusion:

Virtual methods should be used judiciously as they are slow due to the overhead involved in searching the virtual table. They also increase the size of an object of a class by the size of a pointer. The size of a pointer depends on the size of an integer. Note that in DOS based systems the size of a pointer is 2 bytes whereas in UNIX based systems it is 4 bytes.

Thursday, July 23, 2009

Focus on Android !!!

We keep hearing that a lot of OEMs are working on Android smartphones, even though only a few have made it to market. There is an explosion of Android phones set to appear this year and early next year. Phone maker HTC is one of the biggest players in the smartphone world, and the largest maker of Windows Mobil phones around. It produces some of the most popular phones on the market, the Touch and Touch Pro among them.



HTC was the first on the Android train, producing the original T-Mobile G1 and the recent myTouch 3G for the carrier. The company has made it clear that it believes Android is a large part of their future phone development, and a rumor coming out of Asia indicates how large a role Android might play. According to DigiTimes, HTC intends to make Android the platform of choice on over 50 percent of its future phones. This fits in with what we see from HTC, and if true this is a big blow to Windows Mobile, the platform on the major portion of HTC’s lineup for some time.

info@simplyknow.info

Saturday, June 27, 2009

USB Ports: Enable / Disable in WinXP




USB keys are not just a popular way to sneak data out from companies, unhappy employees may use USB ports for delivering trojans or spyware into the company networks.

Now some smart admins disable usb drive by changing the BIOS settings and then lock the BIOS using passwords. Some not so-smart admins fix tapes over the USB ports to prevent employees from inserting any USB device into their computer.

However, both these approaches can prove to be counter-productives as your staff can no longer use USB keyboards, wireless mouse, digital cameras, camcorders, scanners, printers or even USB microphones to their computers.

So a more reasonable option for sysadmins is to disable write access to USB port so that data files cannot be written to the mass storage device. The USB thumb drive will be read-only.

Open the Windows Registry and open the following key
HKEY_LOCAL_MACHINE\System\CurrentControlSet\ Control\StorageDevicePolicies

Now add a new DWORD called WriteProtect and put the value as 0 to disable write privileges to the USB port. To reverse the step, either delete the WriteProtect REG_DWORD or toggle the value to 1 which will enable the port.

Remember that the above trick works only with Windows XP SP2.

If you like to go a step further and disable users from connecting USB storage devices to their computers, here's the trick:

Open registry and navigate to the following registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet \Services\UsbStor

Now in the right pane, double-click Start and type 4 in the Value data box (Hexadecimal) and quite the registry editor. To enable the USB storage devices, change the Start value back to 3.

No matter how good the protection tricks are, determined people always find workarounds. Here are some of the tricks that may render the above methods unusable:

» Employee may boot computer using a LiveCD like Knoppix or Ubuntu so the USB drives are again available to him for writing.

» They could open the computer chasis, take the battery out to reset the BIOS settings.

» Some may even invest in a PS2 to USB port converter.

» If he manages to get admin access for a temporary period (like installing software), he may undo the registry edits.

The cat-mouse game will never end. USB drives will remain a headache for the sysadmins for some time. However, Windows Vista will make life much simpler for IT administrators. There's a new Policy in Vista that allows USB keyboards or mouse to be used but not any USB devices.

info@simplyknow.info

Wednesday, June 17, 2009

Disposable E-mail Accounts

List of some of known websites where you can get an e-mail account both quickly and for free. It will prevent your email account from spam.

Where can we use disposable e-mail address?
Maybe we want to sign up for a site which requires that we provide an e-mail address to send a validation e-mail to. And maybe we don’t want to give up our real e-mail address and end up on a bunch of spam lists.

mail us: info@simplyknow.info