String[] pointer to void* and back
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Sep 18 15:16:47 PDT 2014
On 09/18/2014 02:52 PM, seany wrote:
> what if i needed to access many such runtime variables of many types,
> and did not want to create a member for each type?
If you are holding an address in a void*, you must make sure that the
original object is still at that location when you attempt to access the
object.
If there are limited number of such string[] arrays then you can
populate an array in the beginning and pass around void* values to
elements in there.
However, as soon as an element is added or removed from an array, all
(or some) of the references to those elements get invalidated. (An
exceptions is where the array has capacity and you add an element.)
Linked lists, trees, etc. don't have that problem: Their elements stay
where they are even after elements are added to or removed from the
container.
If you can, storing an index instead of a void* is a better way to go.
Even if the elements are relocated, as long as no element is removed
from the collection, an index value will always be valid.
Ali
More information about the Digitalmars-d-learn
mailing list