垃圾引用和悬挂引用之间有什么区别? [英] What is the difference between garbage and dangling references?

查看:192
本文介绍了垃圾引用和悬挂引用之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

垃圾引用和悬挂引用之间有什么区别?

What is the difference between garbage and dangling references?

推荐答案

悬挂引用是对不再存在的对象的引用.垃圾是无法通过引用到达的对象.

A dangling reference is a reference to an object that no longer exists. Garbage is an object that cannot be reached through a reference.

在垃圾收集语言中不存在悬挂引用,因为仅当对象不再可访问时才回收对象(仅收集垃圾).在某些语言或框架中,您可以使用弱引用",因为在收集过程中不会考虑它们,所以可以将它们悬空.

Dangling references do not exist in garbage collected languages because objects are only reclaimed when they are no longer accessible (only garbage is collected). In some languages or framework, you can use "weak references", which can be left dangling since they are not considered during collection passes.

在具有手动内存管理的语言(例如C或C ++)中,例如,您可以遇到悬空的指针:

In languages with manual memory management, like C or C++, you can encounter dangling pointers, by doing this for instance:

int * p = new int;
delete p;

int i = *p; // error, p has been deleted!

这篇关于垃圾引用和悬挂引用之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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