unsafe Blocks
The unsafe { ... } block relaxes safety checks within it, allowing potentially unsafe operations to be performed. All calls to foreign functions, @C functions, and CFunc type variables must be made within an unsafe context.
In addition to unsafe blocks, you can also mark an entire function as unsafe—this way all operations within the function body are by default in an unsafe context. However, places that call this unsafe function also need to be in an unsafe context.
unsafe isolates risk within a clearly defined scope, reminding developers that this code requires extra care. C code may cause null pointer dereferences, memory leaks, buffer overflows, and other issues that the Cangjie compiler cannot check.
unsafe doesn't turn off all checks, it only allows specific operations related to C interoperability. Other safety mechanisms remain effective.
The example below demonstrates three uses of unsafe: unsafe blocks, unsafe functions, and unsafe blocks containing multiple statements. Try moving the call on line 20 outside the unsafe block to observe the compilation error.