Comparememory() return Value [Done]

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Comparememory() return Value [Done]

Post by collectordave »

Can the compareMemory() return value be modyfied so that when comparing memory areas the return value is 0 when they are the same and the byte number beginning at 1 of the first byte that does not match?

Maybe just use this:-

Code: Select all

Procedure.l CompareMemoryByte(*Mem1,Mem2,Length.l)
  
  Define Pointer.l

  
  For Pointer = 0 To Length
    If PeekA(*mem1 + Pointer) <> PeekA(*mem1 + Pointer)
      ProcedureReturn Pointer
    EndIf
    ProcedureReturn -1
    
  EndProcedure
CD
Last edited by collectordave on Tue Jul 30, 2019 3:41 am, edited 3 times in total.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Comparememory() return Value

Post by NicTheQuick »

So you need FindString() but for binary data?
Unfortunality there is no such command available but you could try to implement your own or search in the board for a similar approach. I remember seeing such a procedure in the past. Maybe an other member knows where it is.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Comparememory() return Value

Post by idle »

Wilbert's Module FindData

viewtopic.php?f=12&t=62519
Windows 11, Manjaro, Raspberry Pi OS
Image
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Comparememory() return Value

Post by Little John »

collectordave wrote:Can the compareMemory() return value be modyfied so that when comparing memory areas the return value is 0 when they are the same and the byte number beginning at 1 of the first byte that does not match?
That's not a good idea, because it would break existing code. What you need is another function.

idle was quicker ...
Anyway, in cases like this it's always a good idea to search the forum for "wilbert". :-)

Or perhaps a simple function like the following might be sufficient:

Code: Select all

Procedure.i MemoryDiff (*mem1.Byte, *mem2.Byte, size.i)
   ; -- simple byte by byte comparison of two memory areas
   ; in : *mem1, *mem2: pointers to the memory areas
   ;      size        : number of bytes to compare
   ; out: offset where the first difference is, or
   ;      -1 if both areas are identical or
   ;      -2 on error
   Protected offset.i, last.i=size-1
   
   If *mem1 = 0 Or *mem2 = 0 Or size < 0
      ProcedureReturn -2
   ElseIf *mem1 = *mem2
      ProcedureReturn -1
   EndIf
   
   For offset = 0 To last
      If *mem1\b <> *mem2\b
         ProcedureReturn offset
      EndIf
      *mem1 + 1
      *mem2 + 1
   Next
   
   ProcedureReturn -1
EndProcedure
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Comparememory() return Value

Post by collectordave »

Updated first post with little procedure to do it.

Thanks for all your replies.

regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Comparememory() return Value

Post by mk-soft »

First make sense with a huge amount of data.

Move to General Discussion
Link: viewtopic.php?f=7&t=73297

Code: Select all

... Move
Last edited by mk-soft on Tue Jul 30, 2019 8:46 am, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Comparememory() return Value

Post by collectordave »

Nice one.

Just what I needed.

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Comparememory() return Value [Done]

Post by Tenaja »

With large data it will be faster to test long integers instead of bytes, then just do bounds testing and find within the failed integer to find the byte.
Post Reply