<div dir="ltr">Hey Chris,<div><br></div><div>So I work with a C library and Swift as well and have a couple of ideas for you depending on how you work with the array. Here are some tricks I have used when passing around arrays from C to Swift to ObjC and never had issues with it. Also, while passing in the first element of the tuple is fine since the first element has the same address as the entire array in C, I would just use the base tuple instead as it is more Swifty.</div><div><br></div><div>You will have to specify the result type of the functions but it is better than using several closures to get to where you want to work with stuff. Removing closures will also help you debug it and see if something else is going on because if you are passing in the first element all the way down and not modifying the pointer your logic should be correct.</div><div><br></div><div>If you don&#39;t mind making a deep copy into a native Swift array you can use a function like this to work with any C array:</div><div><br></div><div><br></div><div>func tupleValuesToArray&lt;T, U&gt;(_ tuple: T) -&gt; U {</div><div>    var toReturn: [U] = []</div><div>    for (_, value) in Mirror(reflecting: tuple).children {</div><div>        if let value = value as? U {</div><div>            toReturn.append(value)</div><div>        } else {</div><div>            fatalError(&quot;Failed to convert \(tuple) into an array&quot;)</div><div>        }</div><div>    }</div><div>    return toReturn</div><div>}</div><div><br></div><div>let swiftArray: [Float] = tupleValuesToArray(comeCArray)</div><div><br></div><div><br></div><div>If you need to mutate the array or if you just don&#39;t want to copy it and/or want to access it as a sequence you should use an Unsafe[Mutable]BufferPointer. You can convert to one with this function:</div><div><br></div><div><br></div><div>func tupleToUnsafe[Mutable]Buffer&lt;T, U&gt;(_ tuple: Unsafe[Mutable]Pointer&lt;T&gt; -&gt; Unsafe[Mutable]BufferPointer&lt;U&gt; {</div><div>    let mirror = Mirror(reflecting: tuple.poinee)</div><div>    return Unsafe[Mutable]BufferPointer&lt;U&gt;(start: Unsafe[Mutable]Pointer(OpaquePointer(tuple)), count: Int(mirror.children.count))</div><div>}</div><div><br></div><div>let buffer: UnsafeMutableBufferPointer&lt;Float&gt; = tupleToUnsafeMutableBuffer(&amp;cArrayAsTuple)<br></div><div><br></div><div><br></div><div>So there are a few things to point out with this function. You have to get around type checking by converting to an OpaquePointer and back in order to cast from T -&gt; U.. There are other solutions to get around the type checks but this is the one I often use even though I dislike it...</div><div><br></div><div>Anyways, once you have a buffer you should then be able to pass in the baseAddress (and count) of the buffer to ObjC and have it work fine so long as no pointer math is messing it up down inside the other parts of your code.</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 21, 2017 at 7:33 AM, Johannes Weiß via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Chris,<br>
<br>
Yes, he&#39;ll need to do<br>
<br>
withUnsafeMutablePointer(to: &amp;inReq.imageInfo.<wbr>transformation) { transformationTuplePtr in<br>
   withUnsafeMutablePointer(to: &amp;inReq.imageInfo.projection) { projectionTuplePtr in<br>
       transformationTuplePtr.<wbr>withMemoryRebound(to: Float.self, capacity: 16) { transformationArrayPtr in<br>
           projectionTuplePtr.<wbr>withMemoryRebound(to: Float.self, capacity: 16) { projectionArrayPtr in<br>
               ....imageReceived(..., transform: transformationArrayPtr, projection: projectionArrayPtr, ...)<br>
           }<br>
       }<br>
   }<br>
}<br>
<br>
if you need access to 5 of them, you&#39;ll need to nest deeper, apologies.<br>
<br>
However, there&#39;s talk on swift-evolution about getting fixed-size arrays into Swift which would help here. But there&#39;s no full proposal yet AFAIK.<br>
<br>
<br>
-- Johannes<br>
<div class="HOEnZb"><div class="h5"><br>
&gt; On 21 Sep 2017, at 2:27 pm, Chris McIntyre &lt;<a href="mailto:nothingwasdelivered@gmail.com">nothingwasdelivered@gmail.com</a><wbr>&gt; wrote:<br>
&gt;<br>
&gt; Sorry to jump in here, and maybe I’m missing something obvious, but in your example Rick is two levels of closure deep and only has access to one of the arrays in his struct. In this case there is another array of floats he needs as an argument to his method call. Will he need to go through the same steps, going two levels of closure deeper, before he has access to both struct members?<br>
&gt;<br>
&gt; This doesn’t seem to scale very well. What about a struct with 5 arrays?<br>
&gt; --<br>
&gt; Chris McIntyre<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;&gt; On Sep 21, 2017, at 6:05 AM, Johannes Weiß via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Hi Rick,<br>
&gt;&gt;<br>
&gt;&gt;&gt; On 21 Sep 2017, at 1:03 am, Rick Mann via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; I&#39;ve got Swift code wrapping a C API. One of the C structs the API uses looks like this:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; typedef struct {<br>
&gt;&gt;&gt;   size_t size;<br>
&gt;&gt;&gt;   float transformation[16];<br>
&gt;&gt;&gt;   float projection[16];<br>
&gt;&gt;&gt;   uint32_t width;<br>
&gt;&gt;&gt;   uint32_t height;<br>
&gt;&gt;&gt; } image_info_t;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; In Swift, the two array members are 16-tuples of floats. Eventually, the Swift code calls an Objective-C delegate, passing a tuple to an array:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; self.delegate?<br>
&gt;&gt;&gt;   .imageReceived(self,<br>
&gt;&gt;&gt;                   index: inReq.imageIndex,<br>
&gt;&gt;&gt;                   data: data,<br>
&gt;&gt;&gt;                   width: width,<br>
&gt;&gt;&gt;                   height: height,<br>
&gt;&gt;&gt;                   transform: &amp;inReq.imageInfo.<wbr>transformation.0,<br>
&gt;&gt;&gt;                   projection: &amp;inReq.imageInfo.projection.0)<br>
&gt;&gt;<br>
&gt;&gt; these are illegal. You&#39;re getting a pointer to _only_ the first element (&amp;inReq.imageInfo.<wbr>transformation.0) but the library will then read 16 elements from there.<br>
&gt;&gt;<br>
&gt;&gt; Something like this should work (and is legal):<br>
&gt;&gt;<br>
&gt;&gt; withUnsafeMutablePointer(to: &amp;inReq.imageInfo.<wbr>transformation) { tupleOfFloatsPtr in<br>
&gt;&gt;    tupleOfFloatsPtr.<wbr>withMemoryRebound(to: Float.self, capacity: 16) { arrayOfFloatsPtr in<br>
&gt;&gt;        ....imageReceived(..., transform: arrayOfFloatsPtr, ...)<br>
&gt;&gt;    }<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt; that&#39;s legal because now we&#39;re capturing a pointer to the whole tuple and therefore Swift guarantees the memory to be in C standard layout.<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; This seems to have been working fine for a long time, but we&#39;re running into a crash in some cases, and so I&#39;ve been digging into it. I turned on the address sanitizer, and I consistently get it to trip in some C++ code that eventually gets called with a float* to that transform tuple.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Now, in Swift, the values of the tuple look great:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; ▿ 16 elements<br>
&gt;&gt;&gt; - .0 : 0.548282921<br>
&gt;&gt;&gt; - .1 : 0.821425974<br>
&gt;&gt;&gt; - .2 : -0.156990349<br>
&gt;&gt;&gt; - .3 : 0.0<br>
&gt;&gt;&gt; - .4 : -0.277842015<br>
&gt;&gt;&gt; - .5 : 0.00185899995<br>
&gt;&gt;&gt; - .6 : -0.960625231<br>
&gt;&gt;&gt; - .7 : 0.0<br>
&gt;&gt;&gt; - .8 : -0.788789928<br>
&gt;&gt;&gt; - .9 : 0.570312977<br>
&gt;&gt;&gt; - .10 : 0.229245573<br>
&gt;&gt;&gt; - .11 : 0.0<br>
&gt;&gt;&gt; - .12 : 0.0475889929<br>
&gt;&gt;&gt; - .13 : -0.0323969983<br>
&gt;&gt;&gt; - .14 : -0.0113521963<br>
&gt;&gt;&gt; - .15 : 1.0<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; I can see this by clicking up the stack a couple frames to the Swift code. But the Objective-C(++) delegate code, that gets passed a float*, sees this (BTW, I can&#39;t figure out how to get the debugger, Xcode or command line, to display inTransform as an array of 16 floats). It&#39;s clearly not the same array.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; (lldb) po inTransform[0]<br>
&gt;&gt;&gt; 0.548282921<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; (lldb) po inTransform[1]<br>
&gt;&gt;&gt; 0.0343905278<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; (lldb) po inTransform[2]<br>
&gt;&gt;&gt; -0<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; (lldb) po inTransform[3]<br>
&gt;&gt;&gt; 0<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; (lldb) po inTransform[4]<br>
&gt;&gt;&gt; 0<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; (lldb) po inTransform[5]<br>
&gt;&gt;&gt; 0<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; (lldb) po inTransform[6]<br>
&gt;&gt;&gt; 95488384<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; (lldb) po inTransform[14]<br>
&gt;&gt;&gt; 0.<wbr>000000000000000000000000000000<wbr>00000000000000560519386<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; (lldb) po inTransform[15]<br>
&gt;&gt;&gt; 0<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; The address sanitizer spits out this:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; ==============================<wbr>==============================<wbr>=====<br>
&gt;&gt;&gt; ==1358==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x00014d3ea224 at pc 0x000100eb9a84 bp 0x00016fd84bd0 sp 0x00016fd84bc8<br>
&gt;&gt;&gt; READ of size 4 at 0x00014d3ea224 thread T0<br>
&gt;&gt;&gt;   #0 0x100eb9a83 in &lt;redacted&gt;::addImage(void const*, int, int, int, float const*, float const*) (&lt;redacted&gt;:arm64+0x100e41a83)<br>
&gt;&gt;&gt;   #1 0x10040b72b in -[&lt;redacted&gt; imageReceived:index:data:<wbr>width:height:transform:<wbr>projection:] (&lt;redacted&gt;:arm64+0x10039372b)<br>
&gt;&gt;&gt;   #2 0x10045d037 in function signature specialization &lt;Arg[0] = Owned To Guaranteed, Arg[1] = Owned To Guaranteed, Arg[2] = Owned To Guaranteed and Exploded&gt; of closure #1 () -&gt; () in &lt;redacted&gt;.imageDownloaded(&lt;<wbr>redacted&gt;.<wbr>DownloadImageRequest, index: Swift.Int) -&gt; () (&lt;redacted&gt;:arm64+0x1003e5037)<br>
&gt;&gt;&gt;   #3 0x100442bbb in closure #1 () -&gt; () in &lt;redacted&gt;.imageDownloaded(&lt;<wbr>redacted&gt;.<wbr>DownloadImageRequest, index: Swift.Int) -&gt; () (&lt;redacted&gt;:arm64+0x1003cabbb)<br>
&gt;&gt;&gt;   #4 0x10043e7fb in reabstraction thunk helper from @callee_owned () -&gt; () to @callee_unowned @convention(block) () -&gt; () (&lt;redacted&gt;:arm64+0x1003c67fb)<br>
&gt;&gt;&gt;   #5 0x103ba9d5b in __wrap_dispatch_async_block_<wbr>invoke (/var/containers/Bundle/<wbr>Application/B3E557CA-5ED9-<wbr>4673-8B07-37A0F2DA472B/&lt;<wbr>redacted&gt;.app/Frameworks/<wbr>libclang_rt.asan_ios_dynamic.<wbr>dylib:arm64+0x4dd5b)<br>
&gt;&gt;&gt;   #6 0x105f1549b in _dispatch_call_block_and_<wbr>release (/usr/lib/system/<wbr>introspection/libdispatch.<wbr>dylib:arm64+0x149b)<br>
&gt;&gt;&gt;   #7 0x105f1545b in _dispatch_client_callout (/usr/lib/system/<wbr>introspection/libdispatch.<wbr>dylib:arm64+0x145b)<br>
&gt;&gt;&gt;   #8 0x105f1a04f in _dispatch_main_queue_callback_<wbr>4CF (/usr/lib/system/<wbr>introspection/libdispatch.<wbr>dylib:arm64+0x604f)<br>
&gt;&gt;&gt;   #9 0x181d43f1f in &lt;redacted&gt; (/System/Library/Frameworks/<wbr>CoreFoundation.framework/<wbr>CoreFoundation:arm64+0xe9f1f)<br>
&gt;&gt;&gt;   #10 0x181d41afb in &lt;redacted&gt; (/System/Library/Frameworks/<wbr>CoreFoundation.framework/<wbr>CoreFoundation:arm64+0xe7afb)<br>
&gt;&gt;&gt;   #11 0x181c622d7 in CFRunLoopRunSpecific (/System/Library/Frameworks/<wbr>CoreFoundation.framework/<wbr>CoreFoundation:arm64+0x82d7)<br>
&gt;&gt;&gt;   #12 0x183af3f83 in GSEventRunModal (/System/Library/<wbr>PrivateFrameworks/<wbr>GraphicsServices.framework/<wbr>GraphicsServices:arm64+0xaf83)<br>
&gt;&gt;&gt;   #13 0x18b20e87f in UIApplicationMain (/System/Library/Frameworks/<wbr>UIKit.framework/UIKit:arm64+<wbr>0x7387f)<br>
&gt;&gt;&gt;   #14 0x100122dc3 in main (&lt;redacted&gt;:arm64+0x1000aadc3)<br>
&gt;&gt;&gt;   #15 0x18178656b in &lt;redacted&gt; (/usr/lib/system/libdyld.<wbr>dylib:arm64+0x156b)<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Address 0x00014d3ea224 is located in stack of thread T0 at offset 36 in frame<br>
&gt;&gt;&gt;   #0 0x10045cd57 in function signature specialization &lt;Arg[0] = Owned To Guaranteed, Arg[1] = Owned To Guaranteed, Arg[2] = Owned To Guaranteed and Exploded&gt; of closure #1 () -&gt; () in &lt;redacted&gt;.imageDownloaded(&lt;<wbr>redacted&gt;.<wbr>DownloadImageRequest, index: Swift.Int) -&gt; () (&lt;redacted&gt;:arm64+0x1003e4d57)<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; This frame has 4 object(s):<br>
&gt;&gt;&gt;   [32, 36) &#39;&#39; &lt;== Memory access at offset 36 overflows this variable<br>
&gt;&gt;&gt;   [48, 208) &#39;&#39;<br>
&gt;&gt;&gt;   [272, 276) &#39;&#39;<br>
&gt;&gt;&gt;   [288, 448) &#39;&#39;<br>
&gt;&gt;&gt; HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext<br>
&gt;&gt;&gt;     (longjmp and C++ exceptions *are* supported)<br>
&gt;&gt;&gt; SUMMARY: AddressSanitizer: stack-buffer-overflow (&lt;redacted&gt;:arm64+0x100e41a83) in &lt;redacted&gt;::addImage(void const*, int, int, int, float const*, float const*)<br>
&gt;&gt;&gt; Shadow bytes around the buggy address:<br>
&gt;&gt;&gt; 0x0001302dd3f0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5<br>
&gt;&gt;&gt; 0x0001302dd400: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5<br>
&gt;&gt;&gt; 0x0001302dd410: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5<br>
&gt;&gt;&gt; 0x0001302dd420: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5<br>
&gt;&gt;&gt; 0x0001302dd430: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5<br>
&gt;&gt;&gt; =&gt;0x0001302dd440: f1 f1 f1 f1[04]f2 00 00 00 00 00 00 00 00 00 00<br>
&gt;&gt;&gt; 0x0001302dd450: 00 00 00 00 00 00 00 00 00 00 f2 f2 f2 f2 f2 f2<br>
&gt;&gt;&gt; 0x0001302dd460: f2 f2 04 f2 00 00 00 00 00 00 00 00 00 00 00 00<br>
&gt;&gt;&gt; 0x0001302dd470: 00 00 00 00 00 00 00 00 f3 f3 f3 f3 f3 f3 f3 f3<br>
&gt;&gt;&gt; 0x0001302dd480: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5<br>
&gt;&gt;&gt; 0x0001302dd490: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5<br>
&gt;&gt;&gt; Shadow byte legend (one shadow byte represents 8 application bytes):<br>
&gt;&gt;&gt; Addressable:           00<br>
&gt;&gt;&gt; Partially addressable: 01 02 03 04 05 06 07<br>
&gt;&gt;&gt; Heap left redzone:       fa<br>
&gt;&gt;&gt; Freed heap region:       fd<br>
&gt;&gt;&gt; Stack left redzone:      f1<br>
&gt;&gt;&gt; Stack mid redzone:       f2<br>
&gt;&gt;&gt; Stack right redzone:     f3<br>
&gt;&gt;&gt; Stack after return:      f5<br>
&gt;&gt;&gt; Stack use after scope:   f8<br>
&gt;&gt;&gt; Global redzone:          f9<br>
&gt;&gt;&gt; Global init order:       f6<br>
&gt;&gt;&gt; Poisoned by user:        f7<br>
&gt;&gt;&gt; Container overflow:      fc<br>
&gt;&gt;&gt; Array cookie:            ac<br>
&gt;&gt;&gt; Intra object redzone:    bb<br>
&gt;&gt;&gt; ASan internal:           fe<br>
&gt;&gt;&gt; Left alloca redzone:     ca<br>
&gt;&gt;&gt; Right alloca redzone:    cb<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; So, how do I properly go back and forth between C and Swift code with pointers to arrays of floats?<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Thanks,<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; --<br>
&gt;&gt;&gt; Rick Mann<br>
&gt;&gt;&gt; <a href="mailto:rmann@latencyzero.com">rmann@latencyzero.com</a><br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt;&gt; swift-users mailing list<br>
&gt;&gt;&gt; <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
&gt;&gt;<br>
&gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt; swift-users mailing list<br>
&gt;&gt; <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
&gt;<br>
<br>
______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div>