bug?

11 views
Skip to first unread message

kobi2187

unread,
Mar 14, 2015, 4:34:54 PM3/14/15
to ooc-...@googlegroups.com
hi,
I am probably just being stupid, but the following code acts strangely

test_while:func()
{
  seq:=[1]
  x:= seq length

  while(x>=0)
  {
    "%d" printfln(x)
    x -= 1
  }
}

it should just print 1 0, instead the while condition is not checked. it goes to infinite loop.
Why?
if there is no array it seems to work.
I haven't programmed for a long time, so could it be just my empty brain?

PS: can I run a for loop backwards? ( 10:1:-1)

Thanks, kobi

Amos Wenger

unread,
Mar 14, 2015, 4:40:16 PM3/14/15
to kobi2187, ooc-...@googlegroups.com
An array's length is a SizeT, which is unsigned, so it wraps around to 2^32-1 on the first iteration :)

You can cast seq length to an int with:

x := seq length as Int

That, or SSizeT, or whatever type you want.

Here's the C code generated by rock for (something resembling) your sample: https://gist.github.com/fasterthanlime/c4854c9a04a9be004097#file-nonsense-c-L43

Hope that helps!
Cheers,
- Amos

--
You received this message because you are subscribed to the Google Groups "ooc-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ooc-lang+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kobi2187

unread,
Mar 15, 2015, 12:22:23 PM3/15/15
to ooc-...@googlegroups.com
Yes, that really helps. thank you!
I always found overflowing a weird behaviour.
I would much rather see an assert firing instead.
btw, why names like SizeT instead of UInt ?

Amos Wenger

unread,
Mar 15, 2015, 12:25:18 PM3/15/15
to kobi2187, ooc-...@googlegroups.com
Oh but there is UInt, and Int, and UInt32, UInt16, UInt8 — the whole charade. It's based on C types, which, unfortunately, there are many of.
"SizeT" is the size of a pointer on your target platform, usually 4 or 8 bytes.

--
Reply all
Reply to author
Forward
0 new messages