.. _LocalMem: .. raw:: html .. |--| unicode:: U+2013 .. en dash .. |---| unicode:: U+2014 .. em dash, trimming surrounding whitespace :trim: .. This file is part of the OpenDSA eTextbook project. See .. http://algoviz.org/OpenDSA for more details. .. Copyright (c) 2012-2016 by the OpenDSA Project Contributors, and .. distributed under an MIT open source license. .. avmetadata:: :author: Nick Parlante, Cliff Shaffer, and Sally Hamouda :requires: Pointer intro :satisfies: Local memory :topic: Pointers Local Memory ============ Thanks For The Memory: Allocation and Deallocation -------------------------------------------------- :term:`Local variables ` are the programming structure everyone uses but no one thinks about. You think about them a little when first mastering the syntax. But after a few weeks, the variables are so automatic that you soon forget to think about how they work. This situation is a credit to modern programming languages |---| most of the time variables appear automatically when you need them, and they disappear automatically when you are finished. For basic programming, this is a fine situation. However, for advanced programming, it's going to be useful to have an idea of how variables work... Variables represent storage space in the computer's memory. Each variable presents a convenient names like length or sum in the source code. Behind the scenes at runtime, each variable uses an area of the computer's memory to store its value. It is not the case that every variable in a program has a permanently assigned area of memory. Instead, modern languages are smart about giving memory to a variable only when necessary. The terminology is that a variable is allocated when it is given an area of memory to store its value. While the variable is :term:`allocated`, it can operate as a variable in the usual way to hold a value. A variable is :term:`deallocated` when the system reclaims the memory from the variable, so it no longer has an area to store its value. For a variable, the period of time from its allocation until its deallocation is called its :term:`lifetime`. The most common memory related error is using a deallocated variable. For local variables, modern languages automatically protect against this error. With pointers, as we will see however, the programmer must make sure that allocation is handled correctly. Local Memory ------------ The most common variables you use are :term:`local variables` within functions such as the variables ``num`` and ``result`` in the following function. All of the local variables and parameters taken together are called its :term:`local storage` or just its "locals", such as ``num`` and ``result`` in the following code... :: // Local storage example int Square(int num) { int result; result = num * num; return result; } The variables are called "local" to capture the idea that their lifetime is tied to the function where they are declared. Whenever the function runs, its local variables are allocated. When the function exits, its locals are deallocated. For the above example, that means that when the ``Square()`` function is called, local storage is allocated for ``num`` and ``result``. Statements like ``result = num * num``; in the function use the local storage. When the function finally exits, its local storage is deallocated. Here is a more detailed version of the rules of local storage: #. When a function is called, memory is allocated for all of its locals. In other words, when the flow of control hits the starting ``{`` for the function, all of its locals are allocated memory. Parameters such as num and local variables such as result in the above example both count as locals. The only difference between parameters and local variables is that parameters start out with a value copied from the caller while local variables start with random initial values. This article mostly uses simple ``int`` variables for its examples, however local allocation works for any type: structs, arrays... these can all be allocated locally. #. The memory for the locals continues to be allocated so long as the thread of control is within the owning function. Locals continue to exist even if the function temporarily passes off the thread of control by calling another function. The locals exist undisturbed through all of this. #. Finally, when the function finishes and exits, its locals are deallocated. This makes sense in a way |---| suppose the locals were somehow to continue to exist |---| how could the code even refer to them? The names like ``num`` and ``result``only make sense within the body of ``Square()`` anyway. Once the flow of control leaves that body, there is no way to refer to the locals even if they were allocated. That locals are available ("scoped") only within their owning function is known as :term:`lexical scoping` and pretty much all languages do it that way now. Examples -------- Here is a simple example of the lifetime of local storage. :: void Foo(int a) { // (1) Locals (a, b, i, scores) allocated when Foo runs int i; float scores[100]; // This array of 100 floats is allocated locally. a = a + 1; // (2) Local storage is used by the computation for (i=0; i