Automatic variable: Difference between revisions

Content deleted Content added
Addbot (talk | contribs)
m Bot: Migrating 1 interwiki links, now provided by Wikidata on d:q1091711
elab local, lifetime vs. context vs. scope
Line 1:
__NOTOC__
In [[computer programming]], an '''automatic variable''' is a [[scope (programming)|lexically-scoped]] [[Variable (programming)|variable]] which is allocated and deallocated automatically when program flow enters and leaves the variable's context. This primarily applies to [[scope (programming)|scopelexically-scoped]]. Thevariables, termwhere ''[[localthe variable]]''context is usuallythe synonymouslexical withcontext, automaticparticularly the function or block in which a variable is defined. Note that inside a function call within a context, sincea thesevariable aremoves out of scope (is inaccessible to the samecalled thingfunction) but is not deallocated, coming back in manyscope programmingwhen the function languagesreturns.
 
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.)