Advanced Swift: Unsafe Memory Access
Did you know you can call C-malloc straight from Swift? Swift lets access the machine at its lowest level. This course will show you how. By Ray Fix.
This is part of the Advanced Swift learning path. View path.
Who is this for?
This course is for developers interested in getting a deeper understanding of the Swift language. It expects you to be comfortable with the basic syntax of Swift and to have perhaps written an app or two. You should be familiar with basic types in the Swift standard library.
This course focuses on:
- Best practices for memory safety
- Pointer types
- Unsafe Swift and why not to use it
Swift is safe by default. Sometimes you may need to forgo this safety for extra performance or to interact with an unsafe language such as C.
When you are working with with unsafe code, you need to concern yourself with the size of objects in memory.
In this video we will explore the fundamentals of unsafe memory access, and learn about the types involved.
Rules to Follow
When you are using memory unsafely, the compiler no longer provides guarantees and you must follow basic rules to avoid undefined behavior.
It's time for a challenge! See if you can use your unsafe Swift skills to wrap a data compression C-API.
Unsafe Swift lets you access raw memory. While it requires extra diligence, you can keep such places small and isolated.