Microsoft Interview Question for Software Engineer / Developers


Team: bing
Country: India
Interview Type: In-Person




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

when using malloc, it returns void type. we need to type cast. And the acutall memory allocated is larger than you applied, the first part stores the number of bytes allocated (offset to teh next memory from the start of this memroy). For example, if you do int *i = (int*) malloc(12), it allocates 16 bytes. The first 4 bytes for actuall size, from 5th byte is return to i, now i can acess 12 bytes from this byte.

- lliyanc March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

node points to first byte of address.
but shouldn't this be
List* node;
--- node=malloc(sizeof(node));
+++ node=malloc(sizeof(LIST));

because when you allocate using sizeof(node) it will returns size of address and not the complete struct size. and when we try to write we might get into segmentation faults.

- mtsingh March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You're right.

- Thomas Edison June 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

malloc allocates the memory for the requested amount of bytes and returns the base address of the memory location that was allocated.

Node points to the first byte of the address that was allocated by malloc

- Srivathsan March 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

List* node;
node=malloc(sizeof(node));
.........
above statement will give error:
cannot convert from 'void *' to List*


malloc return void* pointer to location where memory is allocated

correction:

List* node;
node=(node*)malloc(sizeof(node));

- Nik March 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the implicit void conversion does work in plain old C, and doesn't work in C++.

- eugene.yarovoi March 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes u r right, i tested for C++

- Nik March 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@eugene.yarovoi -
We can assign any data type pointer to void * but not a vice a versa

- Ashish P March 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

when using malloc, it returns void type. we need to type cast. And the acutall memory allocated is larger than you applied, the first part stores the number of bytes allocated (offset to teh next memory from the start of this memroy). For example, if you do int *i = (int*) malloc(12), it allocates 16 bytes. The first 4 bytes for actuall size, from 5th byte is return to i, now i can acess 12 bytes from this byte.

- lliyanc March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

malloc returns void*.
malloc(sizeof(node)); will allocate memory of the size of node.
node points to the first byte of the address that was allocated by malloc.

but malloc(sizeof(node)) might not allocate the required memory for the data type List.

- Palash March 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not needed in c, i will not recomend to use in C, List* node;
node=malloc(sizeof(node)); will work fine in c But casting needed in c++.

- Nishant May 16, 2012 | Flag Reply


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