C Programming language

C Memory functions : malloc() function
Previous Home Next

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 pointer return and the pointer point start of that block. If the request fail then a NULL pointer return.

malloc( number of element * sizeof(element));

Example

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.

(type cast)malloc( number of element * sizeof(element));

Example

int * ptr;
ptr =(int*) malloc(10*sizeof(int));

Similarly for allocation of memory to a structure variable

(struct name)malloc( sizeof(struct name));

Example

struct employee
{
int emp_id;
char emp_name[20];
float emp_contact_no;

};


struct employee *ptr
ptr=(struct employee*)malloc(sizeof(struct employee));

calloc() function

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 successfully then its allocated memory is set as zero and a pointer returned and if the function failed then a NULL pointer return.

void *calloc(size_t number,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.

Example

int main () {
       
int number,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<number;i++) {               
        ptr[i] = i +i;
       }
   for (i=0; i<number;i++) {              
       printf("Result is %d %d\n",i,ptr[i]);
   }
};
free() function

For deallocation of the memory which is allocated through the malloc() function and calloc() function used free() function.

free(ptr);

Example:

int main () {
       
int number, i; 
    printf("Enter the number ");
    scanf("%d",&number);
    printf("Number which is here are",number);
     for (i=0; i<number;i++) {
      ptr[i] = i +i;
   }

for
(i=0; i<number;i++) {
  printf("Result is %d %d\n",i,ptr[i]);
  }
 
free(ptr);
}
;
realloc() function

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().
realloc(ptr, new size);

Example

/* Through realloc() resize the memory  */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main { 
  char buffer[80], *msg;
           
   puts("Enter the text line"); // Input a string.
   gets(buffer);

   /* The string copied in to initial allocated block */
    msg = realloc(NULL, strlen(buffur)+1);    
    strcpy(msg, buffer);    
puts(message); // Display the message which copied.      puts("Enter another text line."); //Get another string from the user.     gets(buffer);     /* Resize the memory and also concatenate the string to it. */    msg = realloc(msg,(strlen(msg) + strlen(buffer)+1));        strcat(msg, buffer);  };  
Previous Home Next