Synopsys R&D Interview Question






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

malloc does not initialize the memory.
calloc initialize all bits of memory allocated to 0.

malloc takes one arguments (number of bytes required)
calloc takes two parameters (number of blocks required, size of each block)

- yours.ramesh February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

malloc takes one arguments (number of bytes required)
calloc takes two parameters (number of blocks required, size of each block)

How can we differentiate (number of bytes required) & (number of blocks required, size of each block)

Can you give an example

I have tried with following implementation:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int *p1 = (int *) malloc(4*sizeof(int));
int *p2 = (int *) calloc(4, sizeof(int));

printf("\naddress=%i, value=%d,",p1, *p1);
++p1;
printf("\tnext address=%i next value=%d",p1, *p1);
printf("\naddress=%i, value=%d,",p2, *p2);
++p2;
printf("\tnext address=%i next value=%d",p2, *p2);
}

both gives same output.......

- Rahul February 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

same output? could you please paste output.

wht i understand is malloc is used if we know the number of bytes in advance
calloc is used if we know the size of each block(like struct) and if we also know how many of them to be allocated (number of blocks required)

otherwise, basic purpose of malloc and calloc is same, to allocate memory.

wht u did is right. calloc may internally do the same (4*sizeof(int)) to allocate. (but initialze all its bits to '0')

- Anonymous February 11, 2011 | 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