使用或不使用智能指针? [英] To use or not to use smart pointers?

查看:73
本文介绍了使用或不使用智能指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天与一些程序员进行了3个小时的会议,这些程序员部分地不相信在C ++中使用智能指针。他们主要担心的是可能的性能影响。我已经无休止地解释了智能

指针的优势(目前我们所有的C ++软件都使用过;我们使用Boost智能指针来b $ b $) m严重担心有一个转换为原始指针的b $ b。我们不是在开发系统软件,而是开发正常的Windows程序(打开例外)。我不想

编写一个没有智能指针的C ++程序,但在那无休止的讨论之后我想知道我是不是太严格了。任何严肃的论据都不使用

智能指针?


鲍里斯

解决方案

Boris写道:


我今天与一些程序员进行了3个小时的会议,但是部分不相信在C ++中使用智能指针。他们的主要问题是可能会对性能造成影响。我已经无休止地解释了智能指针的优点(目前在我们所有的C ++软件中使用了
;我们使用Boost智能指针)就像我一样m

严重关注转向原始指针。我们不是开发系统软件,而是正常的Windows程序(打开

例外)。我不想再编写一个没有

智能指针的C ++程序,但经过无休止的讨论后,我想知道是否

我太严格了。任何严肃的论据都不使用智能指针?



_s_没有性能影响。无论谁声称有,都应该向你证明这一点,而不仅仅是这样说。一个智能指针只是一个让程序员放松的方式,而不必记住何时当指针超出范围时删除对象
。所有访问

到实际对象在编译时解决 - 没有开销。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问




" Boris" < bo *** @ gtemail.netwrote in message

news:op *************** @ burk.mshome.net ...


>我今天与一些程序员会面了3个小时,他们部分不相信在C ++中使用智能指针。他们主要担心的是可能的性能影响。我一直在无休止地解释智能指针的优点(目前在我们所有的C ++软件中使用;我们使用Boost智能指针)因为我非常担心有一个<转移到原始指针。我们不是在开发系统软件,而是开发正常的Windows程序(打开例外)。我不想再写一个没有智能指针的C ++程序了,但在那无休止的讨论之后我想知道我是不是太严格了。任何严肃的论据都不使用智能指针?



哦,我的天哪,你认真吗?没门。你是绝对正确的。智能指针FAR的好处超过任何可能的反对它们的论点。

没有性能影响(我知道),以及优点

(自动和正确的对象/资源生命周期管理,避免在存在例外的情况下内存泄漏,仅举两例)太

智能指针的论点是他们有时奇怪的使用语法(额外打字,丑陋?

我不知道),但是我的观点是,这是一个非常小的代价,因为它们的使用给予了安心和安全。


如果你的同事说服你,你应该在另一个

领域工作。如果他们不想听你讲智能指针的好处,那就去

在其他地方工作,在这里你可以理解你在这个主题上的位置(如果不需要
)!


- 丹尼斯


2007年7月18日星期三16:17:13 -0400,Victor Bazarov写道:


Boris写道:


>我今天和一些其他程序员会面了3个小时>部分不相信在C ++中使用智能指针。他们主要关心的是可能的性能影响。我一直在无休止地解释智能指针的优点(目前在我们的所有C ++软件中使用;我们使用Boost智能指针)因为我非常关心转向原始指针。我们没有开发系统软件,而是正常的Windows程序(打开了例外情况)。我不想再写一个没有智能指针的C ++程序了,但经过无休止的讨论,我想知道我是否太严格了。任何严肃的论据都不使用智能指针?



__没有性能影响。无论谁声称有,都应该向你证明这一点,而不仅仅是说。一个智能指针只是一种方式

让程序员放松,而不必记住何时在指针超出范围时删除

对象。所有对实际的

对象的访问都在编译时解决 - 没有开销。



boost :: shared_ptr(或std :: tr1 :: shared_ptr,如果你愿意的话)有一个显着的性能开销,特别是在被复制时。 www.boost.org 上有

基准测试,我有我自己也测量了这个。


原因很明显:


1. boost :: shared_ptr比普通指针大。它基本上持有

两个指针 - 一个到实际对象另一个到参考

计数器。

2.参考计数器需要复制/销毁

智能指针时递增/递减。

3.操作螺纹参考计数器需要锁定

安全性。


这对于给定的应用程序是否都有所不同,远远不是很明显。我已经切换了一个应用程序,该应用程序广泛使用了一个使用boost :: shared_ptr构建的树
结构来提升:: intrusive_ptr,并且具有

