Variable-Length Arrays & restrict in C
Variable-length arrays (C99) size stack arrays at run time: int buf[n]; where n is a variable — convenient for scratch space, dangerous at scale since stack overflow has no guard and sizeof evaluates at run time. C11 made VLAs optional (compilers may omit them), and large or untrusted sizes belong on the heap instead.
restrict promises a pointer is the sole access path to its object, letting the compiler optimize aggressively (no aliasing checks): void f(int * restrict a, int * restrict b). Lying — accessing through another pointer anyway — is undefined behavior the optimizer will exploit.
VLAs size at run time
int buf[n]; with variable n allocates on the stack — handy scratch space, stack overflow if n runs away.
Optional since C11
Compilers may omit VLAs (__STDC_NO_VLA__ signals absence) — portable code prefers malloc or fixed caps.
restrict promises no aliasing
int * restrict p tells the compiler nothing else touches the object — enabling vectorization and reordering.
Lying is UB
Accessing a restrict object through another pointer voids the warranty — miscompilation follows, silently.
#include <stdio.h> void fill(int * restrict a, int n) { for (int i = 0; i < n; ++i) a[i] = i;} int main(void) { int n = 3; int buf[n]; fill(buf, n); printf("%d %d\n", buf[0], buf[2]); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.