What happens to dangling pointers in java?

What happens to dangling pointers in java?

Question:
What happens to dangling pointers in Java? Supposedly there is garbage collection, but when is it performed? From what Ihave read, it seems really easy to have persistent danglingpointers (pointers to objects that no one is using that aren’t being cleaned up by garbage collection).

Let’s say I have a collection of objects, and one of the functions that manipulates the collection is called removeObject(int i), which removes the ith object from the collection and returns a pointer to the removed object.

Sometimes I need to get the removed object to salvage it for reuse; other times I simply want to remove the object. Is the returned pointer left dangling if I don’t assign it when I make the function call? Should it be left to the function-caller to dispose of unused objects, or should the programmer maketwo functions, deleteObject() and removeObject()?

Answer:
By definition, a dangling reference is an active binding between aname and a de-allocated segment of the heap. By definition, garbageis a segment of the heap that is no longer bound to a name by anactive binding. Only garbage is de-allocated by the garbage collector;hence, a dangling reference is not possible. This, and no memoryleaks, are the principle advantages of garbage collection.

If I understand your example, you are asking what the difference is between

removeObject(i);
and
x = removeObject(i);
Assuming there are no other active references to the i-th objectof the collection, this object becomes garbage after the firstcall. A pointer to the object was returned, but was not bound toa name, hence there is no dangling reference.

By contrast, the returned pointer was bound to x in the second call, so it can still be accessed through the name x, and is not garbage.

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved