[swift-users] C Pointers and Memory

Lou Zell lzell11 at gmail.com
Sat Jul 30 07:30:18 CDT 2016


On Fri, Jul 29, 2016 at 8:40 PM, Chris McIntyre via swift-users <
swift-users at swift.org> wrote:

> Hi guys,
>
> I’m having issues with Swift pointers. I feel like the Interactive With C
> APis document only gets you half way there.
>
> For example, look at this from the docs
>
> If you have declared a function like this one:
>
>    1. func takesAMutablePointer(x: UnsafeMutablePointer<Float>) {
>    2. // ...
>    3. }
>
> You can call it in any of the following ways:
>
>    1. var x: Float = 0.0
>    2. var p: UnsafeMutablePointer<Float> = nil
>    3. var a: [Float] = [1.0, 2.0, 3.0]
>    4. takesAMutablePointer(nil)
>    5. takesAMutablePointer(p)
>    6. takesAMutablePointer(&x)
>    7. takesAMutablePointer(&a)
>
> Seem simple enough. But then I was trying to figure out Core Data
> validation, which takes an AutoreleasingUnsafeMutablePointer<AnyObject?>
> and I can’t figure out what to pass to it.
>
> I tried to create a simple test in a Playground:
>
> func takesAPointer(p: AutoreleasingUnsafeMutablePointer<AnyObject?>){
>
>     return
>
> }
>
>
> var myString = "Hello"
>
>
> takesAPointer(p: &myString)
>

Try this:

import Foundation
func takesAPointer(p: AutoreleasingUnsafeMutablePointer<AnyObject?>){
    return
}
let myString : NSString? = "Hello"
var obj : AnyObject? = myString
takesAPointer(&obj)



>
> Then I get an error stating 'Cannot pass immutable type “AnyObject?” as
> inout argument’.
>
> Everything seems to match the example from the docs. I have a var (so it
> should be mutable) and I’m using the ampersand, but still I’m getting an
> error.
>
> Another problem. I have a specific byte pattern I want to create. For
> arguments sake, lets call it 0x123ABC, and I have it as an Int. I want to
> access the individual bytes (i.e. 12, 3A, BC).
>

Is something like this what you're looking for:
let a = UInt16((0x123ABC >> 16) & 0xFF)
let b = UInt16((0x123ABC >> 8) & 0xFF)
print(String(a, radix: 16))
print(String(b, radix: 16))


>
> The struct reference for UnsafePointer<T> doesn’t talk much about
> initializing it. Most of the initializers take a pointer. I tried the
> init(_ bitPattern:) initializer, and was able to create a pointer, but it
> seemed to point to the address 0x123ABC rather than the address *of*
> 0x123ABC. I tried creating a buffer with malloc, and it gives me an
> UnsafeMutablePointer  but now I can’t figure out how to copy my bytes to
> this buffer.
>
> So clearly there’s something I’m just not grocking about Swift pointers.
> Does anyone know of a more remedial tutorial that is updated for Swift 3?
> I’d like to continue to work in pure Swift, but it just isn’t clicking.
>
> --
> Chris McIntyre
>
>
>
>
> On Jul 29, 2016, at 6:11 AM, James Campbell via swift-users <
> swift-users at swift.org> wrote:
>
> No I haven't thats a big help thank you !
>
> *___________________________________*
>
> *James⎥Head of Trolls*
>
> *james at supmenow.com <james at supmenow.com>⎥supmenow.com
> <http://supmenow.com/>*
>
> *Sup*
>
> *Runway East *
>
> *10 Finsbury Square*
>
> *London*
>
> * EC2A 1AF *
>
> On 29 July 2016 at 10:40, Zhao Xin <owenzx at gmail.com> wrote:
>
>> Have you read
>> https://developer.apple.com/library/tvos/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-ID17
>>  ?
>>
>> Zhaoxin
>>
>> On Fri, Jul 29, 2016 at 4:55 PM, James Campbell via swift-users <
>> swift-users at swift.org> wrote:
>>
>>> ​Do you know of any resources to brush up on the pointer aspect of swift
>>> ? ​
>>>
>>> *___________________________________*
>>>
>>> *James⎥Head of Trolls*
>>>
>>> *james at supmenow.com <james at supmenow.com>⎥supmenow.com
>>> <http://supmenow.com/>*
>>>
>>> *Sup*
>>>
>>> *Runway East *
>>>
>>> *10 Finsbury Square*
>>>
>>> *London*
>>>
>>> * EC2A 1AF *
>>>
>>> On 29 July 2016 at 09:10, Dmitri Gribenko <gribozavr at gmail.com> wrote:
>>>
>>>> On Fri, Jul 29, 2016 at 12:55 AM, James Campbell <james at supmenow.com>
>>>> wrote:
>>>> > So this:
>>>> >
>>>> > if let data = someArrayGeneratingFunction() {
>>>> >   cFunction(UnsafeMutablePointer(data))
>>>> > }
>>>> >
>>>> > Has issues with the array passed to c getting corrupted, but this
>>>> doesn't:
>>>> >
>>>> > let data = someArrayGeneratingFunction()
>>>> >
>>>> > if let data = data {
>>>> >   cFunction(UnsafeMutablePointer(data))
>>>> > }
>>>>
>>>> Neither piece of code is guaranteed to work.  (You are just getting
>>>> lucky that the second one happens to work.)  Array-to-pointer
>>>> conversion only extends the lifetime of the array until the immediate
>>>> function call returns.  So after UnsafeMutablePointer(data) returns,
>>>> the array can be freed.
>>>>
>>>> Use someArrayGeneratingFunction.withUnsafeMutableBuffer { ... } instead.
>>>>
>>>> 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>*/
>>>>
>>>
>>>
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-users
>>>
>>>
>>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160730/088edc90/attachment.html>


More information about the swift-users mailing list