Automatic variable: Difference between revisions

Content deleted Content added
elab local, lifetime vs. context vs. scope
Fixed a couple of misconceptions, made it slighly more general, and added some historical background.
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 contextscope. ThisThe primarily applies to [[scope (programming)|lexically-scoped]] variables, where the context is the lexical context, particularly the function or block in which a variable is defined. NoteAutomatic thatlocal insidedata ais functiontypically call(in withinmost alanguages) context,invisible aoutside variablethe movesfunction outor oflexical scopecontext where it (is defined. It is also invisible and inaccessible to thea locally ''called'' function) <ref>unless this function is also ''defined'' locally, along with that local data, see [[nested function]]</ref>, but is not deallocated, coming back in scope whenas the functionexecution thread returns to the caller.
 
Automatic local variables primarily applies to recursive [[scope (programming)|lexically-scoped]] languages. Automatic local variables are normally [[Stack-based memory allocation|allocated in the stack frame]] of the procedure in which they are declared<ref>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>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, although the concept of recursive (and [[nested function|nested]]) functions in a lexically scoped language was introduced to the wider audience already with [[ALGOL]] in the late 1950s (and then further popularized by its many descendants).
 
The term ''[[local variable]]'' 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.
 
Automatic variables may be [[Stack-based memory allocation|allocated in the stack frame]] of the procedure in which they are declared; this has the useful effect of allowing [[recursion]] and [[reentrant (subroutine)|re-entrancy]]. (For efficiency, the [[compiler optimization|optimizer]] will try to allocate some of these variables in [[processor register]]s.)
 
==In specific programming languages==