670 likes | 914 Views
Storage Classes. Storage Classes. Scope and linkage Storage classes Automatic memory Dynamic memory Memory Model. Storage Classes. There are five storage classes That specify the lifetime and visibility of a variable This does not include dynamic memory
E N D
Storage Classes • Scope and linkage • Storage classes • Automatic memory • Dynamic memory • Memory Model
Storage Classes • There are five storage classes • That specify the lifetime and visibility of a variable • This does not include dynamic memory • The different storage classes vary in combinations of three categories • Scope • Linkage • Duration
Scope • The scope of a variable is the region of the program in which it is visible • Block scope • Function prototype scope • File scope
Block Scope • A block is a region of a program enclosed in opening and closing (curly) brackets • Function definitions • If and switch statements • Loops • ... • A variable defined in a block is visible from where it is defined to the end of the block
More About Block Scope • Two variables with the same name cannot be declared with the same scope • But the scope of variables with the same name may overlap • Only the variable with the least scope is visible • Function parameters have the same scope as the function block • Even though they are declared outside the function’s enclosing brackets
Loops and C99 • Prior to the C99 standard, variables had to be declared at the start of a block • Compilers compliant with C99 allow variables to be declared anywhere in a block • Including in the initialization statement of a for loop • If a variable is declared inside a for loop its scope is the loop • To use a C99 compliant compiler in gcc use the std=c99 switch • gcc -o foofoo.c -std=c99
Function Prototype Scope • Applies to variable names used in function prototypes • The scope only applies to the function prototype • Names given to formal parameters may differ from the names used in the prototype
File Scope • A variable with its definition outside any function has file scope • It is visible from the point it is defined to the end of the file containing its definition • It may also be visible to other files
Linkage • Linkage refers to the file visibility of variables • Variables with block scope have no linkage • They are private to the block or prototype in which they are defined • Variables with file scope have either internal linkage or external linkage • A variable with internal linkage can be used anywhere inside the file in which it is declared • A variable with external linkage can be used anywhere in a multi-file program (i.e. in other files)
Linkage and static • By default, a variable with file scope has external linkage • If the variable is to be visible only within one file it should be specified as static • static int answer = 42;
Duration • A variable has one of three storage durations • Static • Automatic • Dynamic • The duration of a variable determines its lifetime within a program
Static Storage • Statically stored variables last for the lifetime of a program • The number of static variables does not change as a program runs • So no special system is required to maintain them • They are usually allocated space sequentially in an area of main memory • They are initialized to default values
Types of Static Variable • Static variables can have one of three types of linkage • External • Internal • None
External Linkage • A variable declared outside any function has external linkage • It can be used by other files that are part of the same program • int global = 1213; //outside main • A variable with external linkage can only be defined in one file
Using External Variables • External linkage raises the spectre of name ambiguity • A file could define a variable with the same name as another file’s external variable • Variables from other files should be declared with the externspecifier • extern int global; • This specifies that the variable has been declared in another file
Internal Linkage • Variables declared globally are external by default • It may be desirable to limit their scope to a single file • This can be achieved by declaring the variable with the static specifier • static intjust_in_this_file = 211;
No Linkage • A variable declared inside a function can also be specified as static • This affects its lifetime not its visibility • The lifetime of the variable is that of the program • It is stored in the same region of main memory as other static variables • The values of block scope static variables are preserved between function calls • Their scope is still block scope
Block Scope and Static void f1(int x){ static int count = 0; int y = 0; … count++; } defined once and initialized to 0 defined each time that f1 runs x, count, and y all have scope only within f1 adds one to count each time that f1 runs
Automatic Storage The Call Stack
Automatic Storage • Function variables and parameters use automatic storage by default • And have local scope and no linkage • Automatic variables are typically managed on a call stack • A section of allocated memory that grows and shrinks as functions are called • Automatic variables are not initialized • Unless done so explicitly
Functions and Memory • A variable defined in a function block only persists for the lifetime of that function call • Unless it is declared as static • Consider what memory might be allocated when a function is running • Memory required for the function’s data and only required during the function call • Memory that is to persist beyond the lifetime of the function
Allocating Memory for a Call • During a function call memory can be allocated to the function's variables • But not variables of other functions • Ignoring any dynamic memory allocation • Memory allocation should be fast and easy to maintain • This is not referring to the process of accessing a variable • But the process of allocating main memory space
Main Memory Allocation • When allocating main memory to a function it is desirable to • Waste as little space as possible • Minimize the amount of administration required to track free space • It is relatively easy to allocate space to function calls sequentially • If function a calls function b, then function a is not returned to until function b is terminated
Call Stack • Automatic variables are controlled by the call stack • A stack is a last-in-first-out (LIFO) data structure • A program keeps track of the stack with two pointers • One points to the base of the stack • The other points to the top of the stack • The next free memory location • Stack memory is allocated sequentially
Stack Memory – Simple Example Let's look at a simple example of allocating memory on a stack as described previously int x = 12; x = x * 3; double d = 23.567; 36 23.567 12 Why start at byte 3600? No reason, it's just an arbitrary value Notice that this example ignores all sorts of complicating issues
Stack and Functions • Let's look at a more complex example of allocating memory • That includes a main function and two other function calls • To make the example a bit simpler the byte values are not shown • Coloured boxes represent the memory allocated to variables
Another Example int main(){ int r = 3; double area = circleArea(r); double square(double x){ return x * x; } doublecircleArea(double radius){ double pi = 3.1415; doublesq_r = square(radius); returnsq_r * pi; } main memory x 3 3 3.1415 3 r radius pi square's memory start of circleArea's memory
Another Example int main(){ int r = 3; double area = circleArea(r); double square(double x){ return x * x; } doublecircleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); returnsq_r * pi; } main memory 3 3 3.1415 9 r radius pi sq_r start of circleArea's memory
Another Example int main(){ int r = 3; double area = circleArea(r); double square(double x){ return x * x; } doublecircleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } main memory 3 28.274 r area
Pushing and Popping • Function parameters and function variables are pushed onto the stack • And the pointer to the top of the stack is moved up by the size of each variable • When a function terminates the top of the stack is reset to its original position • Releasing memory assigned to the function
Register Variables • C supports the registerspecifier for declaring variables • Register variables are automatic so have • Lifetime over the life of the block, • Local scope, and • No linkage • Register is a hint to the compiler to assign the variable to a register • Allowing the CPU to access it quickly
Compiler Hints • Specifying a variable as register is a request to the compiler so may be ignored • If the registers are full • Or the variable type does not fit in a register • Modern compilers may be able to assign variables to registers on their own • e.g. for loop indexes • As they are required frequently
Stack Overflow • A compiler sets aside a fixed portion of main memory for the call stack • If this is all used up by function calls then a stack overflow occurs • Resulting in termination of the application • Some recursive algorithms are prone to stack overflow
Functions and Linkage • By default functions have external linkage • Functions cannot be declared inside one another so exist over the life of a program • Functions can be declared static to specify that they have internal linkage • And can only be called in that file • Allowing another function with the same name to be used in another file
Scope Quiz void foo(int y) { static intfoo_count = 0; foo_count++; y++; x++; //aargh! printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } // Global Variables intx = 10; static int y = 100; intmain() { inti; intx = 1; printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); printf("\n.. and into loop (y += i, x += i) ...\n\n"); for(i=0; i < 4; i++){ int x = 1000; y = y + i; //aargh! x = x * i; printf("%9s%04d %12s%04d ", "loop i = ", i, "loop x = ", x); printf("%12s%04d\n", "global y = ", y); foo(x); } printf("\n.. and out of loop ...\n\n"); printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); foo(x); return 0; }
Scope Quiz void foo(int y) { static intfoo_count = 0; foo_count++; y++; x++; //aargh! printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } // Global Variables intx = 10; static int y = 100; intmain() { inti; intx = 1; printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); printf("\n.. and into loop (y += i, x += i) ...\n\n"); for(i=0; i < 4; i++){ int x = 1000; y = y + i; //aargh! x = x * i; printf("%9s%04d %12s%04d ", "loop i = ", i, "loop x = ", x); printf("%12s%04d\n", "global y = ", y); foo(x); } printf("\n.. and out of loop ...\n\n"); printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); foo(x); return 0; } where is global x visible? just in foo
Scope Quiz void foo(int y) { static intfoo_count = 0; foo_count++; y++; x++; //aargh! printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } // Global Variables intx = 10; static int y = 100; intmain() { inti; intx = 1; printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); printf("\n.. and into loop (y += i, x += i) ...\n\n"); for(i=0; i < 4; i++){ int x = 1000; y = y + i; //aargh! x = x * i; printf("%9s%04d %12s%04d ", "loop i = ", i, "loop x = ", x); printf("%12s%04d\n", "global y = ", y); foo(x); } printf("\n.. and out of loop ...\n\n"); printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); foo(x); return 0; } where is global y visible? just in main
Scope Quiz void foo(int y) { static intfoo_count = 0; foo_count++; y++; x++; //aargh! printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } // Global Variables intx = 10; static int y = 100; intmain() { inti; intx = 1; printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); printf("\n.. and into loop (y += i, x += i) ...\n\n"); for(i=0; i < 4; i++){ int x = 1000; y = y + i; //aargh! x = x * i; printf("%9s%04d %12s%04d ", "loop i = ", i, "loop x = ", x); printf("%12s%04d\n", "global y = ", y); foo(x); } printf("\n.. and out of loop ...\n\n"); printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); foo(x); return 0; } where is main x visible?
Scope Quiz void foo(int y) { static intfoo_count = 0; foo_count++; y++; x++; //aargh! printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } // Global Variables intx = 10; static int y = 100; intmain() { inti; intx = 1; printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); printf("\n.. and into loop (y += i, x += i) ...\n\n"); for(i=0; i < 4; i++){ int x = 1000; y = y + i; //aargh! x = x * i; printf("%9s%04d %12s%04d ", "loop i = ", i, "loop x = ", x); printf("%12s%04d\n", "global y = ", y); foo(x); } printf("\n.. and out of loop ...\n\n"); printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); foo(x); return 0; } where is loop x visible?
Scope Quiz void foo(int y) { static intfoo_count = 0; foo_count++; y++; x++; //aargh! printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } // Global Variables intx = 10; static int y = 100; intmain() { inti; intx = 1; printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); printf("\n.. and into loop (y += i, x += i) ...\n\n"); for(i=0; i < 4; i++){ int x = 1000; y = y + i; //aargh! x = x * i; printf("%9s%04d %12s%04d ", "loop i = ", i, "loop x = ", x); printf("%12s%04d\n", "global y = ", y); foo(x); } printf("\n.. and out of loop ...\n\n"); printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); foo(x); return 0; } what is the effect of making y static? it is not visible in other files
Scope Quiz void foo(int y) { static intfoo_count = 0; foo_count++; y++; x++; //aargh! printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } // Global Variables intx = 10; static int y = 100; intmain() { inti; intx = 1; printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); printf("\n.. and into loop (y += i, x += i) ...\n\n"); for(i=0; i < 4; i++){ int x = 1000; y = y + i; //aargh! x = x * i; printf("%9s%04d %12s%04d ", "loop i = ", i, "loop x = ", x); printf("%12s%04d\n", "global y = ", y); foo(x); } printf("\n.. and out of loop ...\n\n"); printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); foo(x); return 0; } what is the effect of making foo_count static? it's lifetime is the lifetime of the program
Scope Quiz void foo(int y) { static intfoo_count = 0; foo_count++; y++; x++; //aargh! printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } // Global Variables intx = 10; static int y = 100; intmain() { inti; intx = 1; printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); printf("\n.. and into loop (y += i, x += i) ...\n\n"); for(i=0; i < 4; i++){ int x = 1000; y = y + i; //aargh! x = x * i; printf("%9s%04d %12s%04d ", "loop i = ", i, "loop x = ", x); printf("%12s%04d\n", "global y = ", y); foo(x); } printf("\n.. and out of loop ...\n\n"); printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); foo(x); return 0; } what is printed?
Scope Quiz what is printed ... // Global Variables intx = 10; static int y = 100; intmain() { inti; intx = 1; printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); printf("\n.. and into loop (y += i, x += i) ...\n\n"); for(i=0; i < 4; i++){ int x = 1000; y = y + i; //aargh! x = x * i; printf("%9s%04d %12s%04d ", "loop i = ", i, "loop x = ", x); printf("%12s%04d\n", "global y = ", y); foo(x); } printf("\n.. and out of loop ...\n\n"); printf("%9s%04d %12s%04d\n", "main x = ", x, "global y = ", y); foo(x); return 0; } void foo(int y) { static intfoo_count = 0; foo_count++; y++; x++; //aargh! printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); }
Storage Class Combinations ignoring dynamic memory ...
Dynamic Memory Introduction • The duration of variables allocated in automatic or static memory is fixed • The size of such variables is dependent on type and is also fixed • It may be useful to determine the space to be allocated to a variable at run-time • To avoid wasting space where the space requirements may vary considerably
Dynamic Memory • Dynamic memory is allocated from a different area of memory than the stack • This area of memory is referred to as the free store or the heap • Like stack memory the size is fixed, and is (of course) finite