Cisco Systems Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

I was asked this question - How does free() knows how much memory to free ?- in bloomberg interview. The answer to that is - when you do malloc(size); it reserves size+1 bytes of memory and it stores the value size in the first block. So, when we do free(ptr), it immediately knows how much memory to free.

- rkt August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What data structure would you use to keep track of memory to b freed

- Zee August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

The memory returned by malloc is contiguous block of memory (like an array). When it is free'd , it goes back into a (linked) list of free blocks.

- rkt August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

vote for rkt.

- onpduo August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That means if we try to access the memory location where the malloc returns the pointer, it should show us the size of blocks allocated. I doubt it.

Can you please explain what is structure of memory allocated by malloc and where it stores the size?

- Piyush August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Piyush: "if we try to access the memory location where the malloc returns the pointer, it should show us the size of blocks allocated"

No, that's certainly not true. And it would be bad if it were, because then writing to *returned_ptr would erase this critical information! What I'm sure the poster is saying is that an attempt to allocate k bytes actually allocates a block of k+1 bytes and returns you a pointer to start_of_block + 1. So if you did *(returned_ptr - 1) you'd get the size.

I have two things to note about this theory. First of all it has to be something like size_asked_for + sizeof(void*), since you'll need multiple bytes (like maybe 4) to store the length. A single byte couldn't fit the length. Second, implementation details of this sort are compiler-specific. That said, I hear this is often how it's done. Another possible approach would be to keep some sort of pointer to size mapping (like a hash table, for example) somewhere.

- Anonymous August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

agree totally with the above comment. Malloc allocates size+1 blocks and returned pointer ptr points to the second block. so if you access the first block by ptr[-1] , it prints the size. I wrote a small prog to verify it.

- rkt August 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What type is ptr in this test that you did? The allocation shouldn't be size+1 bytes, but size+sizeof(void*) or something like that (compiler-dependent, though). Did you cast the value returned by malloc to int* first? In that case, ptr[-1] actually read returned_ptr - sizeof(int), which is what I would expect.

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

hello

- anonymous August 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

malloc has to make OS api calls to talk to global heap manager, which would return a pointer to memory block if it can, else NULL. This bookkeeping work is done by OS processes and is nothing to do with malloc or free. malloc or free are just used to invoke those apis.
As far as good datastructure for heapMemoryManagement is considered we can use linked list.
struct node{
void * meory_location;
int memory_allocated_in_bytes_offset;
}
And maintain this in sorted order, so that for call to malloc or free we would have to just traverse this list.

- akki August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Cisco gives hike once in 2-3 years.

New-Joinees only get hike after 2-3 years, so take 100% hike at the time of joining .. . otherwise don't cry after joining. :)

- Simple April 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

try to be the top performer. You will get paid/raise.:-)

- Anonymous May 20, 2014 | 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