Security implications for data members of classes in C + +

Source: Internet
Author: User
Tags modify

In any book on the C + + programming language, there are descriptions similar to the following:

In a class, C + + uses three keywords to set access limits: Public, private, and protected. They determine the use of identifiers following them: Public means that subsequent identifiers can be referenced by the user-defined fact, while private indicates that the following identifier, in addition to the member function of the class, cannot be referenced by the user-defined example Protected provides an interface to the inheritance of a class while protecting it from external access.

In fact, this is true if you want to use an object (or instance) of a class to visit its members. However, in C + +, the user is given a "cross protection Barrier" method by allowing arbitrary conversion of pointer types. This can be seen from the following routines:

#include
class cmyclass{
Double D;
int x,y;
const char ch;   
Public:
int z;
CMyClass (int xx,int yy,char c): Ch (c) {x=xx,y=yy;d=9.8759;z=0;}
Void Show () {
cout<< "d=" <<d<< "<<" x= "<<x<<" "<<" y= "<<y< < "" << "ch=" <<ch<< "";  
cout<< "z=" <<z<<endl; }
};   
Void Main ()
{
cout<<sizeof (cmyclass) <<endl
CMyClass p (1,2, ' U ');
P.show ();
CMyClass *ptr=&p;
//ptr->x=9.32145;//error, because X is its private member and cannot be accessed directly from outside.
double*dp= (double*) ptr;//Get P.D address
*dp=9.32145//Modify P.D value
int*ip= (int*) (dp+1);//Get p.x address
*ip=3 00;   Modify the value of the p.x
* (ip+1) = 200;//Modify the value of P.Y
* (ip+2) = 65;//Modify the value of CH to change to ' A ', but CH is constant!
 * (ip+3) = 100; Modify the value of the P.z
P.show ();
} The
running results are as follows:
**********************************************

d=9.8759 x=1 y=2 ch=u z=0
d=9.32145 x=300 y=200 ch=a z=100
* * ********************************************

The class CMyClass has four private members double D, int x,y and const char CH, whose value is definitely not viewed or modified by its object p, but we did so using the pointer to the object p. The value of D is modified using the convert PTR to double* DP, and then the DP is converted to INT*IP to modify the values of all remaining data members. It is also shown from the above results that char occupies the same amount of memory as int in a class because of the alignment (edge adjustment) of the class. A worse thing is that the value of CH has also been modified! It's a const!!. Pointer is a god! it's omnipotent!!

The "unsafe" nature of the pointer is fully demonstrated in this example and should be limited!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.