CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
Is it better to use malloc() or calloc()?
Category C Interview Questions
Rating (4.0) By 56 users
Added on 10/22/2004
Views 11346
Rate it!
Answers:

Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big:

void *malloc( size_t size );

calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory
at least big enough to hold them all:

void *calloc( size_t numElements, size_t sizeOfElement );

There?s one major difference and one minor difference between the two functions. The major difference is that malloc() doesn?t initialize the allocated memory. The first time malloc() gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused). calloc() fills the allocated memory with all zero bits. That means that anything there you?re going to use as a char or an int of any length, signed or unsigned, is guaranteed to be zero. Anything you?re going to use as a pointer is set to all zero bits. That?s usually a null pointer, but it?s not guaranteed.Anything you?re going to use as a float or double is set to all zero bits; that?s a floating-point zero on some types of machines, but not on all.

The minor difference between the two is that calloc() returns an array of objects; malloc() returns one object. Some people use calloc() to make clear that they want an array.



yes

becuse it provide larger area in heep
part of memory



 Posted by: vikas gupta    

Contact vikas gupta  Contact vikas gupta

malloc is better then calloc.

Internally,calloc calls malloc and then fills with zeros.Hence, two calls is required,one for allocating memory and another call to fill zero's.Hence,calloc is less efficient than malloc.

Garbage value is the value which is not useful to user.Even zero may be garbage value for users.Hence,filling zero's by calloc is of no use.

Also,memory allocated by any function is used only after initialising with user choice value.Hence,accessing memory before initialising is programmers mistake and not mistake of malloc.

Hence malloc is more efficient than calloc.



 Posted by: Lohit.A.H    

Contact Lohit.A.H  Contact Lohit.A.H

malloc() is allocate 1 byte of memory. but calloc() is allocate large type of memory allocation.

calloc return's the array of objects. but malloc() return's the one object oly.



 Posted by: Shanthi    

Contact Shanthi  Contact Shanthi

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.

Plz do post an example if posble....



 Posted by: ashwa    

Contact ashwa  Contact ashwa

One more thing malloc() allocates continous bolck of memory, if not present it returns an error. Whereas calloc() allocates memory whereever it is present...



 Posted by: Subrahmanya    

Contact Subrahmanya  Contact Subrahmanya

1. Both malloc() and calloc() are used to dynamically allocate memory on heap.
2. malloc() is used to allocate a single block of memory.
3. calloc() is used to allocate multiple blocks of memory.
4. It is better to use them when we are working on lists(linked lists, doubley linked lists etc..).
5. At the same time the user must be aware that in c, memory allocated on heap should manually deallocated by the user.
6. So, once the memory allocated on heap is of no use, we must deallocate the memory by using the free() function. This is must!!



 Posted by: Poornima V    

Contact Poornima V  Contact Poornima V

calloc()is alllocate mlutiple space of memory but mallo0c is allocate a single space of memory.



 Posted by: dattu    

Contact dattu  Contact dattu


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
What is the difference between far and near?
View Answer
When should a far pointer be used?
View Answer
Can the size of an array be declared at runtime?
View Answer
What is the heap?
View Answer
What is the difference between NULL and NUL?
View Answer
What is a ?null pointer assignment? error? What are bus errors, memory faults, and core dumps?
View Answer
How can you determine the size of an allocated portion of memory?
View Answer
Can math operations be performed on a void pointer?
View Answer
How do you print an address?
View Answer
Why should I prototype a function?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/1001/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.86