0
let P: UInt32 = 0xB7E15163
let Q: UInt32 = 0x9E3779B9 
let r = 20

var S:[UInt32] = Array(count: 2*r+4, repeatedValue: 0)

S[0] = P;


for i in 1...2*r+3 {
    S[i] = S[i-1] + Q
}

The error occurs on the last line of the code where the closing bracket is. Not sure why I am getting this error, if anyone could help that would be greatly appreciated.

thread #1: tid = 0x11615, 0x000000011ae465ed $__lldb_expr359`__lldb_expr_359.generateKeys () -> Swift.Array<Swift.UInt32> + 4509 at playground359.swift:30, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
  * frame #0: 0x000000011ae465ed $__lldb_expr359`__lldb_expr_359.generateKeys () -> Swift.Array<Swift.UInt32> + 4509 at playground359.swift:30
    frame #1: 0x000000011ae4538e $__lldb_expr359`top_level_code + 910 at playground359.swift:51
    frame #2: 0x000000011ae46831 $__lldb_expr359`main + 49 at <EXPR>:0
    frame #3: 0x000000010eedc510 RC6`get_field_types__XCPAppDelegate + 160
    frame #4: 0x000000010eedd081 RC6`reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_owned (@in ()) -> (@out ()) + 17
    frame #5: 0x000000010eedbcf1 RC6`partial apply forwarder for reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_owned (@in ()) -> (@out ()) + 81
    frame #6: 0x000000010eedd0b0 RC6`reabstraction thunk helper from @callee_owned (@in ()) -> (@out ()) to @callee_owned () -> (@unowned ()) + 32
    frame #7: 0x000000010eedd0e7 RC6`reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_unowned @objc_block () -> (@unowned ()) + 39
    frame #8: 0x000000010f524abc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #9: 0x000000010f51a805 CoreFoundation`__CFRunLoopDoBlocks + 341
    frame #10: 0x000000010f519fc3 CoreFoundation`__CFRunLoopRun + 851
    frame #11: 0x000000010f519a06 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #12: 0x000000010f5c62c1 CoreFoundation`CFRunLoopRun + 97
    frame #13: 0x000000010eed9dcd RC6`top_level_code + 3629
    frame #14: 0x000000010eedc53a RC6`main + 42
    frame #15: 0x00000001121f7145 libdyld.dylib`start + 1
Fabio Cardoso
  • 1,181
  • 1
  • 14
  • 37
Brayden G
  • 93
  • 7

2 Answers2

0

For i = 1, the result of

S[1] = S[0] + Q = 0xB7E15163 + 0x9E3779B9

is not representable as an UInt32, therefore the program aborts.

If you want the addition to "overflow" or "wrap-around" as in (Objective-)C, you have to specify an "overflow operator" &+ explictly:

S[i] = S[i-1] &+ Q

For more information, see "Overflow Operators" in "Advanced Operators" in the Swift documentation.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Ok, this makes sense. I have changed it to allow overflow but still come up with the same error. The code now shows that it loops 43 times but then gives the error again? – Brayden G Nov 12 '14 at 19:01
  • @BraydenG: I tested the code with that modification and it ran without problems. The loop executes only 2*20+3=43 times, so perhaps the problem occurs later? – Martin R Nov 12 '14 at 19:03
  • I commented out the rest of my code and it does work. The error is probably occurring later on. Thanks for the help. – Brayden G Nov 12 '14 at 19:08
-1

I've tested the program using all UInt64 and works well. Hope that's help ;)

sabi
  • 423
  • 2
  • 8