Shop OBEX P1 Docs P2 Docs Learn Events
sizeof equivalent in spin2 — Parallax Forums

sizeof equivalent in spin2

Is there an equivalent of the c function sizeof() in spin2 ? I have tried:

cmdData[0] := $02
cmdData[1] :=$C0

size := strsize(cmdData)

size is 0 so it doesn't seem to work.

Comments

  • Greg,

    In the above code, you'd need to change
    size := strsize(cmdData)

    to

    size := strsize(@cmdData)
    and it'll keep counting bytes until it reaches a nul (0).

    But no, it's not really an equivalent to C's sizeof. If I'm not mistaken, in C, you can even get the size of a type. It's not limited to strings.

  • Still not getting the correct result. size = 1 now.

  • There's no way to get the size of an array because Spin doesn't really have a concept of an array.

    Something like VAR byte array[3] really just means "allocate a byte variable array and reserve 2 more bytes after that". You could (but shouldn't) just as well writeVAR byte array,a1,a2.

    What you probably want is to create a constant that holds the size of the array.

    CON
      ARRAY_SIZE = 3
    VAR
      byte array[ARRAY_SIZE]
    
  • RaymanRayman Posts: 13,861

    Is cmdData defined as byte?

  • MagIO2MagIO2 Posts: 2,243
    edited 2021-03-18 18:48

    strsize can be compared with C function strlen and not with sizeof. sizeof will return the size of the reserved memory, no matter how long the string is, which is currently stored in that array.

    In theory avsa242 is right. Which means that something else in your code is wrong. That's why most time it is best to post the full code here.

    However, my guess would be that your cmdData does not have byte size. Either it is word or long. If you then store a byte, it will only occupy half of the array for a word size element or a quarter for a long size element . The rest is sign extended, thus it is zero for your first assignment. As the strsize is operating on bytes, it will stop at that zero byte(s).

  • It was not defined as byte. I have changed it and it's now working.

Sign In or Register to comment.