Sponsored Links

Interview Questions



INTERVIEW QUESTIONS C DYNAMIC MEMORY ALLOCATION IN C DETAILS

Question: Is it better to use malloc() or calloc()?

Answer: 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.

Category Dynamic Memory Allocation in C Interview Questions & Answers - Exam Mode / Learning Mode
Rating (0.3) By 9109 users
Added on 10/22/2009
Views 82147
Rate it!

Question: Is it better to use malloc() or calloc()?

Answer:

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. Source: CoolInterview.com


yes

becuse it provide larger area in heep
part of memory Source: CoolInterview.com

Answered by: vikas gupta | Date: 4/8/2009 | 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. Source: CoolInterview.com

Answered by: Lohit.A.H | Date: 7/28/2009 | 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. Source: CoolInterview.com

Answered by: Shanthi | Date: 8/30/2009 | 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.... Source: CoolInterview.com

Answered by: ashwa | Date: 9/2/2009 | 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... Source: CoolInterview.com

Answered by: Subrahmanya | Date: 9/11/2009 | 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!! Source: CoolInterview.com

Answered by: Poornima V | Date: 1/5/2010 | Contact Poornima V Contact Poornima V

calloc()is alllocate mlutiple space of memory but mallo0c is allocate a single space of memory. Source: CoolInterview.com

Answered by: dattu | Date: 1/10/2010 | Contact dattu Contact dattu

Following are my 2 cents on this problem.
Both malloc and calloc return a pointer to the allocated region
I dont think calloc can allot a non contiguous block of memory. All the pointer arithmetic would crash Source: CoolInterview.com

Answered by: abhimanipal | Date: 3/16/2010 | Contact abhimanipal Contact abhimanipal

Hi there:

malloc is for "memory allocation". While programming, we very often need to allocate free memory for variables of unknown length, which can not be decided at the time of programming. E.g. a program prompts people's names; since each person's name is different, the programmer is unlikely to allocate a fixed amount of memory to hold their names. (You may argue that it is possible to assign a whole bunch of memory; it is a bad practise and it is problem prone. )
A similar function is calloc().

A very good book to learn C language is C programming, A modern approach by Amar K. brisbane.

Does that answer your question?:Gilly: Source: CoolInterview.com

Answered by: Amar Kumar (brisbane) | Date: 4/14/2010 | Contact Amar Kumar (brisbane) Contact Amar Kumar (brisbane)

I have to assume you have a minimum of programming knowledge for this.

malloc is a C/C++ function that returns a pointer to a block of unused memory. The program that calls the function can then use that memory in any way it sees fit.

calloc does the same thing, but guarantees that the block of memory is all set to zero, that is, that it doesn't have any random values in it.

It's good programming practice to release the memory allocated with malloc or calloc when the program no longer needs it. This is done with free(). Source: CoolInterview.com

Answered by: Amar Kumar (brisbane) | Date: 4/14/2010 | Contact Amar Kumar (brisbane) Contact Amar Kumar (brisbane)


If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
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 heap?
View Answer
How can you determine the size of an allocated portion of memory?
View Answer
What are advantages and disadvantages of external storage class?
View Answer
What is static memory allocation and dynamic memory allocation?
View Answer
What is the purpose of realloc( )?
View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All Dynamic Memory Allocation in C Interview Questions & Answers - Exam Mode / Learning Mode



User Options
India News Network

Latest 20 Questions
Payment of time- barred debt is: (a) Valid (b) Void (c) Illegal (d) Voidable
Consideration is defined in the Indian Contract Act,1872 in: (a) Section 2(f) (b) Section 2(e) (c) Section 2(g) (d) Section 2(d)
Which of the following is not an exception to the rule, "No consideration, No contract": (a) Natural love and affection (b) Compensation for involuntary services (c) Completed gift (d) Agency
Consideration must move at the desire of: (a) The promisor (b) The promisee (c) The promisor or any other party (d) Both the promisor and the promisee
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal



Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |




Copyright ©2003-2024 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions