Strange variable behaviour

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
animo3d
Posts: 24
Joined: Sun Sep 18, 2011 6:02 am
Location: Monterrey Mexico

Strange variable behaviour

Post by animo3d » Wed Jan 06, 2016 1:17 am

Hi i found something i can't explain... may be I did something wrong but this is what is happening...

I have a data sctucture as follows:

Code: Select all

Structure Escena
    MaxTime As Byte
    MinTime As Byte
    Actuado As Byte
    NextE As Byte
    Lights(48) As Byte
    ActList(32) As word
End Structure
and an array of these...

Code: Select all

Dim ProgRun(24) As Escena
all was working until i get in a function and odd things happened when tried to assign te values of some of the array to global vars...

with this code can show you:

Code: Select all

   ActEsc=0
   NumAct=4
    For sa = 0 To NumAct-1
      jt=ProgRun(ActEsc).ActList(sa).byte0
      jg=ProgRun(ActEsc).ActList(sa).byte1
      usart2.write("Esc:",dectostr(actesc)," Act:",dectostr(sa), " T:",dectostr(ProgRun(ActEsc).ActList(sa).byte0), " G:",dectostr(ProgRun(ActEsc).ActList(sa).byte1),13,10)
      usart2.write("Esc:",dectostr(actesc)," Act:",dectostr(sa), " T:",dectostr(jt), " G:",dectostr(jg),13,10)
    Next
where jt and jg are global byte vars, and sa is local byte var, and this is trowing this result:

Code: Select all

Esc:0 Act:0 T:0 G:0
Esc:0 Act:0 T:0 G:0
Esc:0 Act:1 T:0 G:0
Esc:0 Act:1 T:32 G:49
Esc:0 Act:2 T:1 G:1
Esc:0 Act:2 T:6 G:16
Esc:0 Act:3 T:1 G:2
Esc:0 Act:3 T:59 G:197
the values on the array are the real ones i put there, but the assigns to JT and JG, get crazy after the first iteration...

I'm really perplexed here...

any Insight will be apreciated...

User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Re: Strange variable behaviour

Post by Coccoliso » Wed Jan 06, 2016 9:14 am

Hi,
all globals vars are set to 0 and the structure are cleared at the beginning of the program?

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

Re: Strange variable behaviour

Post by Jerry Messina » Wed Jan 06, 2016 12:32 pm

all globals vars are set to 0 and the structure are cleared at the beginning of the program?
No, it's not that. SF doesn't initialize variables at startup... you have to do that yourself if that's what you want.

It looks like it's having a problem with the 'jt = ProgRun(ActEsc).ActList(sa).byte0' syntax. It doesn't always produce the correct address, depending on the usage.
Here's a simple test that shows this:

Code: Select all

device = 18F26K22

include "usart.bas"
include "convert.bas"

Structure Escena
    MaxTime As Byte
    MinTime As Byte
    Actuado As Byte
    NextE As Byte
    Lights(48) As Byte
    ActList(32) As word
End Structure

dim ProgRun(24) as Escena
dim jt as byte
dim jg as byte

dim w as word
dim ix1, ix2 as byte

main:
usart.setbaudrate()

// fill structure array with some values
for ix1 = 0 to 23
    for ix2 = 0 to 31
        ProgRun(ix1).ActList(ix2) = (ix1+1)*256 + (ix2+1)
    next
next

// const array index values...computes these ok
w=ProgRun(0).ActList(1)
jt=ProgRun(0).ActList(1).byte0
jg=ProgRun(0).ActList(1).byte1
// check word vs bytes
if ((w.byte0 <> jt) or (w.byte1 <> jg)) then
    usart.write("not equal", 13, 10)
else
    usart.write("equal", 13, 10)
endif

// variable array index values... not ok
// jt and jg end up with the wrong value
ix1 = 0
ix2 = 1
w=ProgRun(ix1).ActList(ix2)         // w is correct
jt=ProgRun(ix1).ActList(ix2).byte0    // but not these
jg=ProgRun(ix1).ActList(ix2).byte1
// check word vs bytes
if ((w.byte0 <> jt) or (w.byte1 <> jg)) then
    usart.write("not equal", 13, 10)
else
    usart.write("equal", 13, 10)
endif
Both of these should print 'equal', but the second one doesn't.


As a workaround, instead of directly using 'ProgRun(ix1).ActList(ix2).byte0' if you do something like this

Code: Select all

      dim w as word       
      w = ProgRun(ActEsc).ActList(sa)
      jt = w.byte0
      jg = w.byte1
then the values of jt and jg will be correct (plus you'll save a boatload of code)

animo3d
Posts: 24
Joined: Sun Sep 18, 2011 6:02 am
Location: Monterrey Mexico

Re: Strange variable behaviour

Post by animo3d » Wed Jan 06, 2016 6:38 pm

Yes Coco and Jerry all the vars are initilized at start...

but that thing you figured not computing tue right address for de array.... Brilliant....

I will try your workaround...

Jerrry, you are the man..! :D

animo3d
Posts: 24
Joined: Sun Sep 18, 2011 6:02 am
Location: Monterrey Mexico

Re: Strange variable behaviour

Post by animo3d » Wed Jan 06, 2016 7:21 pm

Just to let you know....

worked like a charm..!! :mrgreen: :mrgreen: :mrgreen:

Thanks man...!

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

Re: Strange variable behaviour

Post by David Barker » Fri Jan 08, 2016 6:04 pm

Jerry is definitely a SF star! Thanks for that Jerry...

Post Reply