In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, a local variable that is assigned a value but is not read by any subsequent instruction is referred to as a dead store. Dead stores waste processor time and memory, and may be detected through the use of
static program analysis
In computer science, static program analysis (or static analysis) is the analysis of computer programs performed without executing them, in contrast with dynamic program analysis, which is performed on programs during their execution.
The term i ...
, and removed by an
optimizing compiler
In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. Common requirements are to minimize a program's execution time, memory footprint, storage size, and power cons ...
.
If the purpose of a store is intentionally to overwrite data, for example when a password is being removed from memory, dead store optimizations can cause the write not to happen, leading to a security issue. Some system libraries have specific functions designed to avoid such dangerous optimizations, e.g.
explicit_bzero
on OpenBSD.
Examples
Java example of a dead store:
// DeadStoreExample.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DeadStoreExample
In the above code an
ArrayList
object was instantiated but never used. Instead, in the next line the variable which references it is set to point to a different object. The
ArrayList
which was created when
list
was declared will now need to be de-allocated, for instance by a
garbage collector
A waste collector, also known as a garbageman, garbage collector, trashman (in the US), binman or (rarely) dustman (in the UK), is a person employed by a public or private enterprise to collect and dispose of municipal solid waste (refuse) and r ...
.
JavaScript example of a dead store:
function func(a, b)
The code in the loop repeatedly overwrites the same variable, so it can be reduced to only one call.
See also
*
Dead code The term dead code has multiple definitions. Some use the term to refer to code (i.e. instructions in memory) which can never be executed at run-time.
In some areas of computer programming, dead code is a section in the source code of a program whic ...
*
Unreachable code In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.
Unreachable code is sometimes also called ''dead code' ...
References
{{Compiler optimizations
Compiler optimizations
Software anomalies
Articles with example Java code