Page 1 of 1
TypeOf or similar function
Posted: Sun Dec 20, 2009 4:37 pm
by somic
Hi,
ask for help.
Does someone know how it is possible to determine the type of variable at runtime:
es. something like
Code: Select all
*addr = @dbl
if typeof(*addr) = #pb_double
...
elseif typeof(*addr) = #pb_fixedstring
...
typeof(*addr) = #pb_array
...
Thank you for any advice
Rgds,
Somic
Re: TypeOf or similar function
Posted: Sun Dec 20, 2009 4:46 pm
by SFSxOI
This maybe some info?
http://www.purebasic.fr/english/viewtop ... lit=TypeOf a compiler directive? Don't know if it ever got added as Fred seems to indicate in that thread.
Re: TypeOf or similar function
Posted: Sun Dec 20, 2009 5:06 pm
by eesau
It wouldn't be possible to use TypeOf() on a pointer, as it is just a pointer, and could point to anything. TypeOf for variables would be possible though.
Re: TypeOf or similar function
Posted: Sun Dec 20, 2009 5:29 pm
by somic
@SFSxOI
I knew about that thread but also not able to find the implementation.
@eesau
I understand your point, but SizeOf(*addr) gives correct size of anything pointed by *addr.
I guess that, somewhere in the memory there should be stored the info.
Re: TypeOf or similar function
Posted: Sun Dec 20, 2009 10:43 pm
by Demivec
somic wrote:@eesau
I understand your point, but SizeOf(*addr) gives correct size of anything pointed by *addr.
I guess that, somewhere in the memory there should be stored the info.
If you use SizeOf(*addr) it will always be equal to the size of an Integer. Pointers are Integer variables that reference an address. Here is example code to illustrate:
Code: Select all
Define *addr.Quad,*addr2.Byte
Debug SizeOf(*addr) ;displays 4 on x86 32-bit
Debug SizeOf(*addr2) ;displays 4 on x86 32-bit
I don't think the size is stored anywhere in memory. SizeOf() is a compiler function which means that it doesn't use runtime values. That means the values wouldn't be stored in memory during runtime.