Dynamic Memory Allocation
Why Dynamic memory Allocation(DMA)?
Consider a declaration of array of size 10,int a[10]
- Now as we have declare an array of size 10,so this declaration will reserve 20 bytes for array a in the memory.As array is of int type each element will need 4 bytes so total becomes to 40 Bytes.
- Till now there is no problem with ordinary way,But what if there was an need of only 5 elements , this makes wastage of memory
- What if , we want to store more than 10 elements.
- Other Reason of Using Dynamic memory Allocation is that we can increase the size at the runtime.
- There are some memory allocations functions with the help of them you can solve above problems.
malloc() function
Syntax : malloc() function
(cast_type *) malloc(bytes);
If cast type is not specified,than malloc() function returns void pointer.
(cast_type *) malloc(bytes);
Let's use malloc() function to allocate memory to integer pointer
#include<stdio.h>
void main()
{
int *p;
p = (int*)malloc(sizeof(int));
// malloc() function will return integer pointer.
}
#include<stdio.h> void main() { int *p; p = (int*)malloc(sizeof(int)); // malloc() function will return integer pointer. }
allocating memory to float pointer
#include<stdio.h>
void main()
{
float *p;
p = (float*)malloc(sizeof(float));
// malloc() function will return float pointer.
}
#include<stdio.h> void main() { float *p; p = (float*)malloc(sizeof(float)); // malloc() function will return float pointer. }
allocating memory to char pointer
#include<stdio.h>
void main()
{
char *p;
p = (float*)malloc(sizeof(char));
// malloc() function will return float pointer.
}
#include<stdio.h> void main() { char *p; p = (float*)malloc(sizeof(char)); // malloc() function will return float pointer. }
Now we will create array by allocating memory dynamically by malloc()
allocating memory for 10 blocks of int type
#include<stdio.h>
void main()
{
int *p;
p = (int*)malloc(sizeof(int)*10);
//it will allocate 40 bytes
//it will return pointer to first byte.
}
#include<stdio.h> void main() { int *p; p = (int*)malloc(sizeof(int)*10); //it will allocate 40 bytes //it will return pointer to first byte. }
Solution:
#include<stdio.h>
void main()
{
int i;
int *p;
p = (int*)malloc(sizeof(int)*10);
for(i=0;i < 10;i++)
{
printf("Enter the student roll no:");
scanf("%d",p+i);
}
}
#include<stdio.h> void main() { int i; int *p; p = (int*)malloc(sizeof(int)*10); for(i=0;i < 10;i++) { printf("Enter the student roll no:"); scanf("%d",p+i); } }
Solution:
#include<stdio.h>
void main()
{
int i;
float *p;
p = (float*)malloc(sizeof(float)*10);
for(i=0;i < 10;i++)
{ printf("Enter the Temperature:");
scanf("%f",p+i);
}
printf("\ntemperature of past 10 days was: ");
for(i=0;i<10;i++)
{
printf("%f\t",*(p+i));
}
}
#include<stdio.h> void main() { int i; float *p; p = (float*)malloc(sizeof(float)*10); for(i=0;i < 10;i++) { printf("Enter the Temperature:"); scanf("%f",p+i); } printf("\ntemperature of past 10 days was: "); for(i=0;i<10;i++) { printf("%f\t",*(p+i)); } }
Solution:
#include<stdio.h>
void main()
{
int i;
int *p;
p = (int*)malloc(sizeof(int)*10);
for(i=0;i < 10;i++)
{
printf("\nEnter the Character:");
scanf("%d",(p+i));
}
printf("\nEven number are : ");
for(i=0;i < 10;i++)
{ if(*(p+i) %2 == 0)
{
printf("%d\t",*(p+i));
}
}
}
#include<stdio.h> void main() { int i; int *p; p = (int*)malloc(sizeof(int)*10); for(i=0;i < 10;i++) { printf("\nEnter the Character:"); scanf("%d",(p+i)); } printf("\nEven number are : "); for(i=0;i < 10;i++) { if(*(p+i) %2 == 0) { printf("%d\t",*(p+i)); } } }
0 Comments
Post a Comment