Aricent Interview Question for Software Engineer in Tests






Comment hidden because of low score. Click to expand.
1
of 0 vote

Calloc has overhead of initializing to 0

- Prakhar January 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

calloc allocate memory as well as initialize with 0, that malloc doesnt iniializ...

- Deb kumar June 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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 = m*malloc(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).

This one is false:
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.

- Anuj January 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A less known difference is that in operating systems with optimistic memory allocation, like Linux, the pointer returned by malloc isn't backed by real memory until the program actually touches it.

calloc does indeed touch the memory (it writes zeroes on it) and thus you'll be sure the OS is backing the allocation with actual RAM (or swap). This is also why it is slower than malloc (not only does it have to zero it, the OS must also find a suitable memory area by possibly swapping out other processes)

- ridercoder October 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is something new info. Thanks ridercoder.

- cirus November 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

and dont depend on setting all bit 0 by calloc because all bit 0 may not represent floating point 0 / NULL so relying on this can make programm non portable .

- ridercoder October 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Timming is fine. but one more concern I think is freedom of use also. that is how you are going to use that memory.
as calloc allocate n blocks of memory each of size b bytes.
so good if we know size of each elements and all elements are of same size.
whether malloc allocates memory of n bytes that we can use in the way we want. we can divide this space to n variable of different size. hence malloc is prefered over calloc.

- thesudhir April 19, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More