Talk about unsafe c
Asked on 06/16/2025
1 search
At WWDC 2025, Apple introduced several features and tools to enhance the safety of using C and C++ with Swift, particularly focusing on addressing the challenges of unsafe constructs in C and C++.
Key Points on Unsafe C and C++:
-
Unsafe Pointers: C and C++ pointers are inherently unsafe because they can lead to security and stability issues like buffer overflows and use-after-free errors. Swift imports these pointers as unsafe types, such as
UnsafeMutablePointer
, to signal the lack of safety guarantees. -
Strict Memory Safety: Swift 6.2 introduces a new compiler mode called Strict Memory Safety. This mode alerts developers to any unsafe code and provides notes explaining the reasoning. It is not enabled by default but can be turned on in Xcode to help identify and address unsafe constructs (Safely mix C, C++, and Swift).
-
Annotations for Safety: Developers can add annotations to C and C++ code to convey missing information, allowing Swift to call these functions safely. This includes annotating functions to express bounds and lifetime information, which helps Swift treat unsafe pointers as safe spans (Safely mix C, C++, and Swift).
-
Bound Safety Tools: Apple has developed tools to enhance bound safety in C and C++. These tools include enabling bound safety checks in Xcode and using language extensions to ensure bounds are respected in C code (Safely mix C, C++, and Swift).
-
Swift Spans: Swift introduces a new type called
Span
, which acts like a pointer but with built-in safety features. This type helps prevent common pointer-related mistakes, such as out-of-bounds access (Safely mix C, C++, and Swift).
These efforts are part of Apple's ongoing work to make C and C++ code safer when used in conjunction with Swift, acknowledging that while C and C++ can never be as safe as Swift, they can be made safer with the right tools and practices.

Safely mix C, C++, and Swift
Learn how to mix C, C++, and Swift while improving the safety of your apps. We’ll show you how to find where unsafe C and C++ APIs are called in your Swift code, how to call them more safely, and how to make your app’s existing C and C++ code safer by default.

What’s new in Swift
Join us for an update on Swift. We’ll talk about workflow improvements that make you more productive, and new and modernized library APIs for fundamental programming tasks. We’ll show examples of Swift adoption throughout more layers of the software stack. Finally, we’ll explore new language features for both improving approachability of concurrency, and achieving peak performance when you need it.

Improve memory usage and performance with Swift
Discover ways to improve the performance and memory management of your Swift code. We’ll explore ways to refine your code – from making high-level algorithmic changes to adopting the new InlineArray and Span types for finer control over memory and allocations.