I am learning C++ and I have trouble understanding certain behavior related to pointer.
I have an easy custom object looking like this:
class Human {
public:
//constructor here
private:
std::string name;
std::string address;
}
another custom object that has an instance variable type of a pointer to human:
class property {
public:
Human * ppl;//pointer to the above human object
}
and running this piece of code:
Human * human = new Human("Mark", "address");// pointer address 0x123456 for example
Property * property = new Property();
property->ppl = human;//pass the pointer to property's instance variable
delete human;
human = NULL;//after deleting and setting it to NULL property.ppl still points to 0x123456
After running the above code, the ppl instance variable under property still points to the original memory address but the content of it(name and address) are all cleared(empty string now), I am wondering why is this the behavior? Since I passed the raw pointer around, why after deleting it and setting it to NULL, it still points to a memory address but the content of it has been cleared?
FYI: I am using XCode as the debugging IDE and C++11
Look at this simpler example:
int a = 5;
int b = a;
b = 45;
Setting the value of b
does not change the value of a
. In the same way, setting the value of human
in your code does not change the value of property->ppl
.
This concept has nothing to do with delete
. However, in the case of a pointer, you have to be very careful because property.ppl
no longer points to valid memory. You can access it, but will likely cause a segmentation fault or some other error that is very difficult to debug.
Why does my pointer still point to an address after deleting it?
You are mistaken. The pointer has an invalid value. It does not point to the object it used to point, nor to any other object.
I am wondering why is this the behavior?
The behaviour is the way it is because that's how the language is specified. When an object is destroyed and its memory deallocated, all pointers (and references) that used to point to that object become invalid.
why after deleting it and setting it to NULL, it still points ...
You set human
to null. You never set property->ppl
to null. Those are separate objects (even though they have the same value - ie they point to the same object) and modifying one doesn't affect the other. An anologous situation:
int a = 1;
int b = a;
a = 2; // this does not modify b
The two objects one of the type Human and other of the type Property
Human * human = new Human("Mark", "address");
Property * property = new Property();
occupy different extents of memory allocated for these objects.
In this statement
human = NULL;
there is being changed the memory extent occupied by the object human
.
The memory occupied by the object property
was not changed.
If the class Property would be defined the following way
class Property {
public:
Human * &ppl;//pointer to the above human object
};
and an object of the class Property would be created like
Property * property = new Property { human };
then indeed setting the object human
to NULL
or nullptr
influence on the data member ppl
because it has a referenced type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.