由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - shared_ptr处理stack上面的指针
相关主题
不如各位高手挑个专题讲讲C++11吧关于多个smart pointer系统并存的问题
问个C++中重复删除指针的问题琢磨了一下c++ smart pointer,发现不能到处用
一个nested class的问题Smart Pointer
difference between: char** p and char*p[] ??c++里 weak_ptr用起来是不是耗时间?
C++ pointer problem一个简单的小问题
请问一个关于 cost of pointer的问题 error LNK2001:的错误如何改正?
谁给解释一下这个c question char ** pt1和 char * pt2[] 的区别在哪?
容器中放置智能指针一问C++菜问: 怎么这样也可以?
相关话题的讨论汇总
话题: shared话题: ptr话题: int话题: pointer话题: tr1
进入Programming版参与讨论
1 (共1页)
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.

相关主题
请问一个关于 cost of pointer的问题关于多个smart pointer系统并存的问题
谁给解释一下这个c question琢磨了一下c++ smart pointer,发现不能到处用
容器中放置智能指针一问Smart Pointer
进入Programming版参与讨论
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.

1 (共1页)
进入Programming版参与讨论
相关主题
C++菜问: 怎么这样也可以?C++ pointer problem
菜鸟求教,一个c++的困惑请问一个关于 cost of pointer的问题
再问C++初始化问题。谁给解释一下这个c question
C++ Q05: pointer to constant variable容器中放置智能指针一问
不如各位高手挑个专题讲讲C++11吧关于多个smart pointer系统并存的问题
问个C++中重复删除指针的问题琢磨了一下c++ smart pointer,发现不能到处用
一个nested class的问题Smart Pointer
difference between: char** p and char*p[] ??c++里 weak_ptr用起来是不是耗时间?
相关话题的讨论汇总
话题: shared话题: ptr话题: int话题: pointer话题: tr1