<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Automatic_variable</id>
	<title>Automatic variable - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Automatic_variable"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Automatic_variable&amp;action=history"/>
	<updated>2026-10-25T23:49:30Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Automatic_variable&amp;diff=1289515&amp;oldid=prev</id>
		<title>imported&gt;Naruyoko: /* C, C++ */ (Minimally) incorporate mention to RAII in sentence.</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Automatic_variable&amp;diff=1289515&amp;oldid=prev"/>
		<updated>2024-10-22T03:18:46Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;C, C++: &lt;/span&gt; (Minimally) incorporate mention to RAII in sentence.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{About|Automatic variables|the C++ keyword {{Cpp|auto}} used for [[type inference]]|C++11#Type inference}}&lt;br /&gt;
In [[computer programming]], an &amp;#039;&amp;#039;&amp;#039;automatic variable&amp;#039;&amp;#039;&amp;#039; is a local [[Variable (programming)|variable]] which is allocated and deallocated automatically when program flow enters and leaves the variable&amp;#039;s scope. The [[Scope (computer science)|scope]] is the lexical context, particularly the function or block in which a variable is defined. Local data is typically (in most languages) invisible outside the function or lexical context where it is defined. Local data is also invisible and inaccessible to a &amp;#039;&amp;#039;called&amp;#039;&amp;#039; function,&amp;lt;ref group=&amp;quot;note&amp;quot;&amp;gt;unless it is a [[nested function]], which itself is &amp;#039;&amp;#039;defined&amp;#039;&amp;#039; along that local data&amp;lt;/ref&amp;gt; but is not deallocated, coming back in scope as the [[execution thread]] returns to the caller.&lt;br /&gt;
&lt;br /&gt;
Automatic local variables primarily applies to [[Recursion (computer science)| recursive]] [[scope (programming)|lexically-scoped]] languages.&amp;lt;ref group=&amp;quot;note&amp;quot;&amp;gt;although they exist in a somewhat similar, but not identical, form also in recursive languages with [[dynamic scoping]], such as older variants of [[LISP]]&amp;lt;/ref&amp;gt; Automatic local variables are normally [[Stack-based memory allocation|allocated in the stack frame]] of the procedure in which they are declared.&amp;lt;ref group=&amp;quot;note&amp;quot;&amp;gt;unless otherwise specified, such as static or heap-based data, which are specifiable in some languages&amp;lt;/ref&amp;gt; This was originally done to achieve [[reentrant (subroutine)|re-entrancy]] and allowing [[recursion]],&amp;lt;ref group=&amp;quot;note&amp;quot;&amp;gt;When the reentrant property of the routine is used, for recursion or otherwise, the [[compiler optimization|optimizer]] must &amp;#039;&amp;#039;not&amp;#039;&amp;#039; try to allocate such variables in [[processor register]]s (for efficiency) as this would break the reentrancy.&amp;lt;/ref&amp;gt; a consideration that still applies today. The concept of automatic variables in recursive (and [[nested function|nested]]) functions in a lexically scoped language was introduced to the wider audience with [[ALGOL]] in the late 1950s, and further popularized by its many descendants.&lt;br /&gt;
&lt;br /&gt;
The term &amp;#039;&amp;#039;[[local variable]]&amp;#039;&amp;#039; is usually synonymous with automatic variable, since these are the same thing in many programming languages, but local is more general – most local variables are automatic local variables, but [[static local variable]]s also exist, notably in C. For a static local variable, the allocation is static (the lifetime is the entire program execution), not automatic, but it is only in scope during the execution of the function.&lt;br /&gt;
&lt;br /&gt;
==In specific programming languages==&lt;br /&gt;
===C, C++===&lt;br /&gt;
&lt;br /&gt;
{{See also|C syntax#Storage class specifiers}}&lt;br /&gt;
&lt;br /&gt;
(Called &amp;#039;&amp;#039;automatic&amp;#039;&amp;#039; variables.)&lt;br /&gt;
&lt;br /&gt;
All variables declared within a [[block (programming)|block]] of code are automatic by default. An uninitialized automatic variable has an [[undefined behavior|undefined]] value until it is assigned a valid value of its type. &amp;lt;ref&amp;gt;{{As of|2008|alt=Current}} {{cite web|url= http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf |title=C standard }}&amp;amp;nbsp;{{small|(3.61&amp;amp;nbsp;[[Mebibyte|MiB]])}}: section 6.2.4, Storage durations of objects&amp;lt;/ref&amp;gt; The storage-class specifier &amp;lt;code&amp;gt;auto&amp;lt;/code&amp;gt; can be added to these variable declarations as well, but as they are all automatic by default, this is entirely redundant and rarely done.&lt;br /&gt;
&lt;br /&gt;
In C, using the storage class &amp;lt;code&amp;gt;register&amp;lt;/code&amp;gt; is a hint to the compiler to cache the variable in a processor register. Other than not allowing the address-of operator (&amp;lt;code&amp;gt;&amp;amp;&amp;lt;/code&amp;gt;) to be used on the variable or any of its subcomponents, the compiler is free to ignore the hint.&amp;lt;ref&amp;gt;{{citation|url=https://en.cppreference.com/w/c/language/storage_duration| title=Storage Duration| website=cppreference.com}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In [[C++]], the constructor of automatic variables is called when the execution reaches the place of declaration. The destructor is called when it reaches the end of the given program block (program blocks are surrounded by curly brackets). This feature is often used to manage resource allocation and deallocation, like opening and then automatically closing files or freeing up memory, called [[Resource Acquisition Is Initialization]] (RAII). &lt;br /&gt;
&lt;br /&gt;
Since C++11, C++ allows variables to be declared with the &amp;lt;code&amp;gt;auto&amp;lt;/code&amp;gt; type specifier,&amp;lt;ref&amp;gt;{{citation| url=http://en.cppreference.com/w/cpp/language/auto| title=Placeholder type specifiers| website=cppreference.com}}&amp;lt;/ref&amp;gt; but this means that the variable&amp;#039;s type is [[type inference|inferred]], and does not refer to the scope of the variable.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
(Called &amp;#039;&amp;#039;local variables&amp;#039;&amp;#039;.)&lt;br /&gt;
&lt;br /&gt;
Similar to C and C++, but there is no &amp;lt;code&amp;gt;auto&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;register&amp;lt;/code&amp;gt; keyword. However, the Java compiler will not allow the usage of a not-explicitly-initialized local variable and will give a compilation error (unlike C and C++ where the compiler will usually only give a warning). The Java standard demands that every local variable must be explicitly initialized before being used.&amp;lt;ref&amp;gt;{{cite web | url=http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5 | title=4.12.5 Initial Values of Variables | publisher=Sun Microsystems | access-date=2008-10-17}}&amp;lt;/ref&amp;gt; This differs from instance variables, which are implicitly initialized with default values (which are {{samp|0}} for numbers and {{samp|null}} for objects).&lt;br /&gt;
&lt;br /&gt;
===Perl===&lt;br /&gt;
(Called &amp;#039;&amp;#039;lexical&amp;#039;&amp;#039;, &amp;#039;&amp;#039;my&amp;#039;&amp;#039; or &amp;#039;&amp;#039;private&amp;#039;&amp;#039; variables.)&lt;br /&gt;
&lt;br /&gt;
In Perl, local variables are declared using the &amp;lt;code&amp;gt;my&amp;lt;/code&amp;gt; operator. Uninitialized scalars will have the value &amp;lt;code&amp;gt;undef&amp;lt;/code&amp;gt;; uninitialized arrays or hashes will be &amp;lt;code&amp;gt;()&amp;lt;/code&amp;gt;.&amp;lt;ref&amp;gt;{{cite web | url=http://perldoc.perl.org/perlsub.html#Private-Variables-via-my() | title=Private variables via my() - perlsub - perldoc.perl.org | access-date=2008-10-17}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Perl also has a &amp;lt;code&amp;gt;[[Local variable|local]]&amp;lt;/code&amp;gt; operator that does not create automatic variables,&amp;lt;ref&amp;gt;{{cite web | url=http://perldoc.perl.org/perlsub.html#Temporary-Values-via-local%28%29 | title=Temporary values via local() - perlsub - perldoc.perl.org | access-date=2011-02-25}}&amp;lt;/ref&amp;gt; instead giving global (package) variables a temporary value, which is [[Scope_(computer_science)#Dynamic_scoping|dynamically scoped]] to the enclosing block. When the scope of the variable is left, the old value is restored.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[C syntax#Storage class specifiers]]&lt;br /&gt;
*[[Variable-length array]] ([[C99]] new feature)&lt;br /&gt;
*[[Call stack]]&lt;br /&gt;
*[[Object lifetime]]&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
{{Reflist|group=note}}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Automatic Variable}}&lt;br /&gt;
{{Memory management}}&lt;br /&gt;
[[Category:Memory management]]&lt;br /&gt;
[[Category:Variable (computer science)]]&lt;br /&gt;
&lt;br /&gt;
[[es:Asignación automática de memoria]]&lt;br /&gt;
[[de:Automatische Variable]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Naruyoko</name></author>
	</entry>
</feed>