[swift-users] Using withUnsafePointer on char arrays within C structs

Martin R martinr448 at gmail.com
Thu Feb 23 04:31:21 CST 2017

Is that simplified guaranteed to work? The Swift book says that

> As an optimization, when the argument is a value stored at a physical address in memory, the same memory location is used both inside and outside the function body.

but also 

> Write your code using the model given by copy-in copy-out, without depending on the call-by-reference optimization, so that it behaves correctly with or without the optimization.

If `tmp.0` is copied to a separate location then `String(cString: &tmp.0)` would not work correctly, or would it?

Thank you in advance,

> On 23 Feb 2017, at 09:33, Quinn The Eskimo! via swift-users <swift-users at swift.org> wrote:
> On 22 Feb 2017, at 22:16, Russell Finn via swift-users <swift-users at swift.org> wrote:
>> … is (2) the best I can do for now?
> Yes.
> btw You can simplify the code to this:
>    var description: String {
>        var tmp = self.path
>        return String(cString: &tmp.0)
>    }
> but it still needs that copy.
>> And is this the sort of issue that the Ownership Manifesto is designed to address?
> I’m not sufficiently up to speed on that topic to answer this.  Sorry.
> Share and Enjoy
> --
> Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
> Apple Developer Relations, Developer Technical Support, Core OS/Hardware
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

More information about the swift-users mailing list