OBJECTIVE: Today, scope rules, storage classes, and dynamic memory allocation will be discussed. Memory Allocation Dimension of an array can be determined in two ways; static or dynamic. Static allocation is done at the beginning of the program, which does not allow to change the dimension of the array. However, dynamic allocation gives a flexibility to adjust the dimension of the array. Since it is a dynamic allocation, the dimension of the array can be assigned everywhere in the program. calloc() and malloc() are used to create memory allocation dynamically in the program. A function call of the form calloc(n,dimsize) allocates contiguous memory for an array of n elements with each element having dimsize bytes. The space is initialized with all bits set to zero. If the call is successful, a pointer of type void that points the base of the array in memory is returned; otherwise, NULL is returned. The malloc() is used in a similar manner, if the call is successful, it returns a pointer of type void that points to the requested space in memory; otherwise, NULL is returned. Unlike calloc(), the function malloc() does not initialize the space in memory that it makes available. Space that has been dynamically allocated with either calloc() or malloc() is deallocated by using free(ptr) where the memory is pointed by ptr. If ptr is NULL, the function has no effect. If ptr is not NULL, it must be the base address of space previously allocated by a call to calloc(), malloc(). Let us consider the below simple program; #include<stdio.h> #include<stdlib.h> main() int *a,*b,n; printf("enter the dimension of array:"); scanf("%d",&n); a=calloc(n,sizeof(int)); b=malloc(n*sizeof(int)); Relation of Multidimensional arrays with Pointers The relation between pointers and multidimensional arrays are valid as well as the one dimensional arrays. Let the matrix A has a dimension 3 5 and B has a dimension 7 9 2. Then, A[i][j] is equivalent (&A[0][0] + 5 i + j), B[i][j][k] is equivalent (&B[0][0][0] + 9 2 i + 2 j + k). Scope Rules: The fundamental idea for scoping is that the identifiers are accessible only within the block in which they are declared. Moreover, they are unknown outside of the boundaries of that block. 1
main() int a=2; printf("a=%d\n",a); int a=5; printf("a=%d\n",a); printf("a+1=%d\n",++a); Exercise: Write the output of this program without writing the code. #include<stdio.h> main() int a=1,b=2,c=3; int b=4; float c=5.2; printf("a= %d, b=%d, c=%f\n",a,b,c); a=b; int c; c=b; printf("a= %d, b=%d, c=%f\n",a,b,c); Storage Classes: Every variable and function in C has two basic property: type and storage class. In C, there exists 4 different storage classless ; automatic, external, register, and static with corresponding keywords are auto, extern,register, and static. Automatic Storage Class The variables defined within the function bodies are automatic by default. The automatic storage class is the most common storage class. This stems from two reasons: the variables are defined in the function body, they exist only when the function is called, and these variables disappear when the function is exited. Therefore, these variables are local variables. Declarations of variables in the functions are implicitly automatic. The keyword auto is used explicitly identify the variable which is a member of automatic storage class. auto float a=1; float b; auto int c; 2
a, b, c are member of automatic storage class. External Storage Class To transmit the variables across the functions and blocks, external variables are used. These variables can be accessed by name by any function. When a variable is declared outside a function, storage is permanently assigned to it, and its storage class is extern. External variables never disappear. Because they exist throughout the execution life of the program. Exercise: What should be the output of this program?. extern int a=2, b=3, c=5; int func() int b,c; b=4; c=5; a=c-b; c++; return(b++); main() printf("func returns: %d\n",func()); Register Storage Class The storage class register tells the compiler that the associated variables should be stored in high-speed memory registers. The storage class register is used to improve the execution speed. When the execution speed is concern, a few variables can be defined in register storage class. register int i; /* also it can be written as register i*/ for(i=0;i<limit;i++)... Static Storage Class Static declarations have two important and distinct uses. One of them is to allow a local variable to retain its previous value when the block is reentered. The second use is in connection with external declaration. 3
Exercise: What should be the output of this program?. int functi(int a) static int c=0; c++; return c+a; main() int result,i=0; while(i<=4) result=functi(i); i++; printf("result is %d\n",result); Enumeration Types: The keyword enum is used to declare enumeration types. It provides a means of naming a finite set, and of declaring identifiers as elements of the set. Consider the below declaration; enum daysun, mon, tue, wed, thu, fri, sat; This creates the user-defined type enum day. The keyword enum is followed by the tag name day. The enumerators are the identifiers sun, mon, tue, wed, thu, fri, sat, which are constants of type int. By default, the first one is 0, and each succeeding one has the next integer value. To declare a variable with a type enum day; enum day d1,d2; main() enum daysun=1,mon,tue,wed,thu,fri,sat; enum day d1,d2; d1=fri; d2=3; printf("%d %d",d1,d2); main() enum daysun=12,mon,tue=25,wed,thu,fri,sat; enum day d1,d2; d1=fri; d2=3; printf("%d %d",d1,d2); 4
#include <stdlib.h> enum daysun,mon,tue,wed,thu,fri,sat; typedef enum day day; day find_next_day(day d) day next_day; switch(d) case sun: next_day=mon; case mon: next_day=tue; case tue: next_day=wed; case wed: next_day=thu; case thu: next_day=fri; case fri: next_day=sat; case sat: next_day=sun; return next_day; main() printf("%d",find_next_day(5)); 5
#include <stdlib.h> enum daysun,mon,tue,wed,thu,fri,sat; typedef enum day day; day find_next_day(day d) day next_day; return ((day)(((int)(d)+1) % 7 )); main() printf("%d",find_next_day(12)); 6