智能指针的动机 [英] Motivation for smart pointers

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

问题描述

我想回答的是,在C ++中实现和使用智能指针的最明显的动机是什么?它主要是为了减少因内存访问错误导致的错误或者主要是为了允许延迟设计(即依赖于非确定的非固定设计的

非显式内存释放)?显然两者都有优势,但我想知道程序员是否发现懒惰释放是智能指针的主要用途之一。

对于一些人来说,这个问题背后的动机可能很有趣。

的激励问题是:C ++程序员/软件设计师是否认为显然已经隐式地释放内存的任何优势

在最后一次引用时释放内存使用?


-

Christopher Diggins

另一位语言设计师
http://www.heron-language.com

What I am trying to answer is, what the most prominent motivation for
implenting and using smart pointers in C++ is. Is it primarily to reduce
bugs resulting from memory access errors or primarily to permit lazy
deallocation designs (i.e. designs that rely on non-determined non-fixed
non-explicit deallocation of memory)? Obviously both are advantages, but I
want to know if programmers are finding that lazy deallocation is one of the
major uses of smart pointers.

For some the motivation behind this question may be interesting. The
motivating question is : do C++ programmers / software designers perceive
any advantages to explicitly deallocating memory over implicitly
deallocating memory when the last reference is used?

--
Christopher Diggins
yet another language designer
http://www.heron-language.com

推荐答案

christopher diggins写道:
christopher diggins wrote:
我想回答的是,
实现和使用智能指针的最突出动机是什么在C ++中是。主要是为了减少因内存访问错误导致的错误,还是主要是为了允许延迟释放设计(即依赖于非确定的非固定内存非显式释放的设计)?显然两者都有优势,但我想知道程序员是否发现懒惰释放是智能指针的主要用途之一。

对于一些人来说这个问题背后的动机可能很有趣。这个激动人心的问题是:C ++程序员/软件设计师是否认为在使用最后一个引用时隐式释放内存的任何优势?
What I am trying to answer is, what the most prominent motivation for
implenting and using smart pointers in C++ is. Is it primarily to reduce
bugs resulting from memory access errors or primarily to permit lazy
deallocation designs (i.e. designs that rely on non-determined non-fixed
non-explicit deallocation of memory)? Obviously both are advantages, but I
want to know if programmers are finding that lazy deallocation is one of the
major uses of smart pointers.

For some the motivation behind this question may be interesting. The
motivating question is : do C++ programmers / software designers perceive
any advantages to explicitly deallocating memory over implicitly
deallocating memory when the last reference is used?




对引用的最后引用计数指针

如何变为未使用,与明确删除指针不同

不再使用时? br />



How does the last reference to a reference counted pointer
becoming unused, differ from explicitely deleting a pointer
when it is no longer used?


" christopher diggins" < CD ****** @ users.sourceforge.net>写道:
"christopher diggins" <cd******@users.sourceforge.net> wrote:
我想回答的是,在C ++中实现和使用智能指针的最突出的动机是什么。主要是为了减少因内存访问错误导致的错误,还是主要是为了允许延迟释放设计(即依赖于非确定的非固定内存非显式释放的设计)?显然两者都有优势,但我想知道程序员是否发现懒惰的释放是智能指针的主要用途之一。
What I am trying to answer is, what the most prominent motivation for
implenting and using smart pointers in C++ is. Is it primarily to reduce
bugs resulting from memory access errors or primarily to permit lazy
deallocation designs (i.e. designs that rely on non-determined non-fixed
non-explicit deallocation of memory)? Obviously both are advantages, but I
want to know if programmers are finding that lazy deallocation is one of the
major uses of smart pointers.




首先,我认为智能指针的使用不属于b bb类别的依赖于非确定的,非固定的,

非显式的设计释放记忆。在我看来,释放仍然是高度确定的,非常修复和明确的。与此对比,对于

示例,在另一个线程中运行的垃圾收集器。在这种情况下,

无法通过查看代码来确定解除分配。不是这样

带有智能指针。


我认为使用智能指针的主要原因是为了减少错误。

a设计。



First, I think the use of smart pointers does not fall under the
category of "designs that rely on non-determined, non-fixed,
non-explicit deallocation of memory." The deallocation is still highly
determined, very fixes, and explict in my view. Contrast this with, for
example, a garbage collector running in another thread. In this case,
the deallocation cannot be determined by looking at the code. Not so
with a smart pointer.

I think the primary reason for using smart pointers is to reduce bugs in
a design.


christopher diggins写道:
christopher diggins wrote:
我想回答的是,
实现和使用智能的最突出动机是什么C ++中的指针是。主要是为了减少因内存访问错误导致的错误,还是主要是为了允许延迟释放设计(即依赖于非确定的非固定内存非显式释放的设计)?显然两者都是优点,但我想知道程序员是否发现懒惰释放是智能指针的主要用途之一。


曾经尝试用引用计数对象编写复杂算法吗?


例外安全是另一个。从理论上讲,如果你是一个真正的螺柱,

你可能会把它弄好,但是当有人出现并抛出它时,它不是完全可维护的并且是b $ b一个例外 - 所有地狱都破裂了。


智能指针不需要工作以供参考计数

指针 - 请参阅std :: auto_ptr。

对于一些人来说,这个问题背后的动机可能很有趣。这个激动人心的问题是:C ++程序员/软件设计师是否认为在使用最后一个引用时隐式释放内存的任何优势?
What I am trying to answer is, what the most prominent motivation for
implenting and using smart pointers in C++ is. Is it primarily to reduce
bugs resulting from memory access errors or primarily to permit lazy
deallocation designs (i.e. designs that rely on non-determined non-fixed
non-explicit deallocation of memory)? Obviously both are advantages, but I
want to know if programmers are finding that lazy deallocation is one of the
major uses of smart pointers.
Ever tried to write a complex algorithm with reference counted objects ?

Exception safety is the other one. In theory, if you''re a real stud,
you can probably get it right, but it''s not exactly maintainable and
when someone comes along and throws an exception - all hell breaks loose.

smart pointers do not necessarilt have to work for reference counted
pointers - see std::auto_ptr.

For some the motivation behind this question may be interesting. The
motivating question is : do C++ programmers / software designers perceive
any advantages to explicitly deallocating memory over implicitly
deallocating memory when the last reference is used?




我怀疑你可以明确地使用智能ptrs删除对象,因为他们不再使用
- 所以我不知道你在哪里隐含真的来了

来自。


例如


{

gianni_ptr< typ * > x =新典型;

x-> stuff();


}<<< --- x'的析构函数做魔法这里

我想你可能会想到在C中你需要的代码实际上是你自己的析构函数调用。 C ++提供了比这更多的语义

- 编译器会在需要的时候插入析构函数调用x




这里使用了RAII(资源获取是初始化)习语,但

它不仅仅用于它 - 它还用于互斥量

例。它可能是该语言的基石之一,因为如果你正确使用它就会获得
- 编写可维护的代码非常容易。



I suspect you can use smart ptrs explicitly to delete object when they
are no longer used - so I don''t know where you "implicity" really comes
from.

e.g.

{
gianni_ptr<typ *> x = new typ;
x->stuff();

} <<<--- x''s destructor does magic here
I think you might be thinking like in C where the code you need to
actually put in your own destructor call. C++ provides more semantics
than that - the compiler will insert that destructor call for x
whereever it''s needed.

The RAII (Resource Aquisition Is Initialization) idiom is used here but
it''s used in more than just this - it''s also used for mutexes for
example. It''s probably one of the cornerstones of the language because
if you use it correctly - it''s really easy to write maintainable code.


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

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