关于智能指针的问题 [英] A question about smart pointer

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

问题描述

智能指针是否永远不会导致内存泄漏?


谢谢,

解决方案

< blockquote>文章< 11 ********************** @ 75g2000cwc.googlegroups。 com>,

Yong Hu < YH ******* @ gmail.com>写道:

智能指针是否永远不会导致内存泄漏?




即使你使用,你仍然可以获得内存泄漏智能指针。很多

当然取决于智能指针的功能,但我认为所有这些都非常容易受到循环引用问题的影响。


Yong Hu写道:

智能指针是否永远不会导致内存泄漏?




我会通过比较其他语言来解释。在C ++中,你必须构建智能的&b
指针并仔细管理它们,这样它们就不会泄漏。如果你使用一个原始的

指针指向一个''new''对象,你会更仔细地管理它或者它会泄漏。

你可以创建一个没有''的对象

泄漏的概率要小得多。


Java语言旨在卖给你的老板。设计师希望告诉你的老板,如果你的程序员使用Java,他们就永远不会泄漏内存,就像顽皮的C ++语言让他们泄密一样。 br />

所以Java有内置的智能指针。你不能用Java编写智能指针

- 它是语言的一部分。并且所有对象必须使用smart

指针。没有对象可以创建没有秘密调用一种新的

函数,深入Java内部。


而且Java也会泄漏内存,很多,如果你滥用它或制造多种

错误。


一般来说,垃圾收集是一个非常棘手的问题,并且所有语言都是

有妥协。所有语言都可能泄露。 C ++,即使有智能指针,

的风险也比大多数语言高,因为C ++可以让你编写安全的代码

非常快。


-

Phlip


在消息< 11 **************** ******@75g2000cwc.googlegroups。 com>,Yong

Hu< yh ******* @ gmail.com>写道

智能指针是否永远不会导致内存泄漏?



它是智能,而不是魔术。


避免内存泄漏的方法是定义一个严格的所有权政策,

然后一直使用正确的工具(可能是聪明的)

指示器或其他东西)来实现和执行它。


-

Richard Herring


Does the smart pointer never lead to memory leak?

Thanks,

解决方案

In article <11**********************@75g2000cwc.googlegroups. com>,
"Yong Hu" <yh*******@gmail.com> wrote:

Does the smart pointer never lead to memory leak?



You can still get memory leaks even if you use smart pointers. Much
depends, of course on what the smart pointer does but I think all of
them are pretty susceptible to circular reference problems.


Yong Hu wrote:

Does the smart pointer never lead to memory leak?



I will explain by comparing other languages. In C++, you must build smart
pointers and manage them carefully so they don''t leak. If you use a raw
pointer to a ''new'' object, you manage it more carefully or it will leak.
And you can create an object without ''new'', and it has much lower odds of
leaking.

The language Java was designed to be sold to your boss. The designers want
to tell your boss, "if your programmers use Java, they will never leak
memory, like that naughty C++ language lets them leak."

So Java has built-in smart pointers. You cannot write the smart pointer
with Java - it''s part of the language. And all objects must use smart
pointers. No object can create without a secret call to a kind of ''new''
function, deep inside Java.

And Java also leaks memory, plenty, if you abuse it or make many kinds of
mistakes.

In general, Garbage Collection is a very tough problem, and all languages
have compromises. All languages can leak. C++, even with smart pointers,
is higher risk than most languages because C++ lets you write safe code
that goes very fast.

--
Phlip


In message <11**********************@75g2000cwc.googlegroups. com>, Yong
Hu <yh*******@gmail.com> writes

Does the smart pointer never lead to memory leak?


It''s "smart", not "magic".

The way to avoid memory leaks is to define a rigorous ownership policy,
and then consistently use the correct tools (which might be smart
pointers, or something else) to implement and enforce it.

--
Richard Herring


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

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