
Align Data With __attribute__((aligned)) and alignas
Raise the alignment of variables, structs and heap buffers with __attribute__((aligned(n))), alignas and aligned_alloc for cache lines, SIMD and DMA.
Tag
27 posts

Raise the alignment of variables, structs and heap buffers with __attribute__((aligned(n))), alignas and aligned_alloc for cache lines, SIMD and DMA.

Turn on _FORTIFY_SOURCE=3 with -O2 on glibc 2.33 or later and see which libc calls get bounds checks, how level 3 differs from 2 and what it cannot catch.

Remove struct padding with __attribute__((packed)) to match a wire format, then see the unaligned-access cost, the pointer warning and the memcpy alternative.

Declare a logging function with __attribute__((format(printf, ...))) so GCC and Clang check each call's format and arguments, and pair it with vsnprintf.

Declare a function that calls exit or abort as noreturn so GCC drops dead code, silences false warnings and lays out the error path correctly.

Build a C program with gcc -g, start it under GDB, stop at main, run it to the end and quit cleanly. The smallest complete session, with every output shown.

Turn on auto-vectorization in GCC and Clang, read the optimization reports that say why a loop was skipped, and set the flags that matter, including -march.

A stack canary is a secret value placed before the return address and checked before ret. See how GCC emits it, where glibc stores it, and what it misses.

Write a pkg-config .pc file so users get the right include paths and link flags for your library with one command, and install it where pkg-config will find it.

Build a versioned shared library with -Wl,-soname, upgrade it in place so programs pick up a patch, and ship an ABI break under a new soname.

A shared library has three names: the linker name used at build time, the soname recorded in binaries and the real name of the file. See how they fit together.

Cross-compile C, Autotools, CMake, Meson, Go and Rust programs for an embedded Linux board with a toolchain and sysroot, and fix the errors you will hit.