Automatic variable: Difference between revisions

Content deleted Content added
No edit summary
Line 1:
__NOTOC__
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.
 
Automatic local variables primarily applies to recursive [[scope (programming)|lexically-scoped]] languages.<ref group="note">although they exist in a somewhat similar, but not identical, form also in recursive languages with [[dynamic scoping]], such as older variants of [[LISP]]</ref> Automatic local variables are normally [[Stack-based memory allocation|allocated in the stack frame]] of the procedure in which they are declared.<ref group="note">unless otherwise specified, such as static or heap-based data, which are specifiable in some languages</ref> This was originally done to achieve [[reentrant (subroutine)|re-entrancy]] and allowing [[recursion]],<ref group="note">When the reentrant property of the routine is used, for recursion or otherwise, the [[compiler optimization|optimizer]] must ''not'' try to allocate such variables in [[processor register]]s (for efficiency) as this would break the reentrancy.</ref> considerations 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.