显着的性能提升。


-

Markus Schoder


I had a 3 hours meeting today with some fellow programmers that are partly
not convinced about using smart pointers in C++. Their main concern is a
possible performance impact. I''ve been explaining the advantages of smart
pointers endlessly (which are currently used in all our C++ software; we
use the Boost smart pointers) as I''m seriously concerned that there is a
shift to raw pointers. We are not developing system software but rather
normal Windows programs (with exceptions turned on). I wouldn''t want to
write a C++ program without smart pointers any more but after that endless
discussion I wonder if I''m too strict. Any serious arguments not to use
smart pointers?

Boris

解决方案

Boris wrote:

I had a 3 hours meeting today with some fellow programmers that are
partly not convinced about using smart pointers in C++. Their main
concern is a possible performance impact. I''ve been explaining the
advantages of smart pointers endlessly (which are currently used in
all our C++ software; we use the Boost smart pointers) as I''m
seriously concerned that there is a shift to raw pointers. We are not
developing system software but rather normal Windows programs (with
exceptions turned on). I wouldn''t want to write a C++ program without
smart pointers any more but after that endless discussion I wonder if
I''m too strict. Any serious arguments not to use smart pointers?

There _is_ no performance impact. Whoever claims there is, should
prove it to you instead of just saying that. A smart pointer is just
a way for a programmer to relax and not have to remember when to
delete the object when the pointer goes out of scope. All access
to the actual object is resolved at compile time - no overhead.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



"Boris" <bo***@gtemail.netwrote in message
news:op***************@burk.mshome.net...

>I had a 3 hours meeting today with some fellow programmers that are partly
not convinced about using smart pointers in C++. Their main concern is a
possible performance impact. I''ve been explaining the advantages of smart
pointers endlessly (which are currently used in all our C++ software; we
use the Boost smart pointers) as I''m seriously concerned that there is a
shift to raw pointers. We are not developing system software but rather
normal Windows programs (with exceptions turned on). I wouldn''t want to
write a C++ program without smart pointers any more but after that endless
discussion I wonder if I''m too strict. Any serious arguments not to use
smart pointers?

Oh my gosh, are you serious? No way. You are absolutely correct. The
benefits of smart pointers FAR outweigh any possible arguments against them.
There is no performance impact (that I know of), and the advantages
(automatic and correct object/resource lifetime management, avoidance of
memory leaks in the presence of exceptions, to name only two) are too
compelling to ignore. Another (strange, but typical) argument made against
smart pointers is their sometimes odd usage syntax (extra typing, ugliness?
I don''t know), but in my view, that is a very small price to pay for the
peace of mind and safety afforded by their use.

If your collegues convince you otherwise, you should be working in another
field. If they won''t listen to you about the benefits of smart pointers, go
work somewhere else where your position on the subject is appreciated (if
not required)!

- Dennis


On Wed, 18 Jul 2007 16:17:13 -0400, Victor Bazarov wrote:

Boris wrote:

>I had a 3 hours meeting today with some fellow programmers that are
partly not convinced about using smart pointers in C++. Their main
concern is a possible performance impact. I''ve been explaining the
advantages of smart pointers endlessly (which are currently used in all
our C++ software; we use the Boost smart pointers) as I''m seriously
concerned that there is a shift to raw pointers. We are not developing
system software but rather normal Windows programs (with exceptions
turned on). I wouldn''t want to write a C++ program without smart
pointers any more but after that endless discussion I wonder if I''m too
strict. Any serious arguments not to use smart pointers?


There _is_ no performance impact. Whoever claims there is, should prove
it to you instead of just saying that. A smart pointer is just a way
for a programmer to relax and not have to remember when to delete the
object when the pointer goes out of scope. All access to the actual
object is resolved at compile time - no overhead.

boost::shared_ptr (or std::tr1::shared_ptr if you prefer) has a
significant performance overhead especially when being copied. There are
benchmarks at www.boost.org and I have also measured this myself.

The reasons are fairly obvious:

1. A boost::shared_ptr is larger than a plain pointer. It basically holds
two pointers -- one to the actual object the other to the reference
counter.
2. The reference counter needs to be incremented/decremented when the
smart pointer is copied/destroyed.
3. Locking is required to manipulate the reference counter for thread
safety.

Whether this all makes a difference in a given application is far from
obvious. I have switched an application that was extensively using a tree
structure built using boost::shared_ptr to boost::intrusive_ptr with a
significant performance gain.

--
Markus Schoder


这篇关于使用或不使用智能指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