Automatic variable: Difference between revisions

Content deleted Content added
m Add a link to a memory management
mNo edit summary
Line 18:
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. 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. See [[Resource Acquisition Is Initialization]] (RAII).
 
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.