GeekInterview.com
Series: Subject: Topic:
Question: 502 of 828

What is the difference between malloc and calloc

Asked by: Interview Candidate | Asked on: Mar 23rd, 2006
Showing Answers 1 - 20 of 20 Answers
madasamy

Answered On : Mar 24th, 2006

Malloc is memory allocationcalloc is memory release

Yes  1 User has rated as useful.
  
Login to rate this answer.
sanjay

Answered On : Apr 3rd, 2006

1:what is he difference between malloc and calloc

  
Login to rate this answer.
Rajeev

Answered On : Apr 6th, 2006

Syntax#include <stdlib.h>void *malloc (Size) void free (Pointer) void *realloc (Pointer, Size) void *calloc (NumberOfElements, ElementSize ) SizeSpecifies a number of bytes of memory.  PointerNumberOfElementsSpecifies 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.   

  
Login to rate this answer.

malloc  is dynamic memory allocation,it  allocates the memory and initialize garbage value.calloc is similar to malloc but only difference is initialize zero

Yes  2 Users have rated as useful.
  
Login to rate this answer.
rajesh kanna

Answered On : Jul 8th, 2006

malloc - create the memory spacecalloc-calculate the memory space

  
Login to rate this answer.
sharmila

Answered On : 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.

Yes  2 Users have rated as useful.
  
Login to rate this answer.
gunasekarnp

Answered On : Dec 9th, 2006

View all answers by gunasekarnp

Malloc used for memory reduce

  
Login to rate this answer.
gunasekarnp

Answered On : Dec 9th, 2006

View all answers by gunasekarnp

Reduce the memory size

  
Login to rate this answer.
Sharath

Answered On : 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 memory2. 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.

Yes  3 Users have rated as useful.
  
Login to rate this answer.
fcawad_03

Answered On : Feb 16th, 2007

View all answers by fcawad_03

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

Yes  1 User has rated as useful.
  
Login to rate this answer.
devi

Answered On : 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.

  
Login to rate this answer.
Ashwan

Answered On : 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.

  
Login to rate this answer.

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.

  
Login to rate this answer.
vishal

Answered On : Sep 10th, 2007

Malloc:malloc is used  for allocation of memory of bytes of memoryinitilise to garbage valueit has only one argument is size of bytes of memorycongiguous memory locationreturn a pointer to memoryallocate the memorycalloc(int var,sizeof(each var));block of memoryinitilises to 0two arguments one no.of elemnts and second one sizeof each elementcontiguous memory locations.

  
Login to rate this answer.

Malloc and calloc both are used to assign memory to a varible dynamically.the space takes place in heap area.malloc is for single block  of memory(continous locations).calloc is for non continous blocks.

  
Login to rate this answer.

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.

  
Login to rate this answer.
alokag

Answered On : Jun 30th, 2009

View all answers by alokag

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)

Yes  1 User has rated as useful.
  
Login to rate this answer.
priya425

Answered On : Jul 5th, 2009

View all answers by priya425

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.

  
Login to rate this answer.
RaviRanjan kumar

Answered On : 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

  
Login to rate this answer.
maha

Answered On : May 12th, 2012

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

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.