C Reference ยท Functions and Scope
Storage duration in C
Reference for C storage duration: automatic, static, thread and allocated objects, initial values, where each lives in memory and dangling pointer pitfalls.
Storage duration says how long an object lives in memory. C defines four kinds. Automatic objects exist while their block runs. Static objects exist for the whole program. Thread objects exist for the lifetime of one thread. Allocated objects exist from malloc until free.
The kind you get depends on how and where you declare the object. Choosing correctly decides whether a pointer to the object remains valid, whether it starts with a known value, and how much stack memory a function uses.
int g; /* static: file scope */ void f(void) { int a; /* automatic */ static int s; /* static */ _Thread_local int t; /* thread */ int *h = malloc(sizeof *h); /* allocated (h itself is automatic) */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
| Duration | How you get it | Lifetime | Initial value |
|---|---|---|---|
| Automatic | Block-scope object without static/extern; parameters; compound literals in a block |
Entry to the block until its end (for VLAs, from the declaration) | Indeterminate unless initialized |
| Static | File-scope object; any object declared static or extern |
The whole program run | Zero or null unless initialized (initializer must be constant) |
| Thread | _Thread_local or thread_local (C23); with static or extern at block scope |
From thread start to thread end | Zero or null unless initialized |
| Allocated | malloc, calloc, realloc, aligned_alloc |
Until free or realloc |
Indeterminate (calloc gives zero bytes) |
Errors / undefined behavior: using an object outside its lifetime is undefined. This covers pointers to a returned function's locals, pointers to freed memory, and use of a block's object after the block ends (including goto out of a compound literal's block). Reading an uninitialized automatic or allocated object is undefined for most types. Freeing memory twice, or freeing a non-allocated pointer, is undefined.
Usage notes
- Automatic objects usually live on the stack. The stack is small (often 1 to 8 MB), so keep large arrays out of it; use
staticormallocfor big buffers. Deep recursion multiplies the cost. - Zero initialization is only for static and thread duration.
int x;in a function holds garbage. Writeint x = 0;or= {0}. - Don't return the address of an automatic object. Return an allocated or static object, or fill a caller's buffer.
- Allocated memory has no scope. Any function may free it, so decide who owns it. A missing
freeis a leak; one extra is a double free. callocand pointers.callocfills with zero bytes, which are not guaranteed to represent null pointers or 0.0 on exotic hardware (all common platforms use all-zero for both).- Variable-length arrays have automatic duration with a runtime size; too large a size overflows the stack without warning. Prefer
mallocfor unbounded sizes. - Compound literals
(int[]){1, 2, 3}have automatic duration inside functions and static duration at file scope. - String literals have static duration; modifying them is undefined. Use
char s[] = "text";to get a modifiable automatic copy. - Thread-local storage needs
<threads.h>or pthreads support. C11 threads are available in glibc 2.28;thread_localas a keyword is a C23 feature, accepted by GCC 15.