Determining TypeOf

Just starting out? Need help? Post your questions and find answers here.
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

Yep, seems you're right.

Code: Select all

a.s = "12345678"
CopyMemory(@a, @a+2, 4)
Debug a

a.s = "12345678"
MoveMemory(@a, @a+2, 4)
Debug a

Debug "--------"

a.s = "12345678"
CopyMemory(@a+2, @a, 4)
Debug a

a.s = "12345678"
MoveMemory(@a+2, @a, 4)
Debug a
Fred, how are those two commands implemented? Normally CopyMemory should be a little bit faster, but also more unstable because it hasn't to look for overlapping memory adresses.

This makes me somewhat responsible as it was my idea to introduce MoveMemory :oops:
<°)))o><²³
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

:lol:


I just read in the help file for Windows API and it said that the results of CopyMemory were undefined when the memory blocks overlap and that MoveMemory didn't have this "problem". However they do seem to work the same.
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Well, just because the behaviour is undefined does not mean it _has_ to fail ;)

They both wrap directly the api calls (which as i have just seen are defined as
memcpy() and memmove() in the headers.)

I would guess that it maybe makes a difference for copying/moving larger areas of memory. (not sure though)
All documentation states that memcpy()/CopyMemory() is unsafe to use with
overlapping buffers, so there has to be something to it.
quidquid Latine dictum sit altum videtur
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Do not believe everything that is true.
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 »

I find the path of least resistance to be = doing it Fred's way.
Last edited by netmaestro on Tue Feb 21, 2006 2:59 pm, edited 4 times in total.
BERESHEIT
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

At least here, they seem to be the same:

Code: Select all

s1.s = ""

For x=1 To 20000
  s1 + Chr(Random(25)+65)
Next

s2.s = s1.s


t1 = ElapsedMilliseconds()
For i = 0 To 1000000
  CopyMemory(@s1 + 9900, @s1, 9900)
Next
t2 = ElapsedMilliseconds()
For i = 0 To 1000000
  MoveMemory(@s2 + 9900, @s2, 9900)
Next
t3 = ElapsedMilliseconds()

OpenConsole()

If s1 = s2
  PrintN("Gleich")
Else
  PrintN("Ungleich")
EndIf

PrintN("1. " + Str(t2 - t1))
PrintN("2. " + Str(t3 - t2))

Input() 
CloseConsole()
Though that doesn't mean nothing, another OS - even another Windows - might bring a surprise.
<°)))o><²³
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

freedimension wrote:Though that doesn't mean nothing, another OS - even another Windows - might bring a surprise.
That's the whole point.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Fred wrote:
freedimension wrote:Though that doesn't mean nothing, another OS - even another Windows - might bring a surprise.
That's the whole point.
Not really, since it's the same function on all Windowes. MoveMemory() and CopyMemory() aren't in Windows, they are both macros for functions in the standard c libraries. Once compiled and working it should work everywhere.
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

freedimension wrote:Though that doesn't mean nothing, another OS - even another Windows - might bring a surprise.
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 »

I GOT IT!! I devised a plan to write a command that accurately returns the data type of a variable. It's partly tested and working so far, I just have to put it through the wringer before showing it off. When it's finished I'll post.
Last edited by netmaestro on Tue Feb 21, 2006 1:35 pm, edited 2 times in total.
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

netmaestro wrote:I GOT IT!! I devised a plan to write a command that accurately returns the data type of a variable. It's partly tested and working so far, I just have to put it through the wringer before showing it off. When it's finished I'll post.
That's impossible unless you carry with you some pre-execute information under the table.
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 »

As promised, for numeric types here goes:

Code: Select all

Macro GetTypeOf(x,y) 
  *sto=AllocateMemory(SizeOf(x)) 
  CopyMemory(@x,*sto,SizeOf(x)) 
  x=1 
  If PeekQ(@x) = x And SizeOf(x) = 8 
    y="Quad" 
  ElseIf PeekL(@x) = x And SizeOf(x) = 4 
    y="Long" 
  ElseIf PeekF(@x) = x And SizeOf(x) = 4 
    y="Float" 
  ElseIf PeekD(@x) = x And SizeOf(x) = 8 
    y="Double" 
  ElseIf PeekW(@x) = x And SizeOf(x) = 2 
    y="Word" 
  ElseIf PeekB(@x) = x And SizeOf(x) = 1 
    y="Byte" 
  EndIf 
  CopyMemory(*sto,@x,SizeOf(x)) 
