[英]C++ return null pointer
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
. d_header
是WaterHeater
类型的指针,是house
类的变量。
Line 2 creates a pointer that points to a d_heater
object. 第2行创建一个指向
d_heater
对象的指针。 Since both are pointing to the same object, if either obj
or d_header
is changed, the change will be reflected in the other. 由于两者都指向同一个对象,因此,如果
obj
或d_header
被更改,则更改将反映在另一个对象中。 Line 3 assigns nullptr
to d_header
and line 4 returns obj
. 第3行向
d_header
分配nullptr
,第4行返回obj
。 First question: Isn't obj
also pointing to null, since both object were pointing to the same object? 第一个问题:因为两个对象都指向同一个对象,所以
obj
也不也指向null吗? So what is the deal of returning a Null pointer? 那么返回Null指针有什么意义呢? Second question: is
nullptr
in this case would be the same as delete
? 第二个问题:在这种情况下
nullptr
是否与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
}
Since both are pointing to the same object, if either
obj
ord_header
is changed, the change will be reflected in the other.由于两者都指向同一个对象,因此,如果
obj
或d_header
被更改,则更改将反映在另一个对象中。
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
. 此时,两个指针都指向同一个对象
i
。 Value of i
can be changed by: i
值可以通过以下方式更改:
Directly by assigning a value to i
. 直接给
i
赋值。
i = 15;
Indirectly by assigning a value to where ptr1
points to. 通过为
ptr1
指向的位置分配一个间接值。
*ptr1 = 15;
Indirectly by assigning a value to where ptr2
points to. 通过为
ptr2
指向的位置分配一个间接值。
*ptr2 = 15;
However, you can change where ptr1
points to by using: 但是,您可以使用以下方法更改
ptr1
指向的位置:
ptr1 = &j;
Now, ptr1
points to j
but ptr2
still points to i
. 现在,
ptr1
指向j
而ptr2
仍然指向i
。
Any changes made to i
will be visible through ptr2
but not ptr1
. 对
i
所做的任何更改都可以通过ptr2
看到,但不能通过ptr1
看到。
Any changes made to j
will be visible through ptr1
but not ptr2
. 对
j
所做的任何更改都可以通过ptr1
看到,但不能通过ptr2
看到。
Isn't
obj
also pointing to null, since both object were pointing to the same object?因为两个对象都指向同一个对象,所以
obj
也不也指向null吗?
The answer should be clear now. 答案现在应该清楚了。
obj
continues to point to what d_header
used to point to. obj
继续指向d_header
所指向的对象。 It is not NULL. 它不是NULL。
So what is the deal of returning a Null pointer?
那么返回Null指针有什么意义呢?
The function does not necessarily return a NULL pointer. 该函数不一定返回NULL指针。 The function returns whatever
d_header
used to point to before it was changed to be nullptr
. 该函数返回
d_header
所指向的所有内容,然后将其更改为nullptr
。 It could be a NULL pointer if d_header
used to be NULL before the call to the function. 如果在调用函数之前
d_header
曾经为NULL,则它可能是NULL指针。
is
nullptr
in this case would be the same asdelete
?nullptr
在这种情况下是否与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. 将指针分配给
nullptr
并不自动意味着在指针上调用了delete
。 If you need to deallocate memory that a pointer points to, you'll have to call to call delete
explicitly. 如果需要释放指针所指向的内存,则必须调用显式调用
delete
。
No, both obj
and d_heater
contain addresses in memory. 不,
obj
和d_heater
都包含内存中的地址。 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. 显然,我们知道
bar
仍然保留13
,而obj
仍然保留原始地址。
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 . 如果您希望
obj
保持与d_heater
相同的值, d_heater
可以将其作为引用,然后obj
和d_heater
是相同的变量,它们不只是共享相同的值 。 We can see this again looking at non-pointer variables, but this time let's make bar
a reference: 我们可以再次查看非指针变量,但是这次让我们将
bar
作为参考:
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: 现在,
foo
和bar
都等于42。如果要使用obj
完成相同的操作,请使其成为指针引用:
WaterHeater*& obj = d_heater;
You can see a more detailed example here: http://ideone.com/I8CTba 您可以在此处查看更详细的示例: http : //ideone.com/I8CTba
nullptr
in this case would be the same as delete
?" nullptr
在这种情况下是否与delete
相同?” No, again d_heater
is only an address . 不,再次
d_heater
只是一个地址 。 If you assign a new value to d_heater
it is simply referencing a different point in memory. 如果将新值分配给
d_heater
,则仅引用内存中的另一个点。 If by reassigning d_heater
you lose the address of dynamically allocated memory that's very bad, it's known as a memory leak . 如果通过重新分配
d_heater
丢失了非常糟糕的动态分配内存的地址,则称为内存泄漏 。 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
.) 为了防止泄漏,您必须始终释放动态分配的内存,然后再将最后一个地址丢失到动态分配的内存中(对于带有
new
分配的内存,请调用delete
。)
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
. 在C ++ 11中,这些是
unique_ptr
和shared_ptr
。
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. 您正在将
d_heater
的值分配给obj,但它们仍然是单独的变量。 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. y
当然仍然是7,因为尽管x
和y
具有相同的值,但它们不是同一对象。
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. 因此
d_heater
和obj
是两个不同的实体,碰巧具有相同的值,并且对一个实体的赋值不会反映在另一个实体中。 However, *d_heater
and *obj
are the same object , and if we assign to one of them, it will be reflected in the other. 但是,
*d_heater
和*obj
是同一对象 ,如果我们将它们分配给其中一个,它将反映在另一个对象中。 This is where you're getting confused. 这就是您感到困惑的地方。
To answer your second question, nullptr
is never the same as delete
. 要回答第二个问题,
nullptr
永远与delete
相同。 delete
doesn't change a pointer's value: it frees/deconstructs the object that the pointer points to . delete
不会更改指针的值:它会释放/解构指针所指向的对象。 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. 为指针分配
nullptr
或NULL
不会影响它指向的对象:它只是重新分配了指针,使其不指向对象。
obj isn't point to null
. obj不是指向
null
。 it just point, as you mentioned, to a null
pointer ( d_header
). 正如您提到的,它只是指向
null
指针( d_header
)。 And it doesn't as the same as delete
, you have just made it point to nothing(like empty). 而且它与
delete
,您只是使其指向无内容(如空)。
nullptr
is not pointing to the NULL
pointer. nullptr
没有指向NULL
指针。 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
. 现在,
obj
也指向NULL
。
And nullptr
does not mean delete
. 而且
nullptr
并不意味着delete
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.