Vous êtes sur la page 1sur 15

Dynamic Memory Allocation (DMA):

The process of allocating and freeing memory at run time is known as Dynamic Memory Allocation. This reserves the memory required by the program and returns this resource to the system once the use of reserved space utilized. There are 4 library functions malloc( ), calloc( ), free( ) and realloc( ) for memory management. Theses functions are defined within header file stdlib.h and alloc.h 1) malloc( ): It allocates requested size of bytes and returns a pointer to the first byte of the allocated space. Its syntax is as ptr=(data_type* malloc(size_of_block); where ptr is a pointer of type data_type. The malloc( ) returns a pointer to an area of memory with size of_block. For example: x=(int*)malloc(10*sizeof(int)); A memory space equivalent to 100 times the size of an integer (i.e. 10*2 bytes = 20 bytes) is reserved and the address of the first byte of the memory allocated is assigned to the pointer x of type int (i.e. x refers the first address of allocated memory). 2) calloc( ): The function calloc( ) provides access to the C memory heap which is available for dynamic allocation of variable_size block of memory. Unlike malloc( ), the function calloc( ) accepts two arguments: no_of_blocks and size_of_block. This parameter no_of_blocks specifies the number of items to allocate and size_of_block specifies the size of each item. The function calloc( ) allocates multiple blocks of storage, each of the same size and then sets all bytes to zero. One important difference between malloc( ) and calloc( ) is that calloc( ) initializes all bytes in the allocated block to zero. Thus, it is normally used for requesting memory space at runtime for storing derived data type such as arrays user defined. Its syntax is ptr=(data_type*calloc(no_of_block,size_of_each_block); For example: x=(int*)calloc(5,10*sizeof(int)); or x=(int*)calloc(5,20); The above statement allocates contiguous space for 5 blocks, each of size 20 bytes i.e. we can store 5 arrays, each of 10 elements of integer types. 3) free( ): The built-in function frees previously allocated space by calloc, malloc or realloc function. The memory dynamically allocated is not returned to the system until the programmer returns the memory explicity. This can be done using free( ) function. Ths, this function is used to release the space when it is not required. Its syntax is free(ptr);
1

Where ptr is a pointer to a memory block which has already been created by malloc( ), calloc( ) or realloc( )function. 4) realloc( ): This function is used to modify the size of previously allocated space. Sometimes, the previously allocated memory is not sufficient, we nned additional space and sometime the allocated memory is much larger than necessary. In both situations, we can change the memory size already allocated with the help of function realloc( ). Its syntax is as If the original allocation is done by the statement, ptr=malloc(size); then reallocationof space may be done by the statement ptr=realloc(ptr,newsize);

Application of pointer:
1) Accessing array elements 2) Returning more than one value from a function 3) Accessing dynamically allocated memory 4) Implementing data structure like linked lists, trees, and graphs. 5) Increasing the execution speed as they refer address.
1) Macro substitution directives: A macro is a preprocessor directive which is a program that processes source code before it passes through the compiler. These are placed in the source program before the main(). To define macro, # define statement is used. This statement, also known as macro definition and have the following general form: #define identifier string For Examples: 1. #define PI 3.14 2. #define NAME Rafael 3. #define CITY Butwal 4. #define MARKS 100 Example illustrating the macro concept to calculate area of circle #include<stdio.h> #include<conio.h> #define PI 3.14 void main() { int radius; float area_of_circle; printf("**********Program to calculate area of circle using Macro***********\n"); printf("\nEnter the radius: "); scanf("%d",&radius); area_of_circle=PI*radius*radius; printf("Area of circle is %f",area_of_circle); getch(); } 2) File inclusion directives: This is a process of inserting external files containing functions or macro definitions into the C program. This avoids rewriting those functions or macro definitions. An external file containing function can be included in C program using #include directive. The syntax of #include is as follows:

#include filename Where, #include is the preprocessor directive for file inclusion filename is the file containing the required function definition to be included in C program. There is no space between #symbol and include. But, there must be at least one blank spaces between #include and filename. Filename can be written within pair of double quotations marks as shown below: #include Cube.c Example: Assume that you are computing the cube of a given number. The function for finding the cube is not available in the standard libraries. Now create a file named Cube.c and write the code for it. Then use #include Cube.c in your program as shown below. Program #include<stdio.h> #include<conio.h> #include "Cube.c" void main( ) { int num,cb; printf("Enter the number:"); scanf("%d",&num); cb=cube(num); printf("\nThe cube of the number=%d",cb); getch(); }

