Tell me about unsafe c

Asked on 06/16/2025

2 searches

At WWDC 2025, Apple introduced several features and tools to enhance the safety of using C and C++ with Swift, particularly focusing on unsafe constructs. In the session titled "Safely mix C, C++, and Swift," they discussed how Swift, which is safe by default, can still interact with unsafe C and C++ constructs, especially when dealing with pointers.

One of the key features introduced is the Strict Memory Safety mode in Swift 6.2. This mode is not enabled by default but can be turned on to alert developers to any unsafe code, providing notes that explain the reasoning behind these alerts. This is particularly useful for applications that handle sensitive data and need to ensure security (Safely mix C, C++, and Swift).

Additionally, Swift 6.2 introduces a new safe pointer type called Span, which provides the benefits of pointers while automatically preventing common mistakes such as accessing memory out of bounds or using memory after it has been freed. This is achieved by adding annotations to C and C++ code, allowing Swift to safely call functions that use these pointers (Safely mix C, C++, and Swift).

For developers working with C and C++ code, Apple has also developed tools to enhance bound safety, such as enabling bound safety extensions in Xcode, which help identify and correct unsafe pointer usage (Safely mix C, C++, and Swift).

These efforts are part of Apple's broader initiative to make C and C++ code safer when used in conjunction with Swift, acknowledging that while it's not possible to make these languages as safe as Swift, significant improvements can be made.