C语言 什么是强指针和弱指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15486676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:45:59  来源:igfitidea点击:

What are strong pointers and weak pointers

candroid-sourceandroid-binder

提问by Yukio Fukuzawa

I am confused with the notion of "strong pointer" and "weak pointer". Diane Hackborn herself said that:

我对“强指针”和“弱指针”的概念感到困惑。黛安·哈克伯恩 (Diane Hackborn) 自己说:

The object will remain around while there are strong pointers; it is destroyed once the last one is released. All you can do with a weak pointer is comparison and attempting to promote to a strong pointer; the latter will fail if there are no other strong pointers on the object.

当有强指针时,对象将保持不变;一旦最后一个被释放,它就会被销毁。使用弱指针所能做的就是比较并尝试提升为强指针;如果对象上没有其他强指针,后者将失败。

Which is quite unclear to me. Is a strong pointer an equivalent of a (boost::)shared pointer? And what is the role of a weak pointer if it is there just to attempt to promote itself to a strong pointer? Like, when do we need weak and strong pointers?

这对我来说很不清楚。强指针是否等同于 ( boost::) 共享指针?如果弱指针只是为了尝试将自己提升为强指针,那么它的作用是什么?比如,我们什么时候需要弱指针和强指针?

Update:

更新:

Thank you everyone, but I'm asking specifically about android's kernel spand wp, and they have nothing to do with Java's references at all.

谢谢大家,但我专门询问 android 的内核spwp,它们与 Java 的引用完全没有关系。

Basically I'm trying to crack the code here http://www.androidenea.com/2010/03/share-memory-using-ashmem-and-binder-in.htmlAnd don't really understand the use of spand wp

基本上我试图破解此代码http://www.androidenea.com/2010/03/share-memory-using-ashmem-and-binder-in.html而并不真正了解使用spwp

Update:

更新:

The actual answer lies in the comments of the accepted answer. Thanks to Gabe Sechan:

实际答案在于已接受答案的评论中。感谢 Gabe Sechan:

Strong and weak pointers are different smart pointer implementations and do about the same thing- when a pointer goes out of scope, so long as at least one strong pointer references it it will not be freed. If only weak pointers (or nothing) references it will be. The check is done whenever a strong or weak reference to it is descoped.

if I have 10 weak pointers referencing the same object, and one of those 10 goes out of scope, the object will be destroyed? Whereas with strong pointers, only when all 10 of them go out of scope will the object be destroyed?

Yes, almost. If all you have is 10 weak pointers, it would probably have gone out of scope already, when the last strong pointer went out of scope. The implementation may allow it to stick around a little while longer if there's spare memory, but it will be chopped if you go into a low memory condition and it doesn't sound like their implementation is that advanced from her quote. And the use of this is still mainly caching- it is roughly equivalent to a boost shared_ptr and boost weak_ptr. So basically, a weak pointer can have the object it references go away at any time.

强指针和弱指针是不同的智能指针实现,它们做同样的事情——当一个指针超出范围时,只要至少有一个强指针引用它,它就不会被释放。如果只有弱指针(或什么都没有)引用,它将是。每当对它的强引用或弱引用进行分解时,就会进行检查。

如果我有 10 个弱指针引用同一个对象,而这 10 个弱指针中的一个超出范围,该对象将被销毁?而对于强指针,只有当所有 10 个指针都超出范围时,对象才会被销毁?

是的,几乎。如果您只有 10 个弱指针,那么当最后一个强指针超出范围时,它可能已经超出范围。如果有空闲内存,实现可能会允许它坚持一段时间,但如果你进入低内存条件,它会被砍掉,而且听起来他们的实现不像她的引用那样先进。而且这个的使用还是以缓存为主——大致相当于一个boost shared_ptr和boost weak_ptr。所以基本上,弱指针可以让它引用的对象随时消失。

采纳答案by Gabe Sechan

