What is the difference between malloc and calloc

Showing Answers 1 - 46 of 46 Answers

madasamy

  • Mar 24th, 2006
 

malloc is memory allocationcalloc is memory release

sanjay

  • Apr 3rd, 2006
 

1:what is he difference between malloc and calloc

  Was this answer useful?  Yes

Rajeev

  • Apr 6th, 2006
 

Syntax

#include <stdlib.h>
void *malloc (Size) void free (Pointer) 
void *realloc (Pointer, Size) void *calloc (NumberOfElements, ElementSize ) 
Size

Specifies a number of bytes of memory.

 

Pointer

NumberOfElementsSpecifies the number of elements in the array.
ElementSizeSpecifies the size of each element in the array.

Points to the block of memory that was returned by the malloc or calloc subroutines. The Pointer parameter points to the first (lowest) byte address of the block.

 

 
 

  Was this answer useful?  Yes

rajesh kanna

  • Jul 8th, 2006
 

malloc - create the memory space

calloc-calculate the memory space

  Was this answer useful?  Yes

sharmila

  • Nov 28th, 2006
 

malloc- take one argument i.e(malloc(sizeof(int)*10) and allocate bytes of memory.

calloc-take two argument i.e(calloc(no.of.var,size of each var) and allocate block of memory.

Sharath

  • Jan 12th, 2007
 

1. malloc takes only the "size" of the memory block to be allocated as input parameter.
2. malloc allocates memory as a single contiguous block.
3. if  a single contiguous block cannot be allocated then malloc would fail.
1. calloc takes two parameters: the number of memory blocks and the size of each block of memory
2. calloc allocates memory which may/may not be contiguous.
3. all the memory blocks are initialized to 0.
4. it follows from point 2 that, calloc will not fail if memory can beallocated in non-contiguous blocks when a single contiguous blockcannot be allocated.

fcawad_03

  • Feb 16th, 2007
 

malloc::::: Allocates memory requests size of bytes and returns a pointer to the Ist byte of allocated space calloc:::::::: Allocates space for an array of elements initializes them to zero and returns a pointer to the memory

devi

  • Jun 11th, 2007
 

There are two differences. First, is in the number of arguments. Malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments (number of variables to allocate memory, size in bytes of a single variable). Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

Here are more opinions and answers from FAQ Farmers:

  • The difference between malloc and calloc are: 1. malloc() allocates byte of memory, whereas calloc()allocates block of memory.
  • Calloc(m, n) is essentially equivalent to p = malloc(m * n); memset(p, 0, m * n); The zero fill is all-bits-zero, and does not therefore guarantee useful null pointer values (see section 5 of this list) or floating-point zero values. Free is properly used to free the memory allocated by calloc.
  • Malloc(s); returns a pointer for enough storage for an object of s bytes. Calloc(n,s); returns a pointer for enough contiguous storage for n objects, each of s bytes. The storage is all initialized to zeros.
  • Simply, malloc takes a single argument and allocates bytes of memory as per the argument taken during its invocation. Where as calloc takes two aguments, they are the number of variables to be created and the capacity of each vaiable (i.e. the bytes per variable).
  • I think calloc can allocate and initialize memory, if the asked memory is available contiguously where as malloc can allocate even if the memory is not available contiguously but available at different locations.

  Was this answer useful?  Yes

Ashwan

  • Jul 26th, 2007
 

Malloc -
1. Used to allocate the contiguous memory space for a chunk of bytes.
2. Contains the garbage value by default.
3. Returns NULL pointer if failed to allocate the memory else return the Pointer pointing to the memory block.

Calloc -
1. Used to allocate the contiguous collective memory space.
2. Contains 0 value by default.
3. Returns NULL pointer if failed to allocate the memory else return the Pointer pointing to the memory block.

  Was this answer useful?  Yes

There are 2 differences.
First, is in the number of arguments. malloc() takes a single argument (memory required in bytes), while calloc() needs 2 arguments (number of variables to allocate memory, size in bytes of a single variable).
Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

  Was this answer useful?  Yes

vishal

  • Sep 10th, 2007
 

malloc:
malloc is used  for allocation of memory of bytes of memory
initilise to garbage value
it has only one argument is size of bytes of memory
congiguous memory location
return a pointer to memory
allocate the memory

calloc(int var,sizeof(each var));
block of memory
initilises to 0
two arguments one no.of elemnts and second one sizeof each element
contiguous memory locations.

  Was this answer useful?  Yes

malloc allocates memory continuously, that is like byte order. malloc takes only single parameter that determines in bytes. It takes garbage values as default.

calloc allocates memory either continuously or non-continuously, that is like in blocks. calloc takes two parameters in those one determines datatype and another represents the number of blocks. It takes 0 value as default.

  Was this answer useful?  Yes

alokag

  • Jun 30th, 2009
 

Calloc is similar to malloc but initializes the allocated memory to 0.
No other differences as mentioned in other post.

Imp: The memory allocated in both the cases will be contiguous only else both fail if chunk not available.

Calloc is equivalent to :
 malloc()+ memset(to zero)

priya425

  • Jul 5th, 2009
 

Major difference between malloc and calloc:
Memory allocation through malloc is
equivalent to single dimensional array.
and calloc memory allocation is equivalent to double dimensional array.

  Was this answer useful?  Yes

RaviRanjan kumar

  • Feb 1st, 2012
 

malloc: malloc create the single block of given size by user
calloc: calloc creates multiple blocks of given size
both return void pointer(void *)so boh requires type casting

malloc: eg:

int *p;
p=(int*)malloc(sizeof(int)*5)

above syntax tells that malloc occupies the 10 bytes memeory and assign the address of first byte to P

calloc: eg:

p=(int*)calloc(5,sizeof(int)*5)

above syntax tells that calloc occupies 5 blocks each of the 10 bytes memeory and assign the address of first byte of first block to P

  Was this answer useful?  Yes

maha

  • May 12th, 2012
 

malloc is the memory allocation operator whereas calloc is the memory release operator

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions