Reputation: 496
I have this code below and I think there is something that I don't understand.d_header
is a pointer of type WaterHeater
and a variable of the class house
.
Line 2 creates a pointer that points to a d_heater
object. Since both are pointing to the same object, if either obj
or d_header
is changed, the change will be reflected in the other. Line 3 assigns nullptr
to d_header
and line 4 returns obj
. First question: Isn't obj
also pointing to null, since both object were pointing to the same object? So what is the deal of returning a Null pointer? Second question: is nullptr
in this case would be the same as delete
?
Thanks for your time.
WaterHeater* house::removeWaterHeater(){ //Line 1
WaterHeater *obj = d_heater; //Line 2
d_heater = nullptr; //Line 3
return obj; //Line 4
}
Upvotes: 1
Views: 1697
Reputation: 14313
You're confusing the ideas of two pointers pointing to the same object and two pointers being the same object. When you write:
WaterHeater *obj = d_heater;
d_heater = nullptr;
You're assigning the value of d_heater
to obj, but they're still separate variables. It's just like writing:
int x = 7;
int y = x;
x = 8;
y
is still 7 of course because while x
and y
have the same value they are not the same object.
So d_heater
and obj
are two, distinct entities that happen to have the same the value, and assignments made to one will not be reflected in the other. However, *d_heater
and *obj
are the same object, and if we assign to one of them, it will be reflected in the other. This is where you're getting confused.
To answer your second question, nullptr
is never the same as delete
. delete
doesn't change a pointer's value: it frees/deconstructs the object that the pointer points to. Assigning nullptr
or NULL
to a pointer doesn't affect the object it points: it just reassigns the pointer not to point to an object.
Upvotes: 1
Reputation: 438
obj isn't point to null
. it just point, as you mentioned, to a null
pointer (d_header
).
And it doesn't as the same as delete
, you have just made it point to nothing(like empty).
Upvotes: 0
Reputation: 38919
No, both obj
and d_heater
contain addresses in memory. Changing one does not change the other. This is easy to see if you think about non-pointer variables:
int foo = 13;
int bar = foo;
foo = 42;
Obviously we know that bar
still holds 13
, and in the same way obj
still holds the original address.
If you want obj
to keep the same value as d_heater
you can make it a reference, then obj
and d_heater
are the same variable they don't simply share the same value. We can see this again looking at non-pointer variables, but this time let's make bar
a reference:
int foo = 13;
int& bar = foo;
foo = 42;
Now both foo
and bar
will equal 42. If you want to accomplish the same thing with obj
make it a pointer reference:
WaterHeater*& obj = d_heater;
You can see a more detailed example here: http://ideone.com/I8CTba
nullptr
in this case would be the same as delete
?"No, again d_heater
is only an address. If you assign a new value to d_heater
it is simply referencing a different point in memory. If by reassigning d_heater
you lose the address of dynamically allocated memory that's very bad, it's known as a memory leak. To prevent leaking you must always release dynamically allocated memory before losing the last address to your dynamically allocated memory (call delete
for memory allocated with new
.)
That said, use of dynamic memory allocation is best left to the standard libraries, unless you really know what you're doing. So I'd strongly recommend you look at using auto-pointers. In C++11 those are unique_ptr
and shared_ptr
.
Upvotes: 2
Reputation: 206567
Since both are pointing to the same object, if either
obj
ord_header
is changed, the change will be reflected in the other.
That is incorrect. If the contents of what they point to is changed, that change can be seen through either pointer but if one of them is changed so that it points to something different, the other will still point to the previous object.
Simple example:
int i = 10;
int j = 20;
int* ptr1 = &i;
int* ptr2 = ptr1;
At this point, both the pointers point to the same object, i
. Value of i
can be changed by:
Directly by assigning a value to i
.
i = 15;
Indirectly by assigning a value to where ptr1
points to.
*ptr1 = 15;
Indirectly by assigning a value to where ptr2
points to.
*ptr2 = 15;
However, you can change where ptr1
points to by using:
ptr1 = &j;
Now, ptr1
points to j
but ptr2
still points to i
.
Any changes made to i
will be visible through ptr2
but not ptr1
.
Any changes made to j
will be visible through ptr1
but not ptr2
.
Isn't
obj
also pointing to null, since both object were pointing to the same object?
The answer should be clear now. obj
continues to point to what d_header
used to point to. It is not NULL.
So what is the deal of returning a Null pointer?
The function does not necessarily return a NULL pointer. The function returns whatever d_header
used to point to before it was changed to be nullptr
. It could be a NULL pointer if d_header
used to be NULL before the call to the function.
is
nullptr
in this case would be the same asdelete
?
No, it is not. There are two different operations. Assigning a pointer to nullptr
does not automatically mean that delete
gets called on the pointer. If you need to deallocate memory that a pointer points to, you'll have to call to call delete
explicitly.
Upvotes: 3
Reputation: 422
nullptr
is not pointing to the NULL
pointer. Check this for reference.
If you are doing this:
WaterHeater* house::removeWaterHeater(){ //Line 1
WaterHeater *obj = d_heater; //Line 2
d_heater = NULL; //Line 3
return obj; //Line 4
}
Now, obj
is also pointing to a NULL
.
And nullptr
does not mean delete
.
Upvotes: 0