We mainly use to allocate memory dynamically while execution of program.if you are familiar with linked list then you know that fact that we prefer linked list instead of array when we don't know size of data. its because in case of array we fixed the size but in case of linked list, we just use malloc() function and allocate memory whenever we require to store a value,address etc.
See an example:
struct node{
int item;
struct node *next;
} *head=NULL,*newnode=NULL;
int main(){
// I am going to allocate memory dynamically using malloc()
int ScannedValue;
while(true) {
scanf("%d",&ScannedValue);
// allotted memory as we are going to store a value inside newnode
newnode=(struct node*)malloc(sizeof(newnode));
newnode->item=ScannedValue;
// as we connect nodes backward or forward and continue this process
}
}