issue with comparing structures

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

issue with comparing structures

Post by Jerry Messina » Fri Jun 17, 2016 3:17 pm

It seems the compiler is getting structure compare operations wrong.
When comparing two structures it only seems to work for sizes of 1, 2, or 4 bytes

Code: Select all

// SF 2.2.2.5 or SF 2.2.2.5 BETA 6, ICC 1164
//
// define number of bytes in the struct
// for STRUCT_SIZE = 1, 2, or 4 the comparison works ok
// for other sizes it only compares the first two bytes
// doesn't matter if the struct contains individual items or an array
const STRUCT_SIZE = 7

structure rtc_time_t
    b(STRUCT_SIZE) as byte
end structure

dim t1, t2 as rtc_time_t
dim match as boolean

// 'clear' always gets the structure size correct
clear(t1)
clear(t2)

// structure compare doesn't always compare the correct number of bytes
if (t1 = t2) then
    match = true
endif

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: issue with comparing structures

Post by David Barker » Fri Jun 17, 2016 4:27 pm

I would say that the compiler should not really be allowing this comparison, as there is no internal mechanism for making the comparison (at least, not as far as a remember). There is a mechanism for strings, which are very similar in terms of comparing a block of data but not for structures. My only suggestion for a work around is you write a generic routine that compares a structure. For example,

Code: Select all

Function Equal(a, b As Word, size As Byte) As Boolean
   result = true
   FSR0 = a
   FSR1 = b
   // etc...
End Function

If Equal(@t1, @t2, STRUCT_SIZE) Then // or sizeof(t1)
    match = true
EndIf

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: issue with comparing structures

Post by Jerry Messina » Fri Jun 17, 2016 4:50 pm

>> as there is no internal mechanism for making the comparison

Didn't realize that! It's not something I can recall using before, so if that's the case I'll just toss something together.
Would be nice if it would toss up an error, but it's not pressing so just put it on your 'todo' list if you get a chance.

Thanks.

Post Reply