Learn C
#include "arraysstrings/vla_restrict.h"

Variable-Length Arrays & restrict in C

#concept

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.

#how it works
1

VLAs size at run time

int buf[n]; with variable n allocates on the stack — handy scratch space, stack overflow if n runs away.

2

Optional since C11

Compilers may omit VLAs (__STDC_NO_VLA__ signals absence) — portable code prefers malloc or fixed caps.

3

restrict promises no aliasing

int * restrict p tells the compiler nothing else touches the object — enabling vectorization and reordering.

4

Lying is UB

Accessing a restrict object through another pointer voids the warranty — miscompilation follows, silently.

#example
#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;
}
Output
0 2
1buf sizes itself from n at run time on the stack — small and bounded, so safe here.
2fill's restrict parameter promises exclusive access, letting the loop optimize freely.
30 2 prints — positions filled 0, 1, 2 by the loop.
#use cases
Small run-time-sized scratch buffers without malloc overhead
restrict-qualified numeric kernels the compiler can vectorize
Checking __STDC_NO_VLA__ before depending on VLAs
Preferring heap allocation for large or untrusted sizes

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.