Determining TypeOf

Just starting out? Need help? Post your questions and find answers here.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Determining TypeOf

Post by Dare2 »

I did a search for this, with variations, and found an interesting slanging match on multiple processes. :)

But nothing on what I was looking for. :(

So, is there a way to find the type of a variable, from a pointer or address to that variable.

Eg:

Code: Select all


Procedure handleVar(v)
  x=myTypeOf(v+SomeMagicOffsetOrSomethingLikeThat)
  select x
   case #myLONG
..
EndProcedure

varL.l=123
varS.s="ABC"

handleVar(@varL)
handleVar(@varS)
@}--`--,-- A rose by any other name ..
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

No, it's not possible because PureBasic doesn't has any extra (mainly useless) information to know of which type is a variable. Anyway, it groups all the long, word and byte together, so it could be possible to detect these primitive type (for global/main variable only) by comparing their addresses.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Okay, thanks for the answer. Appreciate it.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Any progress now that PB 4 is out? I understand it's been heavily redesigned, could that make it easier to determine the type of a variable?
Last edited by netmaestro on Tue Feb 21, 2006 2:57 pm, edited 2 times in total.
BERESHEIT
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

MEMORY DEGRADING WARNING ON...

Sometime back I peeked around the value returned by @Variable and @Array() and found some interesting things... I even wrote some simple routines to swap variables around... and I also wrote insert and delete array elements; but wondered what happened with the memory allocated to string element that were pushed out of the end of an array and became orphaned.

I felt I was on thin ice because I was assuming that the method of storing variables might change... so I stopped.

This does remind me... Insert() and Delete() are mighty handy array operations and are simple additions at low level... Fred?
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

No, PB isn't an intepreted langage which means you can't really detect of which type is a variable on runtime (especially when being in a procedure).
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

RichardL wrote:This does remind me... Insert() and Delete() are mighty handy array operations and are simple additions at low level... Fred?
Every value is at the base address of the array plus the index multiplied by the size of the type (or a pointer). Insert and delete will need to move every index of the array that is after where you insert or delete. Not hyper-efficient, actually.
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

Hi Trond.
I'm not sure this is right...

For insert and delete of varibles of known size (all numeric types) this can be very efficient.

Using the customary rules for moving a block where the destination and source overlap it is quicker to move the variables in memory than use a BASIC loop that needs to compute address' from the array index each time.

As an alternative, copying the entire array to a buffer and putting it back could take advantage of high speed block moves. I admit this is not a nice method, but when speed is of the essence it is a technique worth considering. ( I have done this in a hardware system and used an FPGA to move RAM blocks around... the alternative was to design an address mangle that left the data static and remapped the addresses with an ALU... even nastier :lol: )

Do the Intel family processors have MOVE *A1, *A2, N ? If so then block moves should run like grease lightning. (No PC ASM here, yet!)
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You can probably do these functions yourself as ReDim is available with PB4 (with the help of MoveMemory()).
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

What's the point of MoveMemory() when we've got CopyMemory()??
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Trond wrote:What's the point of MoveMemory() when we've got CopyMemory()??
MoveMemory() moves it and CopyMemory copies it? 8)
--Kale

Image
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

CopyMemory() doesn't work if the memory areas overlaps.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

It definetely does. I've used it like that many times.

Code: Select all

Mem = AllocateMemory(6)
For I = 0 To 5
  PokeB(Mem + I, I)
Next

For I = 0 To 5
  Debug PeekB(Mem + I)
Next
Debug "---"
CopyMemory(Mem, Mem+2, 6-2)
For I = 0 To 5
  Debug PeekB(Mem + I)
Next
Kale wrote:
Trond wrote:What's the point of MoveMemory() when we've got CopyMemory()??
MoveMemory() moves it and CopyMemory copies it? 8)
MoveMemory() leaves a black void?
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

Trond wrote:It definetely does. I've used it like that many times.

Code: Select all

Mem = AllocateMemory(6)
For I = 0 To 5
  PokeB(Mem + I, I)
Next

For I = 0 To 5
  Debug PeekB(Mem + I)
Next
Debug "---"
CopyMemory(Mem, Mem+2, 6-2)
For I = 0 To 5
  Debug PeekB(Mem + I)
Next
Backwards too?

Code: Select all

...
CopyMemory(Mem, Mem-2, 6-2)
...
<°)))o><²³
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

freedimension wrote: Backwards too?

Code: Select all

...
CopyMemory(Mem, Mem-2, 6-2)
...
Surely, provided you do not write outside allocated memory (Mem-2). :wink:

Code: Select all

...
CopyMemory(Mem+2, Mem, 6-2)
...
:twisted:
Post Reply