Department of Electrical, Electronics, and Communication Engineering C Programming Language Storage Classes, Linkage, and Memory Management Manar Mohaisen Office: F208 Email: manar.subhi@kut.ac.kr Department of EECE
Review of the Precedent Lecture Explained how to handle files in C Department of Electrical, Electronics, and Communication Engineering
Department of Electrical, Electronics, and Communication Engineering Class Objectives Explain Variable Scopes, Storage Classes, and Linkage Explain Memory Allocation Using malloc() Explain Memory Allocation Using calloc() Explain Memory Release Using free() Discussions
Department of Electrical, Electronics, and Communication Engineering Scopes Scope Region(s) of a program that can access the variable. Scopes Block scope Region between opening brace and matching closing brace Function prototype scope Applies to variable names used in function prototype, Example: int dummy(int size); File scope Can be accessed by all functions in the file (or several files).
Department of Electrical, Electronics, and Communication Engineering Scopes contd. Block Scope sum and i: their scope is all the function q: it has the scope indicated below. double dummy(int a) double sum; int i; for(i = 0; i < 10; i++) double q= i * a; // starts of scope for q // end of scope of q
Department of Electrical, Electronics, and Communication Engineering Scopes contd. File Scope # include <stdio.h> int length = 10; // a variable with file scope void main() double dummy(int a) double sum; int i; for(i = 0; i < length; i++) double q= i * a; // starts of scope for q // end of scope of q
Department of Electrical, Electronics, and Communication Engineering Linkage Linkage Variable with block or prototype scope don t have linkage. Variable with file scope have either internal or external linkage Internal Linkage The variable can be used anywhere inside a single file. External Linkage The variable can be used in a multifile program. int gains = 7; // external linkage static int size = 5; // internal linkage
Department of Electrical, Electronics, and Communication Engineering Storage Classes Storage Classes [Duration] auto specifier The memory location reserved for the variable is temporary. The auto keyword is rarely used. auto int ii = 0; /* equivalent to (int ii = 0) */ static specifier The variable has permanent duration; it is not deleted when the program exits the scope where the static variable is defined. #include <stdio.h> int add2(int a, int b) static int counter = 1; /* static (permanent) variable */ printf( This is call number %d\n, counter++); return(a + b); void main() int ii = 0, jj = 1; for(ii = 5; ii >=0; ii--, jj++) printf( %d\n, add2(ii, jj)); /* prints 6, six times */
Storage Classes contd. Hiding Variables /* hiding.c -- variables in blocks */ #include <stdio.h> int main() int x = 30; /* original x */ printf("x in outer block: %d\n", x); int x = 77; /* new x, hides first x */ printf("x in inner block: %d\n", x); printf("x in outer block: %d\n", x); while (x++ < 33) /* original x */ int x = 100; /* new x, hides first x */ x++; printf("x in while loop: %d\n", x); printf("x in outer block: %d\n", x); Department of Electrical, Electronics, and Communication Engineering return 0;
Storage Classes contd. register Specifier Department of Electrical, Electronics, and Communication Engineering Variable is saved at the CPU registers. Fast access to the variable. However, register keyword is a suggestion to the processor and a register variable might not be saved in a CPU register. #include <stdio.h> void main() register int ii = 0, jj = 1; for(ii = 5; ii >=0; ii--, jj++) printf( %d\n, (ii + jj)); /* prints 6, six times */ extern Specifier A variable with extern specifier is a reference to a variable with the same name defined at the external level of any of the source files in the program.
Storage Classes contd. extern Specifier contd. Example #include <stdio.h> int ii = 5; Department of Electrical, Electronics, and Communication Engineering int add2(int a, int b) /* ii is redefined, global ii is no more visible */ int ii = (a + b); return(ii); void main() extern int ii; /* reference to the external level ii */ for(ii = 10; ii >=0; ii--) printf( %d\n, add2(ii, ii));
malloc() Function Allocates a block of memory. Department of Electrical, Electronics, and Communication Engineering Allocated Memory: malloc() and free() Input is the number bytes to reserve in memory. No name is assigned to the reserved memory block. Therefore, use a pointer to handle the reserved block. Returns a pointer to the first byte of the memory block. double * ptd; ptd = (double *) malloc(30 * sizeof(double)); ptd pointer holds the address of the first reserved byte. Therefore, ptd can be used to access the elements of the block. ptd[1], ptd[5], and ptd[n] are second, sixth, and n-th elements of the block.
Department of Electrical, Electronics, and Communication Engineering Allocated Memory: malloc() and free() contd. Example Ask the maximum size and then allocates enough space. # include <stdio.h> void main() double * ptd; int ii = 0, size = 0; puts( Enter the maximum number of elements ); scanf( %d, &size); ptd = (double *) malloc(size * sizeof(double)); for(ii = 0; ii < size;ii++) ptd[ii] = rand() % 10;
free() Function Department of Electrical, Electronics, and Communication Engineering Allocated Memory: malloc() and free() contd. Allocated memory (using malloc()) is not deleted automatically. So, the memory used by the program increases with every malloc() use. Use free() to release the reserved memory blocks allocated using malloc(). double * ptd; ptd = (double *) malloc(30 * sizeof(double)); // your code free(ptd);
Allocated Memory: calloc() calloc() Function It differs from malloc() in that Department of Electrical, Electronics, and Communication Engineering It takes two arguments Number of elements Element size (in Byte) It initializes all the elements of the memory block to 0. double * ptd; ptd = (double *) calloc(30, sizeof(double)); // your code free(ptd);
Summary & Discussion Department of Electrical, Electronics, and Communication Engineering Explained Variable Scopes, Storage Classes, and Linkage Explained Memory Allocation Using malloc() Explained Memory Allocation Using calloc() Explained Memory Release Using free()