Download presentation
Presentation is loading. Please wait.
Published byPauline Simmons Modified over 9 years ago
2
Scope and linkage Storage classes Automatic memory Dynamic memory Memory Model
3
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
5
The scope of a variable is the region of the program in which it is visible Block scope Function prototype scope File scope
6
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
7
Two variables with the same name cannot be declared in the same block 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
8
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 foo foo.c -std=c99
9
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
10
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
11
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)
12
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;
14
A variable has one of three storage durations Static Automatic Dynamic The duration of a variable determines its lifetime within a program
15
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
16
Static variables can have one of three types of linkage External Internal None
17
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
18
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 extern specifier extern int global; This specifies that the variable has been declared in another file
19
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 int just_in_this_file = 211;
20
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
21
void f1(int x){ static int count = 0; int y = 0; … count++; } void f1(int x){ static int count = 0; int y = 0; … count++; } defined once and initialized to 0 defined each time that f1 runs adds one to count each time that f1 runs x, count, and y all have scope only within f1
22
The Call Stack
23
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
24
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
25
During a function call, memory can be allocated to the function's variables But not to variables of other functions ▪ Ignoring any dynamic memory allocation Memory allocation should be fast and simple That is, the process of allocating main memory space to a variable ▪ Not the process of accessing a variable (although that should be fast too)
26
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
27
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
28
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; int x = 12; x = x * 3; double d = 23.567; Notice that this example ignores all sorts of complicating issues …3600360136023603360436053606360736083609361036113612… Why start at byte 3600? No reason, it's just an arbitrary value 12 36 23.567
29
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
30
int main(){ int r = 3; double area = circleArea(r); int main(){ int r = 3; double area = circleArea(r); double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double square(double x){ return x * x; } double square(double x){ return x * x; } main memory 3 3 3 3 3.1415 start of circleArea's memory square's memory 3 3 r r radius pi x x
31
main memory 3 3 3 3 3.1415 start of circleArea's memory 9 9 sq_r r r radius pi int main(){ int r = 3; double area = circleArea(r); int main(){ int r = 3; double area = circleArea(r); double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double square(double x){ return x * x; } double square(double x){ return x * x; }
32
main memory 3 3 28.274 r r area int main(){ int r = 3; double area = circleArea(r); int main(){ int r = 3; double area = circleArea(r); double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double square(double x){ return x * x; } double square(double x){ return x * x; }
33
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
34
C supports the register specifier 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
35
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
36
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
37
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
38
int main() { int i; int x = 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! (global) 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; } int main() { int i; int x = 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! (global) 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 int foo_count = 0; foo_count++; y++; x++; //aargh! (global) printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } void foo(int y) { static int foo_count = 0; foo_count++; y++; x++; //aargh! (global) printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } // Global Variables int x = 10; static int y = 100; // Global Variables int x = 10; static int y = 100;
39
int main() { int i; int x = 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! (global) 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; } int main() { int i; int x = 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! (global) 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 int foo_count = 0; foo_count++; y++; x++; //aargh! (global) printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } void foo(int y) { static int foo_count = 0; foo_count++; y++; x++; //aargh! (global) printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } // Global Variables int x = 10; static int y = 100; // Global Variables int x = 10; static int y = 100; where is global x visible? just in foo
40
int main() { int i; int x = 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! (global) 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; } int main() { int i; int x = 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! (global) 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 int foo_count = 0; foo_count++; y++; x++; //aargh! (global) printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } void foo(int y) { static int foo_count = 0; foo_count++; y++; x++; //aargh! (global) printf("%9s%04d %12s%04d ", "foo y = ", y, "foo_count = ", foo_count); printf("%12s%04d\n", "global x = ", x); } // Global Variables int x = 10; static int y = 100; // Global Variables int x = 10; static int y = 100; where is global y visible? just in main
41
int main() { int i; int x = 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; } int main() { int i; int x = 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 int foo_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); } void foo(int y) { static int foo_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 int x = 10; static int y = 100; // Global Variables int x = 10; static int y = 100; where is main x visible?
42
int main() { int i; int x = 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; } int main() { int i; int x = 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 int foo_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); } void foo(int y) { static int foo_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 int x = 10; static int y = 100; // Global Variables int x = 10; static int y = 100; where is loop x visible?
43
int main() { int i; int x = 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; } int main() { int i; int x = 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 int foo_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); } void foo(int y) { static int foo_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 int x = 10; static int y = 100; // Global Variables int x = 10; static int y = 100; what is the effect of making y static? it is not visible in other files
44
int main() { int i; int x = 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; } int main() { int i; int x = 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 int foo_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); } void foo(int y) { static int foo_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 int x = 10; static int y = 100; // Global Variables int x = 10; static int y = 100; what is the effect of making foo_count static? it's lifetime is the lifetime of the program
45
int main() { int i; int x = 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; } int main() { int i; int x = 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 int foo_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); } void foo(int y) { static int foo_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 int x = 10; static int y = 100; // Global Variables int x = 10; static int y = 100; what is printed?
46
// Global Variables int x = 10; static int y = 100; int main() { int i; int x = 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 int foo_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 int x = 10; static int y = 100; int main() { int i; int x = 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 int foo_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); } what is printed...
47
ignoring dynamic memory... Storage ClassDurationScopeLinkageDeclared automatic blocknonein a block registerautomaticblocknonein a block with register static externalstaticfileexternaloutside all functions static internalstaticfileinternaloutside all functions with static static no linkagestaticblocknonein a block with static
49
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
50
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
51
Dynamic memory can be allocated at runtime Using malloc to request memory of a given size The malloc function takes a single argument ▪ The number of bytes to be allocated Memory allocated with malloc remains allocated until it is released It is not limited to the lifetime of the block in which it is allocated Memory is released by calling free
52
In addition to allocating memory, malloc returns the address of this memory The address should be assigned to a pointer variable Dynamic memory is often used to create dynamic arrays For example create an array of ten integers ▪ int* arr = malloc(10 * sizeof(int)); ▪ The address returned by malloc is assigned to arr
53
Arrays assigned to pointers may be used just like regular (static) arrays The elements can be accessed using an index There is one major difference between dynamic arrays and static arrays A pointer can be assigned a new array But what happens to the old array?
54
int* arr = malloc(10 * sizeof(int)); //... do stuff with arr //... and make a new, larger array arr = malloc(100 * sizeof(int)); int* arr = malloc(10 * sizeof(int)); //... do stuff with arr //... and make a new, larger array arr = malloc(100 * sizeof(int)); allocates 40 bytes in the heap allocates another 400 bytes in the heap, it does not re-use the 40 bytes that were previously allocated the original 40 bytes cannot be accessed, since the program no longer records the address however, they are also unavailable for reuse, thereby causing a memory leak
55
A memory leak occurs when dynamic memory is allocated but is not de-allocated When it is no longer required Dynamic memory that is not de-allocated is unavailable until the program terminates Or even longer in some (usually older) operating systems Large memory leaks may affect the performance of applications
56
Any dynamic memory that is allocated by a program should be de-allocated By calling free ▪ The free function takes a single argument, the pointer to the memory that is to be de-allocated Every use of malloc should be matched by a call to free When the allocated dynamic memory is no longer required
57
Unlike static or automatic memory the lifetime of dynamic memory is not fixed It is dependent on when the memory is first allocated and when it is released The lifetime of a variable created in dynamic memory is between the calls to malloc and free
58
The calloc function can also be used to allocate dynamic memory It takes two arguments, the size of the array and the size of the array type ▪ int* arr = calloc(10, sizeof(int)); Unlike malloc, calloc also sets all of the bits in the allocated memory to 0 The free function is used to de-allocate memory allocated with calloc
59
Dynamic memory can be exhausted To make memory allocation safe it is good practice to ensure that allocation succeeds Both malloc and calloc will return the NULL pointer if allocation fails int* p; int n = 10000000; p = (int*) malloc(n * sizeof(int)); if(p == NULL){ puts("Memory allocation failed."); exit(EXIT_FAILURE); } int* p; int n = 10000000; p = (int*) malloc(n * sizeof(int)); if(p == NULL){ puts("Memory allocation failed."); exit(EXIT_FAILURE); } exit terminates the application
60
Under the ANSI C standard both calloc and malloc return pointers to void A generic pointer It is therefore good practice to cast the pointer returned by malloc to the correct type int* arr = (int *) malloc(10 * sizeof(int)); casts (converts) the pointer to void to an int pointer
62
static code storage automatic dynamic storage space for program instructions global variables and variables declared as static function calls, local variables and function parameters dynamically allocated variables, addresses are assigned to pointers
63
Used for global variables and variables that are explicitly declared as static With the keyword static Exist for the lifetime of the program Local, static variables are declared and initialized once Should be used for variables that are to persist over the life of the program
64
Used for most local variables declared in a block or function header Exist only for the lifetime of the block The most common method of variable storage Variables can be explicitly declared as automatic by using the auto keyword ▪ To indicate that the variable should not be changed to some other storage class Typically allocated on a call stack
65
Used for variables whose size or lifetime can only be determined at run-time Allows arrays of varying size to be assigned to one pointer variable (over time) The allocation of dynamic memory is controlled by function calls A dynamic variable’s lifetime starts with a call to malloc (or calloc) and ends with a call to free ▪ Or when the program terminates
66
Static, automatic and dynamic memory are different regions of RAM Variables in static memory require the least amount of overhead Automatic variables require more overhead ▪ Since information about function calls needs to be maintained Variables in dynamic memory require the most overhead
67
This function demonstrates that static, automatic and dynamic variables are allocated space in different areas of main memory void memoryTest() { static int x; int y = 0; int* p = malloc(4 * sizeof(int)); double* q = calloc(10, sizeof(double)); char* s = "albatross"; printf("static (s:albatross): %x\n", s); printf("static (&x): %x\n", &x); printf("automatic (&y): %x\n", &y); printf("automatic (&p): %x\n", &p); printf("automatic (&q): %x\n", &q); printf("automatic (&s): %x\n", &s); printf("dynamic (p): %x\n", p); printf("dynamic (q): %x\n", q); } void memoryTest() { static int x; int y = 0; int* p = malloc(4 * sizeof(int)); double* q = calloc(10, sizeof(double)); char* s = "albatross"; printf("static (s:albatross): %x\n", s); printf("static (&x): %x\n", &x); printf("automatic (&y): %x\n", &y); printf("automatic (&p): %x\n", &p); printf("automatic (&q): %x\n", &q); printf("automatic (&s): %x\n", &s); printf("dynamic (p): %x\n", p); printf("dynamic (q): %x\n", q); } string literals are stored in static memory
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.