#include <iostream>
#include <string>
using namespace std;
class CExample {
public:
int a,b,c;
void setvalue(int n, int m);
void multiply() {
c = a*b;
cout<< c <<endl;
};
};
void CExample::setvalue(int n, int m){
a = n; b = m;
}
int main( )
{
CExample *d = new CExample[1];
d[0].setvalue(2,30);
d[0].multiply();
d[1].setvalue(2,30);
d[1].multiply();
d[2].setvalue(2,30);
d[2].multiply();
delete[] d;
return 0;
}
Since I have only allocate 1 Memory resource for Array d, I ought to encounter error when manipulating d[2]. However, the compiler didn't report error or warning. And the result is correct.
Can someone explain why it works?
Thanks
Yes, it is the magic of undefined behavior. Thanks for all of you.