180 likes | 385 Views
Dynamic memory allocation. Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating memory at run time. The following functions are used in c for purpose of memory management.
E N D
Dynamic memory allocation • The process of allocating memory at run time is known as dynamic memory allocation. • C have four library functions for allocating memory at run time. • The following functions are used in c for purpose of memory management.
Dynamic memory allocation malloc() • Allocates memory requests size of bytes and returns address to a pointer. • calloc() • Allocates space for an array of elements initializes them to zero and returns a pointer to the memory • free() • Frees previously allocated space • realloc() • Modifies the size of previously allocated space.
Dynamic memory allocation • Memory allocations process • The memory is divided into 3 • Stack • Heap • Permanent storage • Memory space stack used to allocate static memory allocations ie ordinary variables & arrays • Permanent storage is used to store program code • Heap is used to allocate dynamic memory allocations • Heap is depended to stack & code Stack Heap Permanent Storage
Dynamic memory allocation • Memory allocations process • The free memory region is called the heap. • The size of heap keeps changing when program is executed due to creation and death of variables. • Therefore it is possible to encounter memory overflow during dynamic allocation process. • In such situations, the memory allocation functions mentioned above will return a null pointer.
Dynamic memory allocation • Allocating a block of memory: • A block of memory may be allocated using the function malloc(). • The malloc function reserves a block of memory of specified size and returns a pointer of type void. • This means that we can assign it to any type of pointer. It takes the following form: ptr=(cast-type*)malloc(byte-size);
Dynamic memory allocation • ptr is a pointer of type cast-type the malloc() returns a pointer (of cast type) to an area of memory with size byte-size. • Example: • a memory equivalent to 100 times the area of int bytes is reserved and the address of the first byte of memory allocated is assigned to the pointer x of type int x=(int*)malloc(100*sizeof(int));
Dynamic memory allocation • Allocating multiple blocks of memory • Calloc is another memory allocation function that is normally used to request multiple blocks of storage each of the same size and then sets all bytes to zero. • The general form of calloc is: • The above statement allocates contiguous space for n blocks each size of elements size bytes. • All bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not enough space a null pointer is returned. ptr=(cast-type*) calloc(n,elem-size);
Dynamic memory allocation • Releasing the used space: • Compile time storage of a variable is allocated and released by the system in accordance with its storage class. • With the dynamic runtime allocation, it is our responsibility to release the space when it is not required.
Dynamic memory allocation • Releasing the used space: • The release of storage space becomes important when the storage is limited. When we no longer need the data we stored in a block of memory and we do not intend to use that block for storing any other information, we may release that block of memory for future use, using the free function. • ptr is a pointer that has been created by using malloc or calloc. free(ptr);
Dynamic memory allocation • To alter the size of allocated memory: • The memory allocated by using calloc or malloc might be insufficient or excess sometimes in both the situations we can change the memory size already allocated with the help of the function realloc(). • This process is called reallocation of memory. • The general statement of reallocation of memory is : • This function allocates new memory space of size newsize to the pointer variable ptrans returns a pointer to the first byte of the memory block. • The allocated new block may be or may not be at the same region. ptr=realloc(ptr,newsize);
Dynamic memory allocation • malloc() example • #include <stdio.h> • #include <conio.h> • #include <alloc.h> • void main() • { • int *ptr; • ptr=(int *)malloc(2); • scanf("%d",ptr); • printf("value= %d",*ptr); • free(ptr); /* free allocated memory */ • getch(); • }
Dynamic memory allocation • ptr=ptr-n; • printf("\n given nos "); • for(i=1;i<=n;i++) • { • printf("%d ",*ptr); • ptr++; • } • free(ptr); /* free allocated memory */ • getch(); • } • Calloc() example • #include <stdio.h> • #include <conio.h> • #include <alloc.h> • void main() • { • int *ptr,i,n; • printf("enter limit "); • scanf("%d",&n); • ptr=(int *)calloc(n,2); • for(i=1;i<=n;i++) • { • printf("Enter %d no :- ",i); • scanf("%d",ptr); • ptr++; • }
Dynamic memory allocation • ptr=ptr-n; • printf("\n given nos "); • for(i=1;i<=n;i++) • { • printf("%d ",*ptr); • ptr++; • } • ptr=(int *)realloc(ptr,3); • free(ptr); /* free allocated memory */ • getch(); • } • Realloc() example • #include <stdio.h> • #include <conio.h> • #include <alloc.h> • void main() • { • int *ptr,i,n; • printf("enter limit "); • scanf("%d",&n); • ptr=(int *)calloc(n,2); • for(i=1;i<=n;i++) • { • printf("Enter %d no :- ",i); • scanf("%d",ptr); • ptr++; • }