STATIC and AUTOMATIC Lifetime
In short: In SystemVerilog, a variable’s lifetime decides when its memory is created and freed. A static variable is created once and lives until the simulation ends, so every call shares the same copy. An automatic variable lives on the stack: a fresh copy is made each time you enter the task, function, or block, and it is freed when you leave. Use automatic for recursion and for anything that runs in parallel, so each call gets its own storage.
One small keyword, static or automatic, decides whether every call to a task or function shares one copy of a variable or gets its own. Get it wrong and a recursive function returns nonsense, or two parallel threads quietly corrupt each other’s data. This post explains what the two lifetimes mean, shows the default for every kind of variable and method, walks through a factorial example that behaves differently under each lifetime, and ends with common mistakes and an FAQ.
What lifetime means
A static lifetime means the variable’s memory is allocated once at the start and is never freed until the simulation ends. There is exactly one copy, and every call to the enclosing method sees and updates that same copy.
An automatic lifetime means the variable uses stack storage. Each time you enter the task, function, or block, a new copy is created, and that copy is freed as soon as execution of that method or block finishes. Two calls, even at the same time, each get their own private copy.
A simple way to picture it
Think of a shared notepad glued to a desk versus a fresh sticky note per person. A static variable is the glued notepad: everyone who walks up writes on the same page, so the last writer overwrites the previous one, and whatever is there stays there all day. An automatic variable is a fresh sticky note handed to each person as they arrive: they write on their own note, and it is thrown away when they leave, so nobody steps on anyone else’s writing.
Recursion is like the same person stepping up to the desk several times before finishing the first visit. With one glued notepad they keep overwriting their own earlier note and lose their place. With a new sticky note each time, every level of the recursion keeps its own value. That is exactly why recursive functions need an automatic lifetime.
Default lifetime of variables
Different kinds of variables default to different lifetimes. This table lists the common cases.
| Where the variable is declared | Default lifetime |
|---|---|
| Class variable (property) | Automatic |
| Method (task or function) variable | Automatic |
| Loop-local variable | Automatic |
| Module or program block variable | Static |
| Variable in an initial or always block | Static |
Default lifetime of methods
Methods have defaults too, and this is where recursion trips people up.
| Where the method is declared | Default lifetime |
|---|---|
| Method in a module or program block | Static |
| Method in a class | Automatic |
Because a method inside a module is static by default, its variables and its implied return storage are static too. That default exists to stay compatible with older Verilog. It also means a recursive function written inside a module, without the automatic keyword, does not get a fresh copy per call.
Example: a static (default) recursive function
Here factorial is recursive but uses the module default, which is static. Because the function shares one storage location across all recursive calls, the inner calls overwrite the value the outer call was still using. The result is not the clean factorial you expect.
module tryfact_static;
// No 'automatic' keyword, so inside a module this is STATIC by default.
function integer factorial (input [31:0] operand);
begin
if (operand >= 2)
factorial = factorial(operand - 1) * operand; // shares one copy
else
factorial = 1;
end
endfunction : factorial
integer result;
initial begin
for (int n = 0; n <= 7; n++) begin
result = factorial(n);
$display("%0d factorial = %0d", n, result);
end
end
endmodule : tryfact_static
The static function does not create a separate stack entry for each recursive call, so the recursion cannot keep a private value at each level. The screenshot below shows the output from this static version.
Example: an automatic recursive function
Now add the automatic keyword. Each call gets its own copy of the operand and the return storage on the stack, so recursion works correctly.
module tryfact_auto;
// 'automatic' gives each call its own stack copy, so recursion is correct.
function automatic integer factorial (input [31:0] operand);
begin
if (operand >= 2)
factorial = factorial(operand - 1) * operand; // private per call
else
factorial = 1;
end
endfunction : factorial
integer result;
initial begin
for (int n = 0; n <= 7; n++) begin
result = factorial(n);
$display("%0d factorial = %0d", n, result);
end
end
endmodule : tryfact_auto
Because the function is automatic, it creates a separate call entry and its own variable storage each time, so each level of the recursion keeps its own value. The screenshot below shows the corrected output from the automatic version.
Why this matters beyond recursion
Lifetime also matters any time code runs in parallel. If several threads run the same static task, they share its variables and can overwrite each other. Making the task automatic gives each thread its own copy. In class-based verification this is why class methods default to automatic, and it is good practice to declare reusable tasks and functions as automatic unless you specifically want shared storage.
Common mistakes
- Writing a recursive function inside a module without
automatic, so the shared storage gives wrong answers. - Assuming a module method is automatic. Inside a module or program block it is static by default.
- Running the same static task in several parallel threads and seeing values corrupt each other. Make it automatic.
- Thinking class properties are static. Class variables and class methods are automatic by default.
- Relying on a static variable to remember a value on purpose, then being surprised when a second call sees the old value. If you want that, make the intent clear.
Key takeaways
static: one copy for the whole simulation, shared by every call.automatic: a fresh stack copy per entry, freed on exit.- Module and program methods are static by default; class methods are automatic by default.
- Recursion and parallel threads need
automaticso each call or thread has private storage. - When in doubt for reusable tasks and functions, prefer
automatic.
Honesty note: the code follows a plain reading of the SystemVerilog LRM (IEEE 1800) and is written to show the idea. Exact output depends on your simulator. Confirm behaviour on your own tool or on EDA Playground before relying on it.
Related reading
What is the difference between static and automatic lifetime in SystemVerilog?
A static variable is allocated once and lives until the simulation ends, so every call shares one copy. An automatic variable uses stack storage, so each entry to the task, function, or block gets its own copy that is freed on exit.
What is the default lifetime of a method inside a module?
Static. A task or function declared inside a module or program block is static by default, which is kept for compatibility with older Verilog.
What is the default lifetime of a method inside a class?
Automatic. Class methods, and class properties, are automatic by default, so each call gets its own copy of local variables.
Why do recursive functions need to be automatic?
Because each recursive call needs its own private storage. A static function shares one copy across all calls, so the inner calls overwrite the value the outer call was using and the result is wrong.
What is the default lifetime of a variable in an initial or always block?
Static. Variables declared in an initial or always block, and module or program block variables, are static by default.
Does automatic lifetime affect parallel threads?
Yes. If several threads run the same static task they share its variables and can corrupt each other. An automatic task gives each thread its own copy, so they do not interfere.
How do I make a task or function automatic?
Add the automatic keyword, for example function automatic integer f(…); or task automatic t(…);. You can also set a default lifetime for a module with module automatic.
Are class variables static or automatic by default?
Automatic. Class variables (properties) and class methods default to automatic lifetime in SystemVerilog.
Part of the learning path SystemVerilog for verification
