[swift-users] How to get UnsafePointer or UnsafeMutablePointer from ContiguousArray?

Dmitri Gribenko gribozavr at gmail.com
Thu Feb 11 01:40:43 CST 2016


On Wed, Feb 10, 2016 at 11:09 PM, Sergey Kuratov via swift-users
<swift-users at swift.org> wrote:
> Hello!
>
> I got some problem with ContiguousArray<->C interaction - I can't get
> address of array buffer to process in native code.

Just pass the array to your C function that accepts an unsafe pointer.
The compiler will perform the conversion automatically.

You can also use the withUnsafeMutableBufferPointer method, but make
sure to use the pointer only inside of the closure.  Don't try to get
a pointer to array contents and persist it.  The pointer will become
invalid.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-users mailing list