2) 2005 fall, Q.2.a Write an algorithm & draw flowchart to check whether a given number is palindrome or not. Algorithm: Step 1: start Step2: initialize reverse=0 Step 3: input number Step4: copy number to a [a=number ] Step5: calculate: b= number%10 reverse=(reverse*10)+b number=number/10 Step 6: repeat step 5 until condition (number>=1) satisfied Step 7: check whether (reverse==a)? If yes, Display the given number is palindrome Else, display the given number is not palindrome. Step 8: end

3) 2008 fall, Q.4b Write a menu driven program to display the following menu and perform the respective task until user wants to exit from the program. MENU 1) To convert 0C to Farahnite ( F=9/5*c+32) 2) Display even number between 101 to 200 3) Exit. Program:
#include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> void main()

{ int i,choice,c; float f; begin: printf("\n1: To convert celsius into farahnite"); printf("\n2:To print even number from 101 to 200"); printf("\n3:To exit from output screen"); printf("\nEnter your choice:"); scanf("%d",&choice); switch(choice) { case 1: printf("\n Enter the value in celsius: "); scanf("%d",&c); f=(9/5)*c+32; printf("farahnite= %f",f); break; case 2: for(i=101;i<=200;i++) if(i%2==0) printf("%d\t",i); break; case 3: exit(0); } getch(); printf("You want to check again, then press any key to continue:"); goto begin; }

7) 2006fall,Q.3a Write a program that calculates the following sequence 1/1!+2/2!+3/3!+.+n/n! Where n is the number input by user. Program #include<stdio.h> #include<conio.h> void main() { float count=1,fact,n,i; float sum=0,b; printf("enter the value for n:"); scanf("%f",&n); while(count<=n) { fact=1; for(i=1;i<=count;i++) { fact=fact*i; } b=count/fact; sum=sum+b; count++26

} printf("sum=%f\t",sum); getch(); }
2005 fall, Q.5 a WAP to take a string & find out how many vowels are there? Program: h#include<stdio.h> #include<conio.h> #include<string.h> void main() { char string[100]; int i,count=0; printf("Enter the string: "); gets(string); for(i=0;string[i]!='\0';i++) { if(string[i]=='A'||string[i]=='E'||string[i]=='I'||string[i]=='O'||string[i]=='U'||string[i]=='a'||string[i]=='e'||string[i]=='i'|| string[i]=='o'||string[i]=='u') { count++; } } printf("Total number of vowel= %d",count); getch(); }

Exam 2009(spring) #include<stdio.h> #include<conio.h> void main() { int i; for(i=1;i<=100;i=i+2) printf("%d\t",i); getch(); } #include<stdio.h> #include<conio.h> void main() { int num[50],temp,j,i,n; printf("enter number:"); 1

scanf("%d",&n); for(i=0;i<n;i++) scanf("%d\t",&num[i]); for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(num[i]>num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } printf("the num in accending order:"); for(i=0;i<n;i++) printf("%d\t",num[i]); getch(); } #include<stdio.h> #include<conio.h> void sum(int n[]) { int sum=0,i; for(i=0;i<5;i++) sum=sum+n[i]; printf("\t%d",sum); } void main() { int a[5]={2,4,5,7,8}; clrscr(); sum(a); getch(); } #include<stdio.h> #include<conio.h> void main() { int r,q,sum=0; long int num; clrscr(); printf("enter 5 digit number:"); scanf("%ld",&num); do { q=num/10; r=num%10; num=q; 1

