dynamic memory allocation in c programming, dynamic memory allocation in c example programs, c programming dynamic memory allocation, c program for dynamic memory allocation, c program using dynamic memory allocation, example program for dynamic memory allocation in c,dynamic memory allocation in c example, memory management in c programming, dynamic allocation of memory in c, memory allocation program in c, c programming memory management
Explain Dynamic Memory Allocation with malloc(), calloc(), reallc(), And free() function in C example with source code
Dynamic Memory Allocation
Or
Explain
malloc(),calloc(),reallc(),
And
free() function in C
In dynamic memory management
memory will be allocate at runtime. This is also known as heap memory.
Some language at the run time have ability to calculate and assign the
memory space at run time for an array but c can't have this feature. But in C
there is some function which provide the facility to allocate and deallocate
the memory. In ,Local Memory, when these function will call memory will
allocate automatically and when function will exit memory will deallocated
automatically . In dynamic memory allocation ,allocation of memory is
explicitly requested for a particular size of block .For deallocation of
memory explicitly requested.
the
function through which the dynamic memory allocation and deallocation will
performed are :
- malloc()
- calloc()
- free()
- realloc()
malloc():malloc()function
allocate a block of byte of memory in byte. In this when the memory
block needed explicitly requested. The malloc() function is same as a function
is request for RAM in the system memory. If the request is grant then
a void pointerreturn and the pointer point start of that block. If
the request fail then a NULL pointer return.
Example:
malloc(
number of element * size of each element);
int * ptr;
ptr =
malloc(10*sizeof(int));
Where size represents
the memory required in bytes .The memory which is provided is contiguous
memory. But malloc function return void pointer so it needed type casting of
that pointer.
Examlpe:
(type
cast)malloc( number of element * size of each element);
int * ptr;
ptr =(int*)
malloc(10*sizeof(int));
similarly for
allocation of memory to a structure variable :
Examlpe:
(struct
name)malloc( sizeof(struct name));
struct employee
{
intemp_id;
charemp_name[20];
floatemp_contact_no;
};
struct employee *ptr
ptr=(struct
employee*)malloc(sizeof(struct employee));
2.calloc():In malloc requested
memory is provided a block of contiguous memory .calloc() function is similar to the malloc rather then calloc()
function
allocated the memory
block for an array of elements. Memory for a group of objects used
calloc() function. If calloc() function is executed succesfully then its
allocated memory is set as zero and a
pointer returned and if the function
failed then a NULL pointer return.
Example:
void
*calloc(size_tnumber,size_t size);
size_t used for unsigned
on most compilers.The number is the number of objects which is allocate,
and size is the size (in bytes) of each object.
int main ()
{
intnumber,i;
printf("Enter
the number ");
scanf("%d",&number);
printf("Number
which is here are",number);
int *ptr = (int *)
calloc (number,sizeof(int));
for (i=0;
i
{
ptr[i] = i +i;
}
for (i=0;
i
{
printf("Result
is %d %d\n",i,ptr[i]);
}
}
3. free
():
For deallocation of the memory which is allocated through the malloc() function and calloc() function used free() function.
For deallocation of the memory which is allocated through the malloc() function and calloc() function used free() function.
Example:
free(ptr);
int main () {
intnumber,i;
printf("Enter the number ");
scanf("%d",&number);
printf("Number which is here are",number);
for (i=0; i ptr[i] = i +i;
} for (i=0; i
printf("Result is %d %d\n",i,ptr[i]);
}
free(ptr);}
int main () {
intnumber,i;
printf("Enter the number ");
scanf("%d",&number);
printf("Number which is here are",number);
for (i=0; i ptr[i] = i +i;
} for (i=0; i
printf("Result is %d %d\n",i,ptr[i]);
}
free(ptr);}
4.realloc()
realloc() function is
used for resize the size of memory block which is allocated by the
malloc() and calloc () function.
Two situation
where use realloc() function.
·
When
allocated block is insufficient need more memory then use realloc().
·
When
allocated memory is much more then the required application then use
realloc(). Example:
realloc(ptr, new
size);
/* Through realloc()
resize the memory . */
#include
#include
#include
{ char buffer[80], *msg;
/*
Input a string. */
puts("Enter the
text line");
gets(buffer);
/* The string copied in to initial allocated block
*/
msg = realloc(NULL,
strlen(buffur)+1);
strcpy(msg,
buffer);
/*
Display the message which copied. */
puts(message);
/* Get another string from the user.
*/
puts("Enter
another text line."); ;
gets(buffer);
/* Resize the memory and also concatenate
the string to it. */
msg = realloc(msg,(strlen(msg)
+ strlen(buffer)+1));
strcat(msg, buffer);
}