z****e 发帖数: 2024 | 1 类似这个:
int* pi;
tr1::shared_ptr spi=tr1::shared_ptr(pi);
这样做,会有什么问题么? |
h*****0 发帖数: 4889 | 2 shared_ptr针对的是指针指向的对象,不是指针本身。
【在 z****e 的大作中提到】 : 类似这个: : int* pi; : tr1::shared_ptr spi=tr1::shared_ptr(pi); : 这样做,会有什么问题么?
|
d****p 发帖数: 685 | 3 pi is not initialized to a pointer to variable allocated on heap. and it is
even not initialized to 0.
so either
....boost::shared_ptr spi(new int 1)
or
... boost::shared_ptr spi // initialized to 0
Normally it is a bad form to write spi() since the pointer
could be either (1) a pointer to local variable or (2) a pointer not "fresh"
- shared_ptr's shared_count object exclusively wraps a raw pointer to heap.
So the following code will terribly fail:
int* myIntPtr = new int
【在 z****e 的大作中提到】 : 类似这个: : int* pi; : tr1::shared_ptr spi=tr1::shared_ptr(pi); : 这样做,会有什么问题么?
|
z****e 发帖数: 2024 | 4 多谢。
我也是这么想的,
也许tr1 和 boost 实现有区别?
怎么我上边那两行也没报错,也没有运行错误呢?
我也试了这个:
int* pi=new int();
tr1::shared_ptr spi1=tr1::shared_ptr(pi);
tr1::shared_ptr spi2=tr1::shared_ptr(pi);
好像没事。
is
fresh"
heap.
【在 d****p 的大作中提到】 : pi is not initialized to a pointer to variable allocated on heap. and it is : even not initialized to 0. : so either : ....boost::shared_ptr spi(new int 1) : or : ... boost::shared_ptr spi // initialized to 0 : Normally it is a bad form to write spi() since the pointer : could be either (1) a pointer to local variable or (2) a pointer not "fresh" : - shared_ptr's shared_count object exclusively wraps a raw pointer to heap. : So the following code will terribly fail:
|
h*****0 发帖数: 4889 | 5 UB……
你直接试:
int* p = new int();
int* q = p;
delete p;
delete q;
应该也没事
【在 z****e 的大作中提到】 : 多谢。 : 我也是这么想的, : 也许tr1 和 boost 实现有区别? : 怎么我上边那两行也没报错,也没有运行错误呢? : 我也试了这个: : int* pi=new int(); : tr1::shared_ptr spi1=tr1::shared_ptr(pi); : tr1::shared_ptr spi2=tr1::shared_ptr(pi); : 好像没事。 :
|
N***m 发帖数: 4460 | 6 report error on ubuntu
【在 h*****0 的大作中提到】 : UB…… : 你直接试: : int* p = new int(); : int* q = p; : delete p; : delete q; : 应该也没事
|
z****e 发帖数: 2024 | 7 那个肯定是错的。
【在 N***m 的大作中提到】 : report error on ubuntu
|
z****e 发帖数: 2024 | 8 我又试了一下,发现shared_ptr应该只能用在heap,和new,delete应该还是一样的。
你那个例子应该也是如你所说,会运行错误的。
多谢。
is
fresh"
heap.
【在 d****p 的大作中提到】 : pi is not initialized to a pointer to variable allocated on heap. and it is : even not initialized to 0. : so either : ....boost::shared_ptr spi(new int 1) : or : ... boost::shared_ptr spi // initialized to 0 : Normally it is a bad form to write spi() since the pointer : could be either (1) a pointer to local variable or (2) a pointer not "fresh" : - shared_ptr's shared_count object exclusively wraps a raw pointer to heap. : So the following code will terribly fail:
|
d****p 发帖数: 685 | 9 What you said is almost 100% true except one case:
shared pointer can wrap pointer pointing to local variable in its scope if
you explicitly provide a null deleter
when constructing the shared pointer.
【在 z****e 的大作中提到】 : 我又试了一下,发现shared_ptr应该只能用在heap,和new,delete应该还是一样的。 : 你那个例子应该也是如你所说,会运行错误的。 : 多谢。 : : is : fresh" : heap.
|
z****e 发帖数: 2024 | 10 你说太对了,我都快忘了这个事了。
多谢提醒,这点是和auto_ptr另一巨大区别啊!
【在 d****p 的大作中提到】 : What you said is almost 100% true except one case: : shared pointer can wrap pointer pointing to local variable in its scope if : you explicitly provide a null deleter : when constructing the shared pointer.
|
|
|
h****8 发帖数: 599 | 11 能否展开说说? 什么叫null deleter?
【在 d****p 的大作中提到】 : What you said is almost 100% true except one case: : shared pointer can wrap pointer pointing to local variable in its scope if : you explicitly provide a null deleter : when constructing the shared pointer.
|
z****e 发帖数: 2024 | 12 就是不调用operator delete的deleter.
【在 h****8 的大作中提到】 : 能否展开说说? 什么叫null deleter?
|
h****8 发帖数: 599 | 13 那具体是什么样子的?
【在 z****e 的大作中提到】 : 就是不调用operator delete的deleter.
|
z****e 发帖数: 2024 | 14 void null_deleter(char*){}
int main(int argc, char* argv[]){
char* a;
shared_ptr spA(a,null_deleter);
}
【在 h****8 的大作中提到】 : 那具体是什么样子的?
|
d****p 发帖数: 685 | 15 if this code stays in main function, it is possible that compiler doesn't
generate code destructing the two shared pointers and you won't see a crash.
Otherwise, there will be a crash for sure.
【在 z****e 的大作中提到】 : 多谢。 : 我也是这么想的, : 也许tr1 和 boost 实现有区别? : 怎么我上边那两行也没报错,也没有运行错误呢? : 我也试了这个: : int* pi=new int(); : tr1::shared_ptr spi1=tr1::shared_ptr(pi); : tr1::shared_ptr spi2=tr1::shared_ptr(pi); : 好像没事。 :
|
z****e 发帖数: 2024 | 16 for sure for sure.
crash.
【在 d****p 的大作中提到】 : if this code stays in main function, it is possible that compiler doesn't : generate code destructing the two shared pointers and you won't see a crash. : Otherwise, there will be a crash for sure.
|