[swift-users] Mapping C semantics to Swift
Ramakrishna Mallireddy
ramakrishna.malli at gmail.com
Thu May 19 14:15:08 CDT 2016
An working example,
let fm = NSFileManager.defaultManager()
var mh = mach_header() // Defined as C struct, <mach-o/loader.h>
var lc = load_command() // Defined as C struct <mach-o/loader.h>
var location = 0
if let data = fm.contentsAtPath("home/user/projects/some.o") {
data.getBytes(&mh, length: sizeof(mach_header))
location += sizeof(mach_header)
data.getBytes(&lc, range: NSRange(location: location, length: sizeof(
load_command)) )
location += sizeof(load_command)
}
-Rk
On Thu, May 19, 2016 at 10:52 PM, Joe Groff via swift-users <
swift-users at swift.org> wrote:
>
> > On May 19, 2016, at 10:00 AM, Ken Burgett via swift-users <
> swift-users at swift.org> wrote:
> >
> > I would like to know if a struct in Swift has any guarantee of
> contiguity, like a struct in C. I am attempting to port a C program that
> makes many assumptions about the underlying semantics of C structs, and
> does things like overlaying an array over the struct using a cast, and then
> performs and cryptographic hashing of the entire struct by treating it as
> an array.
> >
> > 1. I a Swift struct contiguous?
> >
> > 2. If 1 above is true, can I somehow cast an array over the struct?
>
> Swift structs have unspecified layout. If you depend on a specific layout,
> you should define the struct in C and import it into Swift for now.
>
> -Joe
> _______________________________________________
> 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/20160520/4979b634/attachment.html>
More information about the swift-users
mailing list