// pointer to classes example
#include <iostream>
usingnamespace std;
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area (void) {return (width * height);}
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
int main () {
CRectangle a, *b, *c;
CRectangle * d = new CRectangle[2];
b= new CRectangle;
c= &a;
a.set_values (1,2);
b->set_values (3,4);
d->set_values (5,6);
d[1].set_values (7,8);
cout << "a area: " << a.area() << endl;
cout << "*b area: " << b->area() << endl;
cout << "*c area: " << c->area() << endl;
cout << "d[0] area: " << d[0].area() << endl;
cout << "d[1] area: " << d[1].area() << endl;
delete[] d;
delete b;
return 0;
}
Everything was clear to me until I started to play with this code. I noticed, that if I change the line 19 to
CRectangle * d = new CRectangle; //instead of CRectangle * d = new CRectangle[2];
Nothing changes... I could create as many objects d (array of objects, I guess) as I want to (like d[0]...d[n]) and still have the correct output even though pointer d to the class CRectangle was dynamically allocated for only one object (not the array of objects)....
Could somebody please explain why is that happening?
A pointer is simply a variable that stores a memory address. Your pointer d can store the address of a single CRectangle that's been allocated. It can store the address of the first CRectangle in an array of objects. It can store a memory address that you haven't put any data in, if you choose.
It's up to you, as a programmer, to make sure you give your pointers sensible values, and only do sensible manipulations with them.
Edit: And writing beyond the end of memory you haven't allocated, results in undefined behaviour. It could corrupt other data. It could cause your software to crash.
If you're really unlucky, it could still work as if you'd allocated the memory - which seems to be what's happening to you.
Yes,
that was really surprising for me.... I didn't expect that it's going to work, but when I tried, it worked such way even with d[0]...d[4] (I didn't went further).
So, I have one pointer d for a single CRectangle, but in works like it's a pointer for an array at my machine...
Seems like the lesson for me here: even if everything seems to work fine it doesn't mean that my program is written properly ;)
So, I have one pointer d for a single CRectangle, but in works like it's a pointer for an array at my machine...
No. It works like a pointer to a CRectangle. Because, regardless of how many elements you allocate memory for in line 19, d is still just a pointer to a CRectangle. C++ allows you to use the semantics of an array with any pointer, as an alternative to pointer arithmetic, which is why lines 20, 29 and 30 are syntactically correct, regardless of how much memory you've allocated.
A pointer to an array would be something different, depending on what sort of array you're using. If you're using C-style arrays (as you do in the code you've posted), then it would be: