Memory allocation in c programmming
Memory allocation in c...
- C provide features to manual managements of memory, by using this features we can manage memory at run time,.
- whenever we require memory allocation or reallocation at run time by using Dynamic Memory Allocation functions we can create amount of required memory.
There are following functions:
- malloc - It is used to allocate specified number of bytes .
- calloc - It is used to allocate specified number of bytes and initialize all memory with 0.
- reaclloc - It is used to reallocate the dynamically allocated memory to increase or decrease amount of the memory.
- free - It is used to release dynamically allocated memory.
Dynamic Memory Allocation
01 C programs to create memory for int, char and float variable at run time.
here, In this program we will create memory for int, char and float variables at run time using malloc() function and before exiting the program we will release the memory allocated at run time by using free() function./*C program to create memory for int,char and float variable at run time.*/
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int *iVar;
- char *cVar;
- float *fVar;
- /*allocating memory dynamically*/
- iVar=(int*)malloc(1*sizeof(int));
- cVar=(char*)malloc(1*sizeof(char));
- fVar=(float*)malloc(1*sizeof(float));
- printf("Enter integer value: ");
- scanf("%d",iVar);
- printf("Enter character value: ");
- scanf(" %c",cVar);
- printf("Enter float value: ");
- scanf("%f",fVar);
- printf("Inputted value are: %d, %%.2f\n",*iVar,*cVar,*fVar);
- /*free allocated memory*/
- free(iVar);
- free(cVar);
- free(fVar);
- return 0;
- }
OUTPUT:
Enter integer value: 200Enter character value: x
Enter float value: 555.76
Inputted value are: 200, x, 555.76
02 C program to input and print texts using Dynamic Memory .
In this program we will create memory for text strings at run time using malloc() function,text string will be inputted by the user and displayed. Using free() function we will release the occupied memory.
/*C program to input and print text using Dynamic Memory Allocation.*/
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int n;
- char *text;
- printf("Enter limit of the text: ");
- scanf("%d",&n);
- /*allocate memory dynamically*/
- text=(char*)malloc(n*sizeof(char));
- printf("Enter text: ");
- scanf(" "); /*clear input buffer*/
- gets(text);
- printf("Inputted text is: %s\n",text);
- /*Free Memory*/
- free(text);
- return 0;
- }
Enter limit of the text: 100
Enter text: I am from jaipur.
Inputted text is: I am jaipur.
03 C program to read a 1D array, print sum of all elements along with inputted array elements using Dynamic Memory Allocation.
- In this program we will allocate memory for one dimensional array and print the array elements along with sum of all elements. Memory will be allocated in this program using malloc() and released using free().
/*C program to read a one dimensional array,
print sum of all elements along with inputted array
elements using Dynamic Memory Allocation.*/
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int *arr;
- int limit,i;
- int sum=0;
- printf("Enter total number of elements: ");
- scanf("%d",&limit);
- /*allocate memory for limit elements dynamically*/
- arr=(int*)malloc(limit*sizeof(int));
- if(arr==NULL)
- {
- printf("Insufficient Memory, Exiting... \n");
- return 0;
- }
- printf("Enter %d elements:\n",limit);
- for(i=0; i<limit; i++)
- {
- printf("Enter element %3d: ",i+1);
- scanf("%d",(arr+i));
- /*calculate sum*/
- sum=sum + *(arr+i);
- }
- printf("Array elements are:");
- for(i=0; i<limit; i++)
- printf("%3d ",*(arr+i));
- printf("\nSum of all elements: %d\n",sum);
- return 0;
- }
Enter total number of elements: 5
Enter 5 elements:
Enter element 1: 11
Enter element 2: 22
Enter element 3: 33
Enter element 4: 44
Enter element 5: 55
Array elements are: 11 22 33 44 55
Sum of all elements: 165
04 .C program to read and print the student details using structure and Dynamic Memory Allocation.
vIn this program we will create a structure with student details and print the inputted details.Memory to store and print structure will be allocated at run time by using malloc() and released by free().
/*C program to read and print the student details
using structure and Dynamic Memory Allocation.*/
using structure and Dynamic Memory Allocation.*/
- #include <stdio.h>
- #include <stdlib.h>
- /*structure declaration*/
- struct student
- {
- char name[30];
- int roll;
- float perc;
- };
- int main()
- {
- struct student *pstd;
- /*Allocate memory dynamically*/
- pstd=(struct student*)malloc(1*sizeof(struct student));
- if(pstd==NULL)
- {
- printf("Insufficient Memory, Exiting... \n");
- return 0;
- }
- /*read and print details*/
- printf("Enter name: ");
- gets(pstd->name);
- printf("Enter roll number: ");
- scanf("%d",&pstd->roll);
- printf("Enter percentage: ");
- scanf("%f",&pstd->perc);
- printf("\nEntered details are:\n");
- printf("Name: %s, Roll Number: %d, Percentage: %.2f\n",pstd->name,pstd->roll,pstd->perc);
- return 0;
- }
Enter name: pappu
Enter roll number: 1
Enter percentage: 87.50
Entered details are:
Name: pappu, Roll Number: 1, Percentage: 87.50
05 C program to read and print the N student details using structures and Dynamic Memory Allocation.
this program we will create a structure with N number of student details and print the inputted details. Memory to store and prints structure will be allocated at run time by using malloc() and released by free().
/*C program to read and print the N student
details using structure and Dynamic Memory Allocation.*/
- #include <stdio.h>
- #include <stdlib.h>
- /*structure declaration*/
- struct student
- {#include <stdio.h>
- #include
- char name[30];
- int roll;
- float perc;
- };
- int main()
- {
- struct student *pstd;
- int n,i;
- printf("Enter total numbers of elements: ");
- scanf("%d",&n);
- /*Allocated memory dynamically for n objetcs*/
- pstd=(struct student*)malloc(n*sizeof(struct student));
- if(pstd==NULL)
- {
- printf("Insufficient Memory, Exiting... \n");
- return 0;
- }
/*read and print details*/
for(i=0; i<n; i++)
{
printf("\nEnter detail of student [%3d]:\n",i+1);
printf("Enter name: ");
scanf(" "); /*clear input buffer*/
gets((pstd+i)->name);
printf("Enter roll number: ");
scanf("%d",&(pstd+i)->roll);
printf("Enter percentage: ");
scanf("%f",&(pstd+i)->perc);
}
for(i=0; i<n; i++)
{
printf("%30s \t %5d \t %.2f\n",(pstd+i)->name,(pstd+i)->roll, (pstd+i)->perc);
}
return 0;
}
Enter total number of elements: 3
Enter detail of student [1]:
Enter name: Mike
Enter roll number: 1
Enter percentage: 87.50
Enter detail of student [2]:
Enter name:raju
Enter roll number: 2
Enter percentage: 65
Enter detail of student [3]:
Enter name: pappu
Enter roll number: 3
Enter percentage: 98.70
Entered details are:
raju 1 65
Jussy 2 88.00
pappu 3 98.70


No comments:
Post a Comment