![]() In this section, you’ll use unsafe Swift pointers to store and load two integers. You’ll learn more about them in the following sections. In total, there are eight pointer combinations. Unsafe Swift pointers use a predictable naming scheme that describes the pointers’ traits: mutable or immutable, raw or typed, buffer style or not. This communicates intent better, is less error-prone and avoids undefined behavior. You always want to use the most appropriate pointer type for your purpose. Swift contains almost a dozen pointer types, each with different capabilities and purposes. Swift doesn’t offer just a single UnsafePointer type that accesses memory in an unstructured way, like C’s char * does. When done incorrectly, this could lead to undefined behavior, not just a predictable crash. The extra typing may seem annoying, but it reminds you that you’re accessing memory that the compiler isn’t checking. Types that involve direct memory access get an unsafe prefix, so the pointer type name is UnsafePointer. If you want to explore memory layout in greater detail, check out Mike Ash’s excellent talk, Exploring Swift Memory Layout. To see how the layout differs for class versus struct, add the following:Ĭlasses are reference types, so MemoryLayout reports the size of a reference: Eight bytes. Given that, the best Swift can do is pack at an interval of eight bytes. That’s because its alignment requires it to be on 4-byte boundaries. That’s because each EmptyStruct you create has to have a unique memory address, even though its size is zero.įor SampleStruct, the size is five but the stride is eight. It can exist at any address since alignment is one and all numbers are evenly divisible by one. ![]() Next, look at the layout of some user-defined structs by adding the following to the playground: For these basic types, the size is the same as the stride. When you pack a bunch of Int16s together, they pack at an interval of stride. That means it has to start on even addresses - that is, addresses divisible by two.įor example, it’s legal to allocate an Int16 at address 100, but not at 101 - an odd number violates the required alignment. It determines the size, alignment and stride of each specified Type and returns a number in bytes.įor example, an Int16 is two bytes in size and has an alignment of two as well. MemoryLayout is a generic type evaluated at compile time. MemoryLayout.stride // returns 8 (on 64-bit) MemoryLayout.alignment // returns 8 (on 64-bit) MemoryLayout.size // returns 8 (on 64-bit) To see this in action, you’ll use MemoryLayout to tell you the size and alignment of components of some native Swift types. On a 64-bit system, for example, a word is 8 bytes or 64 bits. Processors can also efficiently access words of memory, which are typically more than one byte. The smallest addressable unit of storage is a byte, which usually consists of eight bits.Įight-bit bytes can store values from 0-255. You can visualize memory as a series of boxes - billions of boxes, actually - each containing a number.Įach box has a unique memory address. Unsafe Swift works directly with the memory system. Exploring Memory Layout With Unsafe Swift Since all the code in this tutorial is platform-agnostic, you may select any platform. Start by opening the UnsafeSwift playground. It uses unsafe Swift, but hides that detail from users. In the final playground, you’ll create a platform-independent alternative to arc4random to generate random numbers.In the second, you’ll take a low-level C API that performs streaming data compression and wrap it with a Swifty interface.In the first playground, you’ll use several short snippets of code to explore memory layout.This tutorial consists of three empty Swift playgrounds: ![]() Getting Startedĭownload the begin project by clicking the Download Materials button at the top or bottom of the tutorial. C experience is beneficial but not necessary. If you need to brush up on your skills, please check out our iOS and Swift for Beginners series. Note: While this is an advanced topic, you’ll be able to follow along if you have reasonable competency in Swift.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |