Content deleted Content added
mNo edit summary |
Patar knight (talk | contribs) Adding local short description: "Local variable in computer programming", overriding Wikidata description "stack-allocated variable that is automatically deallocated upon leaving the scope in which it was declared" |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1:
{{Short description|Local variable in computer programming}}
{{About|Automatic variables|the C++ keyword {{Cpp|auto}} used for [[type inference]]|C++11#Type inference}}
In [[computer programming]], an '''automatic variable''' is a local [[Variable (programming)|variable]] which is allocated and deallocated automatically when program flow enters and leaves the variable'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 ''called'' function,<ref group="note">unless it is a [[nested function]], which itself is ''defined'' along that local data</ref> but is not deallocated, coming back in scope as the [[execution thread]] returns to the caller.
Line 14:
(Called ''automatic'' variables.)
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. <ref>{{As of|2008|alt=Current}} {{cite web|url= http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf |title=C standard }} {{small|(3.61 [[Mebibyte|MiB]])}}: section 6.2.4, Storage durations of objects</ref> The storage-class specifier <code>auto</code> can be added to these variable declarations as well, but as they are all automatic by default, this is entirely redundant and rarely done.
In C, using the storage class <code>register</code> is a hint to the compiler to cache the variable in a processor register. Other than not allowing the address-of operator (<code>&</code>) to be used on the variable or any of its subcomponents, the compiler is free to ignore the hint.<ref>{{citation|url=https://en.cppreference.com/w/c/language/storage_duration| title=Storage Duration| website=cppreference.com}}</ref>
In [[C++]], the constructor of automatic variables is called when the execution reaches the place of declaration.
Since C++11, C++ allows variables to be declared with the <code>auto</code> type specifier,<ref>{{citation| url=http://en.cppreference.com/w/cpp/language/auto| title=Placeholder type specifiers| website=cppreference.com}}</ref> but this means that the variable's type is [[type inference|inferred]], and does not refer to the scope of the variable.
|