Safe Division in C with Maybe Monad

2025-08-11

This article details the implementation of type and bounds-safe generic containers in C. The author introduces a `Maybe` type, inspired by Haskell, to handle functions that might return no value (e.g., division by zero). A safe division function is created using macros to define `Maybe`, handling zero division and the edge case of dividing the minimum representable integer by -1. GCC assembly code is analyzed to verify the function's safety. The author concludes by noting the limitations of this approach for proving the complete safety of C programs.

Read more
Development

Generic Containers in C: A Safe and Efficient vec Implementation

2025-07-26

This article details the implementation of a type- and bounds-safe generic container, `vec`, in C. `vec` is essentially a resizable array, dynamically growing using `realloc`. The author explains the implementation, including the `vec_push` function and error handling for memory allocation. Two improved interfaces, `vec_push_cap` and `vec_push_auto`, are explored for performance optimization. Bounds safety is discussed, along with interoperability with traditional C arrays via the `vec2array` macro.

Read more
Development Generic Programming