From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/program%2Funreachable.html | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 devdocs/c/program%2Funreachable.html (limited to 'devdocs/c/program%2Funreachable.html') diff --git a/devdocs/c/program%2Funreachable.html b/devdocs/c/program%2Funreachable.html new file mode 100644 index 00000000..09e33e03 --- /dev/null +++ b/devdocs/c/program%2Funreachable.html @@ -0,0 +1,79 @@ +

unreachable

Defined in header <stddef.h>
#define unreachable() /* see below */
+
(since C23)

The function-like macro unreachable expands to a void expression. Executing unreachable() results in undefined behavior.

+

An implementation may use this to optimize impossible code branches away (typically, in optimized builds) or to trap them to prevent further execution (typically, in debug builds).

+

Possible implementation

+ +
// Uses compiler specific extensions if possible.
+#ifdef __GNUC__ // GCC, Clang, ICC
+ 
+#define unreachable() (__builtin_unreachable())
+ 
+#elifdef _MSC_VER // MSVC
+ 
+#define unreachable() (__assume(false))
+ 
+#else
+// Even if no extension is used, undefined behavior is still raised by
+// the empty function body and the noreturn attribute.
+ 
+// The external definition of unreachable_impl must be emitted in a separated TU
+// due to the rule for inline functions in C.
+ 
+[[noreturn]] inline void unreachable_impl() {}
+#define unreachable() (unreachable_impl())
+ 
+#endif

Example

#include <assert.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+ 
+struct Color { uint8_t r, g, b, a; };
+struct ColorSpan { struct Color* data; size_t size; };
+ 
+// Assume that only restricted set of texture caps is supported.
+struct ColorSpan allocate_texture(size_t xy)
+{
+    switch (xy)
+    {
+    case 128: [[fallthrough]];
+    case 256: [[fallthrough]];
+    case 512:
+    {
+        /* ... */
+        struct ColorSpan result = {
+            .data = malloc(xy * xy * sizeof(struct Color)),
+            .size = xy * xy
+        };
+        if (!result.data)
+            result.size = 0;
+        return result;
+    }
+    default:
+        unreachable();
+    }
+}
+ 
+int main(void)
+{
+    struct ColorSpan tex = allocate_texture(128); // OK
+    assert(tex.size == 128 * 128);
+ 
+    struct ColorSpan badtex = allocate_texture(32);  // Undefined behavior
+ 
+    free(badtex.data);
+    free(tex.data);
+}

Possible output:

+
Segmentation fault

See also

+
C++ documentation for unreachable
+ + + +
1. +GCC docs: __builtin_unreachable
2. +Clang docs: __builtin_unreachable
3. +MSVC docs: __assume
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/program/unreachable +

+
-- cgit v1.2.3