Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Last active April 6, 2019 01:53
Show Gist options
  • Save robertmryan/1dfe3d206da13e4c1de7f7be7b1068f1 to your computer and use it in GitHub Desktop.
Save robertmryan/1dfe3d206da13e4c1de7f7be7b1068f1 to your computer and use it in GitHub Desktop.
// consider
void process(long long * array, int arraySizeInBytes) {
long long value = 1;
for (long long i = 0; i < arraySizeInBytes / sizeof(long long); i++) {
value *= i + 1;
NSLog(@"%lld", array[i]);
array[i] = value;
}
}
@robertmryan
Copy link
Author

This works:

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        DispatchQueue.global().async {
            var a = [Int64](repeating: -1, count: 5)
            process(&a, Int32(a.count * MemoryLayout<Int64>.stride))

            print(a)
        }
    }

}

As does this:

class ViewController: NSViewController {

    var a = [Int64]()

    override func viewDidLoad() {
        super.viewDidLoad()

        DispatchQueue.global().async {
            self.a = [Int64](repeating: -1, count: 5)
            process(&self.a, Int32(self.a.count * MemoryLayout<Int64>.stride))

            print(self.a)
        }
    }

}

They both produce

2019-04-05 18:48:37.993089-0700 MyApp[5422:664763] -1
2019-04-05 18:48:37.993116-0700 MyApp[5422:664763] -1
2019-04-05 18:48:37.993125-0700 MyApp[5422:664763] -1
2019-04-05 18:48:37.993131-0700 MyApp[5422:664763] -1
2019-04-05 18:48:37.993138-0700 MyApp[5422:664763] -1
[1, 2, 6, 24, 120]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment