<div dir="ltr"><span style="font-size:12.8px">Hi,</span><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I just moved to Xcode 8 (actually, it upgraded itself without me realizing it, but anyway), and some previously working Swift 2.2 code now doesn't work. I'm having trouble migrating.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">The code in question is supposed to declare, but not allocate, a block of memory. A void** to that block of memory was then passed to a C function (cudaMalloc to be specific) which then allocated memory on the GPU. The swift 2.2 code that worked was like this:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">var data_on_device : UnsafeMutablePointer<Void></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">...</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">self.byteCount = count * sizeof(CFloat)</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">self.data_on_device = nil </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">let status = cudaMalloc(&data_on_device, self.byteCount) </blockquote><div><br></div><div>Then elsewhere in the code I would convert this pointer to its appropriate typed version:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="color:rgb(0,134,179);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre-wrap">UnsafeMutablePointer</span><span style="color:rgb(167,29,93);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre-wrap"><</span><span style="color:rgb(0,134,179);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre-wrap">Float</span><span style="color:rgb(167,29,93);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre-wrap">></span><span style="color:rgb(51,51,51);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre-wrap">(<wbr>foo</span><span style="color:rgb(167,29,93);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre-wrap">.</span><span style="color:rgb(51,51,51);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre-wrap">data_on_device)</span> </blockquote><div><br></div><div>Can anyone help me get the equivalent functionality in Swift 3?</div><div><br></div><div>The closest I've been able to come is the following, which (a) was very non-obvious and took a lot of trial and error to find, and (b) is ugly.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">var data_on_device : UnsafeMutablePointer<Float><br>let data_on_device_void_pointer : UnsafeMutableRawPointer<br>...<br>self.byteCount = count * MemoryLayout<Float>.size<br>self.data_on_device = UnsafeMutablePointer<Float>.<wbr>allocate(capacity:0)<br>self.data_on_device_void_<wbr>pointer = UnsafeMutableRawPointer(data_<wbr>on_device)<br>var pointer_as_optional : UnsafeMutableRawPointer? = data_on_device_void_pointer<br>let pointer : UnsafeMutablePointer<<wbr>UnsafeMutableRawPointer?> = UnsafeMutablePointer<<wbr>UnsafeMutableRawPointer?>(&<wbr>pointer_as_optional)<br>let status = cudaMalloc(pointer, self.byteCount)</blockquote><div><br></div><div> For reference, the code and git history is here: <a href="https://github.com/dowobeha/mt-marathon-swift-c/blob/master/main.swift" target="_blank">https://github.com/<wbr>dowobeha/mt-marathon-swift-c/<wbr>blob/master/main.swift</a></div><div><br></div><div>Thanks,</div><div>Lane</div></div>
</div>