Dynamic Memory Allocation
- It is used to allow the allocation of memory at runtime.
- There are four function are used to achieve dynamic memory allocation.
- malloc()
- calloc()
- realloc()
- free()
malloc()
- It stands for Memory Allocation.
- It allocates/reserves a single block of memory of specified size.
- It returns NULL if memory is insufficient.
- malloc function returns the address of first byte in the allocated memory if memory is sufficient.
- Syntax
ptr=(cast-type*)malloc(byte-size)
- Here ptr is the pointer, cast-type is the data type in which we want to cast the returning pointer,byte-size is the allocated memory space.
- Example
p = (int*) malloc(50 * sizeof(int));
- Here in the above example the given statements allocates 100 or 200 bytes of memory.If the size of int is 2 then 50*2=100 bytes will be allocated,if the size of int is 4 then 50*4=200 bytes will be allocated.Pointer p holds the address of first byte of allocated memory.
calloc()
- It stands for contiguous allocation.
- It allocates/reserves a multiple block of memory of same size.
- The allocated memory space is initialized to zero.
- It returns NULL if memory is insuffient.
- Syntax
ptr=(cast-type*)calloc(number of block,size of each block)
- Here ptr is the pointer,cast-type is the data type in which we want to cast the returning pointer.
- Example
p = (float*) calloc(50 , sizeof(float));
- Here in the above example the given statements allocates 50 block of memory space with size of 4 byte because size of float is 4 byte.
realloc()
- If the allocated memory space is insufficient then it is possible to modify the allocated space.
- realloc function is used to modify the size of previously allocated memory.
- Syntax
ptr = realloc(ptr, new size );
free()
- This function is used to free the allocated memory space.
- Syntax
free(ptr);
Example:malloc
Find sum of given value#include <stdio.h> #include <stdlib.h> int main() { //declaring variable float sum=0,*p; int no; printf("How many elements you want to add\n"); scanf("%d", &no);//taking input //allocating memory space p= (float*) malloc(no * sizeof(float)); if(p == NULL) { printf("Insufficient space,memory not allocated."); exit(0); } //taking inputs of element for(int i = 0; i < no; ++i) { printf("Enter element %d\n",i+1); scanf("%f", p + i); //finding the sum of elements sum =sum+ *(p + i); } //Printing the sum of element printf("Total Sum of element= %f", sum); //free the allocated space free(p); return 0; } /* ### Output ### How many elements you want to add 4 Enter element 1 3.4 Enter element 2 9.3 Enter element 3 9.2 Enter element 4 4.3 Total Sum of element= 26.200001 */
Example:calloc
same program with calloc#include <stdio.h> #include <stdlib.h> int main() { //declaring variable float sum=0,*p; int no; printf("How many elements you want to add\n"); scanf("%d", &no);//taking input //allocating memory space p= (float*) calloc(no,sizeof(float)); if(p == NULL) { printf("Insufficient space,memory not allocated."); exit(0); } //taking inputs of element for(int i = 0; i < no; ++i) { printf("Enter element %d\n",i+1); scanf("%f", p + i); //finding the sum of elements sum =sum+ *(p + i); } //Printing the sum of element printf("Total Sum of element= %f", sum); //free the allocated space free(p); return 0; } /* ### Output ### How many elements you want to add 4 Enter element 1 3.4 Enter element 2 9.3 Enter element 3 9.2 Enter element 4 4.3 Total Sum of element= 26.200001 */
Here
I am requesting if you found this post was helpful for you then let me
know by your comment in below comment box and share it with your friends.
Also you can contact me on Instagram @rajusunagar_
If
you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other.
Thank you!!.......
0 Comments