使用Java创建悬挂指针 [英] Creating a dangling pointer using Java

查看:41
本文介绍了使用Java创建悬挂指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Java创建悬空指针?

How can i create a dangling pointer using Java?

推荐答案

根据 Wikipedias定义在下面, .

According to Wikipedias definition below, no.

计算机编程中的悬挂指针和野生指针是不指向适当类型的有效对象的指针.

在不修改指针值的情况下删除或释放对象时会出现悬空指针,因此指针仍指向已释放内存的内存位置

Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory

没有办法删除(或垃圾收集")(如果您愿意的话)某些引用仍指向(1)的对象.

There is no way to delete (or "garbage collect" if you so wish) an object which some reference still points to(1).

在上面的维基百科文章中,您确实可以阅读:

Further down in the above Wikipedia article you can indeed read:

在Java之类的语言中,不会出现悬空指针,因为没有任何机制可以显式地释放内存.而是,垃圾收集器可以释放内存,但是仅当该对象无法从任何引用访问该对象时.

In languages like Java, dangling pointers cannot occur because there is no mechanism to explicitly deallocate memory. Rather, the garbage collector may deallocate memory, but only when the object is no longer reachable from any references.

使引用 not 指向(有效")对象的唯一方法是为其分配null.

The only way to make a reference not point to an ("valid") object, is to assign null to it.

(1)除非它是WeakReference,否则在垃圾回收时该引用将无效.

这篇关于使用Java创建悬挂指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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