www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pointer, if pointed type is unknown?

reply "seany" <seany uni-bonn.de> writes:
In my previous post 
(http://forum.dlang.org/thread/jcaomfduqeofobculgli forum.dlang.org) 
I asked for an array of pointers to different types, as a stack 
for my own, where I can generate and insert runtime variables.

However, seems like I need to know the type of variable the 
pointer is pointing to. That is,

double pi = 3.14159;
size_t ptr = cast(ulong)&pi; // so far oky

double pi_2 = *ptr; // this is where things break down

won't work.

What can I do if the type of the variable is not known?
Aug 02 2014
next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Sat, Aug 02, 2014 at 08:21:35PM +0000, seany via Digitalmars-d-learn wrote:
 In my previous post
 (http://forum.dlang.org/thread/jcaomfduqeofobculgli forum.dlang.org) I
 asked for an array of pointers to different types, as a stack for my
 own, where I can generate and insert runtime variables.
 
 However, seems like I need to know the type of variable the pointer is
 pointing to. That is,
 
 double pi = 3.14159;
 size_t ptr = cast(ulong)&pi; // so far oky
 
 double pi_2 = *ptr; // this is where things break down
 
 won't work.
 
 What can I do if the type of the variable is not known?
You can't do anything unless you do runtime type introspection, say via TypeInfo (i.e., typeid(x)). But for non-classes, typeid doesn't give very much information either. Sounds like what you really want is to use a Variant: http://dlang.org/phobos/std_variant.html T -- You are only young once, but you can stay immature indefinitely. -- azephrahel
Aug 02 2014
prev sibling next sibling parent Daniel Gibson <metalcaedes gmail.com> writes:
Am 02.08.2014 22:21, schrieb seany:
 In my previous post
 (http://forum.dlang.org/thread/jcaomfduqeofobculgli forum.dlang.org) I
 asked for an array of pointers to different types, as a stack for my
 own, where I can generate and insert runtime variables.

 However, seems like I need to know the type of variable the pointer is
 pointing to. That is,

 double pi = 3.14159;
 size_t ptr = cast(ulong)&pi; // so far oky

 double pi_2 = *ptr; // this is where things break down

 won't work.

 What can I do if the type of the variable is not known?
if you really think you have to do this (and not use variants or something): use an appropriate cast, i.e. double pi_2 = *(cast(double*)ptr); furthermore: why not use void* instead of size_t? Cheers, Daniel
Aug 02 2014
prev sibling parent "Nobody" <Nobody nowhere.rk> writes:
On Saturday, 2 August 2014 at 20:21:37 UTC, seany wrote:
 In my previous post 
 (http://forum.dlang.org/thread/jcaomfduqeofobculgli forum.dlang.org) 
 I asked for an array of pointers to different types, as a stack 
 for my own, where I can generate and insert runtime variables.

 However, seems like I need to know the type of variable the 
 pointer is pointing to. That is,

 double pi = 3.14159;
 size_t ptr = cast(ulong)&pi; // so far oky

 double pi_2 = *ptr; // this is where things break down

 won't work.

 What can I do if the type of the variable is not known?
I there is a sort of data structure then you can use a type tuple as a map, used to interpret your pointers. However it relies on the context.
Aug 03 2014