C langugae : Dynamic Memory Management

C dynamic memory allocation refers to performing dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely malloc, realloc, calloc and free.
The C++ programming language includes these functions for backwards compatibility; its use in C++ has been largely superseded by operators new and new[].
Many different implementations of the actual memory allocation mechanism, used by malloc, are available. Their performance varies in both execution time and required memory.
Function Description
malloc allocates the specified number of bytes
realloc increases or decreases the size of the specified block of memory. Reallocates it if needed
calloc allocates the specified number of bytes and initializes them to zero
free releases the specified block of memory back to the system

Allocating Memory Dynamically
malloc() : - The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form.
void *malloc( size_t size );
The malloc( ) function reserves a contiguous memory block whose size in bytes is at least size. When a program obtains a memory block through malloc( ), its contents are undetermined.
//Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using malloc() function.
#include < stdio.h >
#include < stdlib.h >
int main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i {
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
calloc(): -The name calloc stands for "contiguous allocation". The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.
void *calloc( size_t count, size_t size );
The calloc( ) function reserves a block of memory whose size in bytes is at least count x size. In other words, the block is large enough to hold an array of count elements, each of which takes up size bytes. Furthermore, calloc( ) initializes every byte of the memory with the value 0.
Both functions return a pointer to void, also called a typeless pointer. The pointer's value is the address of the first byte in the memory block allocated, or a null pointer if the memory requested is not available
//Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using calloc() function.
#include < stdio.h >
#include < stdlib.h >
int main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i {
scanf("%d",ptr+i); sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
realloc(): - If the previously allocated memory is insufficient or more than sufficient. Then, you can change memory size previously allocated using realloc().
ptr=realloc(ptr,newsize);
#include < stdio.h >
#include < stdlib.h >
int main()
{
int *ptr,i,n1,n2;
printf("Enter size of array: ");
scanf("%d",&n1);
ptr=(int*)malloc(n1*sizeof(int));
printf("Address of previously allocated memory: ");
for(i=0;i printf("%u\t",ptr+i);
printf("\nEnter new size of array: ");
scanf("%d",&n2);
ptr=realloc(ptr,n2);
for(i=0;i printf("%u\t",ptr+i); return 0;
}
free(): - The amount of static memory is fixed at compile time; it does not change while the program is running. The amount of memory used for automatic variables grows and shrinks automatically as the program executes. But the amount of memory used for allocated memory just grows unless you remember to use free().
Characteristics of Allocated Memory
A successful memory allocation call yields a pointer to the beginning of a memory block. "The beginning" means that the pointer's value is equal to the lowest byte address in the block. The allocated block is aligned so that any type of object can be stored at that address.
An allocated memory block stays reserved for your program until you explicitly release it by calling free( ) or realloc( ). In other words, the storage duration of the block extends from its allocation to its release, or to end of the program.
The arrangement of memory blocks allocated by successive calls to malloc( ), calloc( ), and/or realloc( ) is unspecified.


Free Web Hosting