1

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
}
6
  • 1
    Not a full answer so I'm just going to put it into a comment - nullptr doesn't mean you're pointing to null, but rather that it value is a null pointer (in other words an invalid pointer or empty pointer - a pointer to nowhere in some sense). This should give a glance at an answer to your first question. A glance to your second: no. Commented Jul 4, 2015 at 3:55
  • You said it's not pointing to null but an empty pointer, isn't that the same thing? Commented Jul 4, 2015 at 4:18
  • 2
    void *x = NULL; int *y = &x; y points to NULL, but y is not a null pointer. Commented Jul 4, 2015 at 4:28
  • this makes more sens thank you!! Commented Jul 4, 2015 at 4:32
  • That little example looks like it won't compile. Commented Jul 4, 2015 at 5:02

5 Answers 5

3

Since both are pointing to the same object, if either obj or d_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:

  1. Directly by assigning a value to i.

     i = 15;
    
  2. Indirectly by assigning a value to where ptr1 points to.

     *ptr1 = 15;
    
  3. 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 as delete?

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.

Sign up to request clarification or add additional context in comments.

Comments

2

Q: "Isn't obj also pointing to null, since both object were pointing to the same object?"

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

Q. "Is 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.

2 Comments

thanks but your examples are not quite relevant . d_heater is also a pointer not a simple integer.
@Bobby Those are just to help you understand the concept. Although C++ treats pointers differently than other primitives, at it's core any pointer is just an integer. For me it is helpful to think about pointers like simple integers in certain situations, I was hoping this would be helpful to you as well. Either way I'd certainly look at the example I typed up: ideone.com/I8CTba
1

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.

Comments

0

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.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.