calloc

malloc

We need to split and pass the memory we want We can pass how much memory we want.
Ex: p=calloc(5,sizeof(int) Ex: p=malloc(5,sizeof(int)
Function return void Functions Return void
Returns starting address but before allocating to us it will zero it. It will clear the previous garbage value. Returns starting address.
Calloc is slower than malloc Faster than calloc.
It is safe as compared to malloc Not safe as calloc.
Dynamic memory allocation A function which allocates block memory during run time.

 

There are 4 library routines calloc(), free(), alloc(),and malloc() which is used to allocate memory and free it up during the program execution Once when a memory space of specific size is allocated, it returns null pointer pointing to that allocated memory location.

 

Calloc() initialized the allocated memory to zero Malloc() doesn’t initializes the allocated memory. It contains garbage Values
It allocates multiple block of requested memory. It allocates only single block of requested memory.

Why use malloc()

Syntax of malloc()

ptr = (cast_type *) malloc (byte_size); 

Example of malloc() in C

ptr = (int * ) malloc(50)

Why use calloc()

Syntax of Calloc()

ptr = (cast_type *) calloc (n, size); 

Categorized in:

C

Tagged in:

, , , , , , , , , , ,

Share Article:

Leave a Reply

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO