Comparing byte arrays

I want to compare two byte arrays for equality (same length, same elements in the same order) which is easy enough to do with a small method. But I did wonder about this:

[code]Dim aa(), bb() As byte

aa.Append (10)
bb.Append (10)

if (aa=bb) then
Label1.Text = “Equal”
else
Label1.Text = “Unequal”
end if[/code]

but this always gives “unequal”. What actually is the sematics of comparing two arrays as above? I was hoping that comparing arrays for equality would be faster than a loop I could write myself.

Likely what you are comparing here is the memory address of the first byte

if these were memoryblocks , 10 bytes long, you could still address the bytes in much the same way

mem1.byte(6) =

But you could compare the .stringvalue of the memoryblocks for equality

The comparison is “are they actually the same array?”

Consider

dim aa(), bb() as byte

aa.append 10
bb.append 10

if (aa = bb) then
  label1.text = "Equal"
else
  label1.text = "Not Equal"
end
// Not Equal

bb = aa
bb.append 20
label1.text = str(aa(1)) 
// 20

if (aa = bb) then
  label1.text = "Equal"
else
  label1.text = "Not Equal"
end
// Equal

Arrays are similar to objects in that the array exists independent of the variable that refers to it, and you can have more than one variable refer to the same array.

Depending on the size of your arrays, a loop should be fine. If the arrays are huge, this might be faster, but I haven’t tested:

if aa.Ubound <> bb.Ubound then
 return false
end if

if aa.Ubound = -1 then
  return true
end if

dim mb1 as new Xojo.Core.MemoryBlock( aa )
dim mb2 as new Xojo.Core.MemoryBlock( bb )

return mb1 = mb2

[quote=402546:@Kem Tekinay]Depending on the size of your arrays, a loop should be fine. If the arrays are huge, this might be faster, but I haven’t tested:

[code]
if aa.Ubound <> bb.Ubound then
return false
end if

if aa.Ubound = -1 then
return true
end if

dim mb1 as new Xojo.Core.MemoryBlock( aa )
dim mb2 as new Xojo.Core.MemoryBlock( bb )

return mb1 = mb2
[/code][/quote]

The arrays would be unlikely to be much more than 100 bytes long, in practice. So I wasn’t so much worried about that, as to whether I could just do if (aa=bb) then ... (since it looks nicer :slight_smile: and avoids me spending 30 secs coding up a loop).

However: what TimH has to say above implies that if I were to pass a byte array as a parameter to a method, and if in that method I set this array to another that was Dimmed inside the method, then my first array would be a reference to the method’s array - is this so? Sort of like this:

[code]dim outer() as byte
outer.append (100)

myfunc (outer)
msgbox (outer(0).totext) // should be 10

subroutine myfunc (byref bytes() as byte)

dim inner() as byte

bytes = inner

inner.append (10)
inner.append (20)

end sub[/code]

However, experimenting with this would seem to show that this is only the case if the bytes parameter for myfunc is declared with the byref keyword, as above. This would appear to contradict the documentation, which says that arrays are passed by reference.

You’ve got the assignment reversed. Try

dim inner() as byte
inner = bytes

Then it will work the way you expected, without byref.

Ah, that makes sense - thanks.