{{Use dmy dates|date=April 2019|cs1-dates=ly}} In computer programming, a '''dead store''' is a local variable that is assigned a value but is read by no following instruction. Dead stores waste processor time and memory, and may be detected through the use of static program analysis, and removed by an optimizing compiler.
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.<ref>{{Cite web |url=https://www.owasp.org/index.php/Insecure_Compiler_Optimization |title=Insecure Compiler Optimization | OWASP}}</ref> Some system libraries have specific functions designed to avoid such dangerous optimizations, e.g. <code>explicit_bzero</code> on OpenBSD.<ref>{{Cite web |url=http://man.openbsd.org/OpenBSD-current/man3/bzero.3 |title=OpenBSD manual pages |website=man.openbsd.org |access-date=2016-05-14}}</ref>
==Examples== ===Java=== Dead store example in Java: <syntaxhighlight lang="java"> // DeadStoreExample.java import java.util.ArrayList; import java.util.Arrays; import java.util.List;
public class DeadStoreExample { public static void main(String[] args) { List<String> list = new ArrayList<String>(); // This is a Dead Store, as the ArrayList is never read. list = getList(); System.out.println(list); }
private static List<String> getList() { return new ArrayList<String>(Arrays.asList("Hello")); } } </syntaxhighlight>
In the above code an <code>ArrayList<String></code> 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 <code>ArrayList</code> which was created when <code>list</code> was declared will now need to be de-allocated, for instance by a garbage collector.
===JavaScript=== Dead store example in JavaScript: <syntaxhighlight lang="javascript"> function func(a, b) { var x; var i = 300; while (i--) { x = a + b; // dead store } } </syntaxhighlight>
The code in the loop repeatedly overwrites the same variable, so it can be reduced to only one call.<ref>{{cite web |url=http://blogs.msdn.com/b/ie/archive/2010/11/17/html5-and-real-world-site-performance-seventh-ie9-platform-preview-available-for-developers.aspx |title=HTML5, and Real World Site Performance: Seventh IE9 Platform Preview Available for Developers}}</ref>
==See also== * Dead code * Unreachable code
==References== {{Reflist}}
{{Compiler optimizations}} Category:Compiler optimizations Category:Software anomalies Category:Programming language comparisons <!-- Hidden categories below --> Category:Articles with example Java code Category:Articles with example JavaScript code