I compiled & ran the code pasted below and surprisingly it worked without errors. (g++/linux)
How can a deleted object have some members still available ? Is it a normal behaviour ?
#include <iostream>
using namespace std;
class chair {
public:
int height;
int x;
int y;
chair() {
before = last;
if(last!=NULL)
last->after = this;
else
first = this;
last = this;
after = NULL;
}
~chair() {
if(before != NULL)
before->after = after;
else
first = after;
if(after != NULL)
after->before = before;
else
last = before;
}
chair* before;
chair* after;
static chair* first;
static chair* last;
};
chair* chair::first;
chair* chair::last;
int main() {
chair *room = NULL;
int tempx = 0;
int tempy = 1;
while(tempx<=3) {
tempy = 1;
while(tempy<=3) {
room = new chair();
room->x = tempx;
room->y = tempy;
tempy++;
}
tempx++;
}
room = chair::first;
while(room!=NULL) {
cout << room->x << "," << room->y << endl;
delete room;
room = room->after;
}
}
What you are doing is undefined behavior you are accessing a deleted object. The data you are looking at is still available and the area of memory where that information is stored hasn’t been overridden yet but nothing is stopping that from happening.