11 Section 2 Local Memory Thanks For The e Memory You th abou t noner a f 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 automa lly when you nee em,ar isappe Allocation And Deallocation eprese like orage space mem presents 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 mode are sma mory to a variable only when necessary.The value While the variable is allocat dit can rate able in th y to hold a value.A variable is deallocated when the system reclaims the memory from the a variable.the period of time is using a deallocated variable.For loca as we will see however,the programmer must make sure that allocation is handled correctl Local Memory and va parameters taken together are called its"local storage"or just its"locals",such as num and result in the following code. int results 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 When the its locals are deallocated.For the above example,that e) storage.When the function finally exits,its local storage is deallocated
11 Section 2 — Local Memory Thanks For The Memory 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... Allocation And Deallocation 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 allocated, it can operate as a variable in the usual way to hold a value. A variable is 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 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 "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 "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
12 Here is a more detailed version of the rules of local storage its locals are allocated memory.Parameters such as num and local result in the above ex veen the caller while random initial values.This article mostly uses simple int variables for its 2.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 function. 3.Finally,when the function finishes and exits,its locals are deallocated This makes sense in a way w to continue to exist ow could pose the locals were son The names way.Once the flow of control leaves that body,the re is n ay to refer to the locals even if they were allocated That locals are availab openaeyRhamgegs6wwnsca ("scoped")only within thei void in【/Locais(a,b,i,)allocated when For0rums float scores[100];/This array of 100 floats is allocated locally. a=a +1;//(2)Local storage is used by the computation i+)( //(3)Locals continue to ndist 1 //(4)The locals are all deallocated when the function exits. hich shows how the simple rueholocdh their function begins running and are deallocated when it exits"can build more complex irm grasp of how local allocation works to understand the ons andsin ons whet the code and the state of memory at that time is shown in the drawing
12 Here is a more detailed version of the rules of local storage... 1. 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. 2. 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. 3. 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 "lexical scoping" and pretty much all languages do it that way now. Small Locals Example 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<a; i++) { Bar(i + a); // (3) Locals continue to exist undisturbed, } // even during calls to other functions. } // (4) The locals are all deallocated when the function exits. Large Locals Example Here is a larger example which shows how the simple rule "the locals are allocated when their function begins running and are deallocated when it exits" can build more complex behavior. You will need a firm grasp of how local allocation works to understand the material in sections 3 and 4 later. The drawing shows the sequence of allocations and deallocations which result when the function X() calls the function Y() twice. The points in time T1, T2, etc. are marked in the code and the state of memory at that time is shown in the drawing