NetApp Interview Question for Software Engineer / Developers






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

The memory area just before the starting address that malloc returns contains information about no. of bytes allocated. The similar thing is used by free() to determine the size of the memory to be freed

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

I am not sure about this. Did you mean that the malloc actually allocates few bytes more for the information of the allocated bytes?

I checked out but it seems not..
Somebody correct me.

- ted October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it sure does, but that is internal information in the CRT and you are not suppose to use. It is definitly not portable.

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

Pointer do contain the information about how much bytes are reserved however I guess that is compiler dependent !

- Anonymous October 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pointer do contain the information about how much bytes are reserved however I guess that is compiler dependent !

- Anonymous October 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Unfortunately, there is no standard or portable way. (Some compilers provide nonstandard extensions.) If you need to know, you'll have to keep track of it yourself.ex some compiler have malloc.h header in which _msize()function to get size.

but The malloc function allocates a memory block of at least size bytes. The block may be larger than size bytes because of space required for alignment and maintenance information

so its implementation dependet ...

BUT HOW FREE() KNOWS how much memory to free?

sol-> when we request memory from malloc . compiler writes some info about total size ,checksum etc just before memory allocated for malloc and free uses that information to free memory. it gets ptr and read memory just befor that to get info
how much to free

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

great.

_msize( pointer ) gives u the result

.

- Karthik APJ October 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

malloc_usable_size(p) works fine on linux

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

nice work

- karn January 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

yap malloc_usable_size(p) works fine

- sudhir2884 January 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

malloc_usable_size may not always return the requested size of malloc.

unix.com/man-page/freebsd/3/malloc_usable_size/

- redfabric February 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

in the function try to allocate memory untill you exhast all (try allocating like 2, 4, 8, 16, 32, 64, 128 etc), take count for all. when you done this you know how much more memory you newly allocated. freed all memory you allocated. free the memory pointed by p. now again find the existing free memory. take the difference will give you the memory pointed by p. this is kind of not direct method, but careful code gives you the output needed

- Anonymous June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

- allocate all remaining memory and exhast all free memory
- free the memory pointed by 'p' now
- try to allocate the memory now using chunks like 1, 2, 4, 8, 16, 32, 64, 128 etc untill all memory exhast again. once last chunk is not allocted, then try for it half untill you succeeded
- now you know how much memory you allocated which is nothing but your size of memory pointed by 'p'

- Vinod Patankar June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

inside code shd be

(char *)(&p + 1) - (char *)(&p)

- mlakshmanarao July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wrong

- Anonymous August 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

#include<stdlib.h>
#include <stdio.h>
int main()
{
int *p;
p=(int*)malloc(sizeof(int)*8);
printf("%d\n",*--p);
}

- pramod August 24, 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