Android is meant to be programmed in Java, not C. Any documentation from the Android team would reference that language. In Java there are strong and weak references. A weak reference doesn't stop the garbage collector from cleaning it up, a strong reference does. They're used for caching on some OSes, but on Android as of 3.0 holding only weak references to an object means it will be collected immediately.

Android 旨在用 Java 编程,而不是 C。Android 团队的任何文档都会引用该语言。在 Java 中,有强引用和弱引用之分。弱引用不会阻止垃圾收集器清理它,强引用会。它们用于在某些操作系统上缓存,但在 Android 3.0 上,仅保留对对象的弱引用意味着它将立即被收集。

C has no equivalent of a weak reference, as it has no garbage collection.

C 没有等效的弱引用,因为它没有垃圾收集。

回答by StarPinkER

sp means StrongPointer in Android, the memory that occupied by the pointed object will be freed if the reference count equals to 0. wp means WeakPointer, so if I have a weak pointer, I don't care whether the referenced object is alive or not. It might be used in some cache and comparison scenarios.

sp表示Android中的StrongPointer,如果引用计数等于0就会释放被指向的对象占用的内存。 wp表示WeakPointer,所以如果我有一个弱指针,我不关心被引用的对象是否活着. 它可能用于某些缓存和比较场景。

First, take a quick look at the sp implementation in StrongPointer.h.

首先,快速浏览一下StrongPointer.h中的 sp 实现。

It is simply a wrapper for reference counting. For example,

它只是一个用于引用计数的包装器。例如,

template<typename T> template<typename U>
sp<T>& sp<T>::operator = (U* other)
{
    if (other) ((T*)other)->incStrong(this);
    if (m_ptr) m_ptr->decStrong(this);
    m_ptr = other;
    return *this;
}

If you create a Strong Pointer by sp<IBinder> strongPointer, the m_ptr is the referenced object. As you can see in the source code, the sp template only represents a strong pointer so that system won't free the memory as long as I hold this sp. It doesn't maintain a reference counter. The counter is maintained in RefBaseclass. And in order to use the StrongPointer, your obj need to be an instance of RefBase.

如果您通过 来创建强指针sp<IBinder> strongPointer,则 m_ptr 是被引用的对象。正如你在源代码中看到的,sp模板只代表一个强指针,所以只要我持有这个sp,系统就不会释放内存。它不维护引用计数器。计数器在RefBase类中维护。为了使用 StrongPointer,您的 obj 需要是 RefBase 的实例。

RefBase class maintains both strong reference counter and weak reference counter, the only difference is the referenced object will be freed if the strong counts to 0. Moreover, for an object managed by Refbase, it may referenced by some Strong Pointers and Weak Pointers simultaneously.

RefBase 类同时维护强引用计数器和弱引用计数器,唯一的区别是如果强引用计数为 0,引用的对象将被释放。此外,对于 Refbase 管理的对象,它可能同时被一些强指针和弱指针引用。

You can see a widely uses of StrongPointers in Android framework, most of them are on IBinder object, a native binder object can passed through different processes. Different processes can hold strong pointers to a same object, the object won't be revoked by system as long as one process are still holding the pointer.

你可以看到在Android框架中StrongPointers的使用非常广泛,大部分都在IBinder对象上,一个原生的binder对象可以通过不同的进程。不同的进程可以持有指向同一个对象的强指针,只要一个进程还持有该指针,该对象就不会被系统撤销。

回答by Emil Davtyan

This is a nice post discussing the difference between regular reference ( or "StrongReference" ), SoftReferences, WeakReferences, and even PhantomReferences in Java, enjoy: http://weblogs.java.net/blog/2006/05/04/understanding-weak-references

这是一篇很好的文章,讨论了Java 中常规引用(或“StrongReference”)、SoftReferences、WeakReferences 甚至PhantomReferences之间的区别,请欣赏:http: //weblogs.java.net/blog/2006/05/04/understanding-弱引用