EndMacro 
  
a.l 
b.q 
c.f 
d.d 
e.w 
f.b 


LastType.s = ""  ;store the result here 
GetTypeOf(a,LastType) 
Debug LastType 
GetTypeOf(b,LastType) 
Debug LastType 
GetTypeOf(c,LastType) 
Debug LastType 
GetTypeOf(d,LastType) 
Debug LastType 
GetTypeOf(e,LastType) 
Debug LastType 
GetTypeOf(f,LastType) 
Debug LastType 
Last edited by netmaestro on Tue Feb 21, 2006 1:40 pm, edited 5 times in total.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

:D

Regardless of whether it should work or not, pretty nifty!
@}--`--,-- A rose by any other name ..
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

netmaestro wrote:As promised, for numeric types, here goes:
Only numeric types? That's cheating. Besides:
1. You use SizeOf() which is not a run-time function, it's result is determined at compile time (look at the generated assembly code).
2. It doesn't work. Huh? Look at this:

Code: Select all

Macro GetTypeOf(x,y) 
  *sto=AllocateMemory(SizeOf(x)) 
  CopyMemory(@x,*sto,SizeOf(x)) 
  x=1 
  If PeekQ(@x) = x And SizeOf(x) = 8 
    y="Quad" 
  ElseIf PeekL(@x) = x And SizeOf(x) = 4 
    y="Long" 
  ElseIf PeekF(@x) = x And SizeOf(x) = 4 
    y="Float" 
  ElseIf PeekD(@x) = x And SizeOf(x) = 8 
    y="Double" 
  ElseIf PeekW(@x) = x And SizeOf(x) = 2 
    y="Word" 
  ElseIf PeekB(@x) = x And SizeOf(x) = 1 
    y="Byte" 
  EndIf 
  CopyMemory(*sto,@x,SizeOf(x)) 
EndMacro 
  
a.l 
b.q 
c.f 
d.d 
e.w 
f.b 
g.c ;)

LastType.s = ""  ;store the result here 
GetTypeOf(a,LastType) 
Debug LastType 
GetTypeOf(b,LastType) 
Debug LastType 
GetTypeOf(c,LastType) 
Debug LastType 
GetTypeOf(d,LastType) 
Debug LastType 
GetTypeOf(e,LastType) 
Debug LastType 
GetTypeOf(f,LastType) 
Debug LastType
GetTypeOf(g,LastType) ;)
Debug LastType
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 »

And for chars included:

Code: Select all

Macro GetTypeOf(x,y) 
  *sto=AllocateMemory(SizeOf(x)) 
  CopyMemory(@x,*sto,SizeOf(x)) 
  x=1 
  If PeekQ(@x) = x And SizeOf(x) = 8 
    y="Quad" 
  ElseIf PeekL(@x) = x And SizeOf(x) = 4 
    y="Long" 
  ElseIf PeekF(@x) = x And SizeOf(x) = 4 
    y="Float" 
  ElseIf PeekD(@x) = x And SizeOf(x) = 8 
    y="Double" 
  ElseIf PeekW(@x) = x And SizeOf(x) = 2 
    y="Word" 
  ElseIf PeekB(@x) = x And SizeOf(x) = 1 
    y="Byte"
    x=255
    If x = 255 ; a byte type will fail this test 
      y="Char" 
    EndIf
  EndIf 
  CopyMemory(*sto,@x,SizeOf(x)) 
EndMacro 
  
a.l 
b.q 
c.f 
d.d 
e.w 
f.b 
g.c  

LastType.s = ""  ;store the result here 
GetTypeOf(a,LastType) 
Debug LastType 
GetTypeOf(b,LastType) 
Debug LastType 
GetTypeOf(c,LastType) 
Debug LastType 
GetTypeOf(d,LastType) 
Debug LastType 
GetTypeOf(e,LastType) 
Debug LastType 
GetTypeOf(f,LastType) 
Debug LastType 
GetTypeOf(g,LastType) ;) 
Debug LastType 
Last edited by netmaestro on Thu Feb 23, 2006 10:42 am, edited 3 times in total.
Post Reply