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

Sergey Kuratov ksubox.swift at gmail.com
Thu Feb 11 02:13:15 CST 2016


>> Can I define similar for my Swift classes ?

>No.  What would be your use case?

I also could have my Swift object with several members, but when send to
native part would like to send address exactly one of members.
Something like this:

// Swift part

class ABlock {

var channels = 2

var samplerate = 44100

var  buffer : UnsafeMutablePointer<Int16>

init() {

buffer = UnsafeMutablePointer<Int16>.alloc(2048)

}

deinit {

buffer.dealloc(2048)

}

}

var block = ABlock
generate(block) <- here I would like to send address of ABlock.buffer to
native part

// C part
void generate(int16_t *buf) {
...
}


On Thu, Feb 11, 2016 at 4:54 PM, Dmitri Gribenko <gribozavr at gmail.com>
wrote:

> Re-adding swift-users back.
>
> On Wed, Feb 10, 2016 at 11:49 PM, Sergey Kuratov <ksubox.swift at gmail.com>
> wrote:
> > Thank you, Dmitri!
> >
> > I wonder how this automatic transformation to unsafe pointer happens (I
> > guess elements buffer has some offset from array object memory) ?
>
> The compiler knows how to extract the pointer from the array, and how
> to extend its lifetime to ensure it is not deallocated before the C
> function returns.
>
> > Can I define similar for my Swift classes ?
>
> No.  What would be your usecase?
>
> 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>*/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160211/bead526f/attachment.html>


More information about the swift-users mailing list