diff options
| author | David Faulkner <[email protected]> | 2026-08-07 23:40:47 -0500 |
|---|---|---|
| committer | David Faulkner <[email protected]> | 2026-08-07 23:40:47 -0500 |
| commit | b3e9e62599532050fc776c5e8f076915b56c2235 (patch) | |
| tree | af252346106a61b18cc6fc6fdbd32e962d096f1c /crash.c | |
Import official C23 code examples for Modern C (Jens Gustedt, 2024)HEADupstream-importmain
- Add official C source files, Makefile, c23-fallback.h, and LICENSE
- Update README.md with study mirror notice
Diffstat (limited to 'crash.c')
| -rw-r--r-- | crash.c | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -0,0 +1,33 @@ +#include "c23-fallback.h" +#include <stdio.h> +#include <stdalign.h> +#include <inttypes.h> +#include <complex.h> +#include "crash.h" + +void enable_alignment_check(void); +typedef complex double cdbl; + +int main(void) { + enable_alignment_check(); + /* An overlay of complex values and bytes. */ + union { + cdbl val[2]; + unsigned char buf[sizeof(cdbl[2])]; + } toocomplex = { + .val = { 0.5 + 0.5*I, 0.75 + 0.75*I, }, + }; + printf("size/alignment: %zu/%zu\n", + sizeof(cdbl), alignof(cdbl)); + /* Run over all offsets, and crash on misalignment. */ + for (size_t offset = sizeof(cdbl); offset; offset /=2) { + printf("offset\t%zu:\t", offset); + fflush(stdout); + cdbl* bp = (cdbl*)(&toocomplex.buf[offset]); // align! + printf("%g\t+%gI\t", creal(*bp), cimag(*bp)); + fflush(stdout); + *bp *= *bp; + printf("%g\t+%gI", creal(*bp), cimag(*bp)); + fputc('\n', stdout); + } +} |