sum+=r; }while(q!=0); printf("the sum of 5 digit integer is:%ld",sum); getch(); } #include<stdio.h> #include<conio.h> #define m 3 #define n 3 void main() { int matrix[m][n],matrix1[m][n],i,j; printf("enter matrix of order %d*%d\n",m,n); for(i=0;i<m;i++) for(j=0;j<n;j++) { printf("\nmatrix[%d][%d]=",i,j); scanf("%d",&matrix[i][j]); } printf("the entered matrix is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d",matrix[i][j]); } printf("\n"); } for(i=0;i<m;i++) for(j=0;j<n;j++) matrix1[j][i]=matrix[i][j]; printf("the transpose matrix are:\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { printf("%d",matrix1[i][j]); } printf("\n"); } getch(); } #include<stdio.h> #include<conio.h> #include<string.h> void main() 1

{ char st1[50],st2[50]; int diff; clrscr(); printf("enter string:"); gets(st1); strcpy(st2,st1); strrev(st1); diff=strcmp(st1,st2); if(diff==0) printf("the string is palindrome!"); else printf("rhe string id not palindrome:"); getch(); }

2006 Fall,Q.4b
Write a program using structure to read name,roll,marks in three subjects of 20 students and prints the record in ascending order of the total marks obtained in three subjects. Program #include<stdio.h> #include<conio.h> void main() { struct data { char name[10]; int roll; int m1,m2; int total; }d[20],temp; int i,n; printf("Enter how many students are there="); scanf("%d",&n); printf("\nEnter The Records"); for(i=0;i<n;i++) { printf("\n\nEnter %d record\n",i+1); printf("Name="); scanf("%s", d[i].name); printf("Roll="); scanf("%d", &d[i].roll); printf("marks 1st="); scanf("%d", &d[i].m1); printf("Marks 2nd="); scanf("%d", &d[i].m2);
1

} for(i=0;i<n;i++) { d[i].total=d[i].m1+d[i].m2; } for(i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(d[i].total>d[j].total) { temp=d[i]; d[i]=d[j]; d[j]=temp; } } } printf("\n\nName\tRoll\tM1 \tm2\tTotal\n\n"); for(i=0;i<n;i++) printf("\n%s\t%d\t%d\t%d\t%d",d[i].name,d[i].roll,d[i].m1,d[i].m2,d[i].total); getch(); } Output:

2003 spring, Q.6b Write a complete program to read 100 students record with the following fields and display record of BBA faculty only. Rollno Name Faculty DOB(date of birth)
1

Program

#include<stdio.h> #include<conio.h> #include<string.h> void main() { struct DOB { int day; int month; int year; }; struct student { char name[10]; int roll; char faculty[20]; struct DOB d[100]; }s[100]; int i,n; printf("Enter how many students's records you want to Enter and Display: "); scanf("%d",&n); printf("\nEnter The Records"); for(i=0;i<n;i++) { printf("\n\nEnter %d record\n",i+1); printf("Student Name: "); scanf("%s", s[i].name); printf("Roll No.: "); scanf("%d",&s[i].roll); printf("Faculty: "); scanf("%s",s[i].faculty); printf("DOB: "); printf("Day: "); scanf("%d",&s[i].d[i].day); printf("Month: "); scanf("%d",&s[i].d[i].month); printf("Year: "); scanf("%d",&s[i].d[i].year); }
1

printf("\n\nStudent Name\tRoll No.\tFaculty\tDOB\n\n"); for(i=0;i<n;i++) { if(strcmp(s[i].faculty,"BBA")==0) { printf("\n%s\t\t%d\t\t%s\t%d-%d%d",s[i].name,s[i].roll,s[i].faculty,s[i].d[i].day,s[i].d[i].month,s[i].d[i].year); } } getch(); }

2009 fall, Q.5b: WAP in C to multiply two rectangular matrices and display the resultant matrix. Program: #include<stdio.h> #include<conio.h> void main() { int i,j,k,a[2][3],b[3][2],c[2][2],parsum=0; for(i=0;i<2;i++) { for(j=0;j<3;j++) { printf("\nEnter value for a[%d][%d]",i,j); scanf("%d",&a[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<2;j++) { printf("\nEnter value for b[%d][%d]",i,j); scanf("%d",&b[i][j]); } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { for(k=0;k<3;k++) { parsum+=a[i][k]*b[k][j]; }
1

