3 ms·
> A plain int carries no information about the validity of the object behind that handle because they are carried around as copies, not references. A pointer i
by AnIdiotOnTheNet 6y ago
> A plain int carries no information about the validity of the object behind that handle because they are carried around as copies, not references.
A pointer is just an integer index to a byte in memory in most computing architectures.
- bluetomcat 6y agoA pointer is an absolute locator of a byte in the address space of a process, while an integer index implies a certain base (start of the module-private array) and a multiplier (object size). By using handles, "user code" is freed from having to know the base and the multiplier, which allows for a more compact locator that remains constant in case of base/multiplier changes.
- kazinator 6y agoAh, but that pointer doesn't get re-used on you until you free it, and for that there are strategies like refcounting and GC. Integers in a small array will get re-used; they have to.
- andolanra 6y agoYou should probably read the whole article, because it describes strategies for dealing with exactly this problem in the section under Memory safety considerations and then again in the update at the end. The short form is: if you have a bounded size on your array, then you will often have unused bits in your index pointer; you can use these to store a "generation counter", which needs to match the slot. You can now re-use indices, because you can compare the generation of the index (stored in the extra bits) with the generation count in the slot, and if they match, it means that the item has been 'freed' and a new thing is in the slot.
- deleted 6y ago[deleted]