垃圾引用和悬空引用有什么区别?

发布于 2024-11-05 07:13:57 字数 22 浏览 0 评论 0原文

垃圾引用和悬空引用有什么区别?

What is the difference between garbage and dangling references?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝梦月影 2024-11-12 07:13:57

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

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

在手动内存管理的语言中,例如 C 或 C++,您可能会遇到悬空指针,例如这样做:

int * p = new int;
delete p;

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

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.

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!
这样的小城市 2024-11-12 07:13:57

悬空引用是对不再存在的对象的引用。

什么被视为垃圾取决于垃圾收集器的实现。

对于跟踪和引用计数 GC,不会存在悬空引用(除非存在 GC 实现错误),因为只有那些不存在引用的元素才被视为有资格进行垃圾回收。

因此,悬空引用几乎只对于具有手动内存管理的系统来说是一个问题。

A dangling reference is a reference to an object that doesn't exist anymore.

What is considered garbage depends on the implementation of your garbage collector.

With both tracing and reference counting GCs, dangling references cannot exist (unless there's a GC implementation bug) because only those elements are considered eligible for garbage collection to which no reference exists.

Thus, dangling references are an issue pretty much only for systems with manual memory management.

攀登最高峰 2024-11-12 07:13:57

悬空引用:对最初分配但现在已释放的内存地址的引用

int x= 1000;   //creates a new 
memory block
int* p = x;   // *p is the pointer to address block 1000(mem location) 
int *p = 20;
printf("%d",*p); //This pointer prints 20 
delete p; 
printf("%d",*p); // This would throw an error, because now p is 
                 // inaccessible or dangling. *p is a dangling pointer.

垃圾:已在堆上分配且尚未显式释放但程序无法访问的内存。
Java 有一个垃圾收集器。它及时删除悬空指针和其他垃圾内存。

Dangling Reference: Reference to a memory address that was originally allocated, but is now deallocated

int x= 1000;   //creates a new 
memory block
int* p = x;   // *p is the pointer to address block 1000(mem location) 
int *p = 20;
printf("%d",*p); //This pointer prints 20 
delete p; 
printf("%d",*p); // This would throw an error, because now p is 
                 // inaccessible or dangling. *p is a dangling pointer.

Garbage: Memory that has been allocated on the heap and has not been explicitly deallocated, yet is not accessible by the program.
Java has a garbage collector. It timely removes dangling pointers and other garbage memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文