c[i][j]=parsum; parsum=0; } } printf("\n\n***********The Multiplication of two rectangular matrices*************\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("\t%d ",c[i][j]); } printf("\n"); } getch(); } 2005 fall,Q.6 b) Create a file named university.dat. WAP to keep the records Of N colleges under Pokhara University in a file. These record contains name, location and no_of_faculties of the college and display the names of colleges in Kathmandu location. Program: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> void main() { struct college { char name[20]; char location[20]; int no_of_faculties; }c[5]; FILE *f; int N,i; f=fopen("c:\\university.txt","w"); [Note: write c:\\university.dat according to question] if(f==NULL) { printf("File cannot be created"); exit(0); } printf("Enter value for N:"); scanf("%d",&N); for(i=0;i<N;i++) { printf("Enter college name: ");
1

scanf("%s",c[i].name); printf("Enter location name: "); scanf("%s",c[i].location); printf("Enter Number of faculties: "); scanf("%d",&c[i].no_of_faculties); } printf("\nWriting information to a file....\n"); fprintf(f,"College_Name\t\tLocation\t\tNo._of_faculties\n"); for(i=0;i<5;i++) { fprintf(f,"\n%s\t\t\t%s\t\t\t%d\n",c[i].name,c[i].location,c[i].no_of_faculties); } printf("\nReading same content from file.....\n"); fscanf(f,"%s%s%d",c[i].name,c[i].location,&c[i].no_of_faculties); printf("College_Name\t\tLocation\t\tNo._of_faculties\n"); printf("\n................................................................\n"); for(i=0;i<5;i++) { if(strcmp(c[i].location,"Kathmandu")==0) printf("\n%s\t\t\t%s\t\t\t%d\n",c[i].name,c[i].location,c[i].no_of_faculties); printf("\n"); } fclose(f); getch(); }

2006 fall,Q. 3,b: WAP to read the name, age and basic salary of an employee from keyboard and write these data to a file. Program: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> void main() { FILE *fptr; char name[20][10]; int age[10]; float basic_salary[10]; int N,i; fptr=fopen("c:\\employee.txt","w"); if(fptr==NULL) { printf("File cannot be created");
1

exit(0); } printf("Enter value for N:"); scanf("%d",&N); for(i=0;i<N;i++) { printf("Enter Employee name: "); scanf("%s",&name[i]); printf("Enter age: "); scanf("%d",&age[i]); printf("Enter basic salary: "); scanf("%f",&basic_salary[i]); } fprintf(fptr,"\nName\t\tAge\t\tBasic Salary\n"); for(i=0;i<N;i++) { fprintf(fptr,"%s\t\t%d\t\t%f\n",name[i],age[i],basic_salary[i]); } fclose(fptr); getch(); }

2009 Spring,Q.5 a) WAP to open a file name student.text to keep the records of students (roll_no, name, course, semester) in write mode and perform the following operations: i) Insert records in the file. ii) Display all those records for which course is BBA and semester is 2. Program: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> void main() { struct student { char name[20]; int roll; char course[20]; int semester; }s[5]; int i; FILE *f; f=fopen("c:\\student.txt","w+");
1

if(f==NULL) { printf("File cannot be created!!!"); exit(0); } for(i=0;i<5;i++) { printf("Enter information of student No %d\n",i+1); printf("Name: "); scanf("%s",s[i].name); printf("Enter roll: "); scanf("%d",&s[i].roll); printf("Enter course: "); scanf("%s",s[i].course); printf("Enter semester: "); scanf("%d",&s[i].semester); } printf("\nWriting information to a file....\n"); fprintf(f,"Name\t\tRoll\t\tCourse\t\tSemester\n"); for(i=0;i<5;i++) { fprintf(f,"\n%s\t\t%d\t\t%s\t\t%d\n",s[i].name,s[i].roll,s[i].course,s[i].semester); } printf("\nReading same content from file.....\n"); fscanf(f,"%s%d%s%d",s[i].name,&s[i].roll,s[i].course,&s[i].semester); printf("Name\t\tRoll\t\tCourse\t\tSemester\n"); printf("\n.......................................................\n"); for(i=0;i<5;i++) { if(strcmp(s[i].course,"BBA")==0) if(s[i].semester==2) printf("%s\t\t%d\t\t%s\t\t%d\n",s[i].name,s[i].roll,s[i].course,s[i].semester); printf("\n"); } fclose(f); getch(); }

Vous aimerez peut-être aussi