Yahoo Interview Question for Software Engineer / Developers






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

I think it is implementation-dependent.

- hunt September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@hunt: no, why do you think so? explain clearly

- hunter September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the address it's pointing to!

- Anonymous September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am geeting a non-zero value of P in gcc. but don't konw the reason. can anybody explain?

- Franko September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am allocating zero byte using malloc()? how does it allocate memory?

- Franko September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it depends on compiler. But can we use free() to deallocate this memory?

- Erik September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

not a problem to free this ptr. I have done it. But why it is allocating memory?

- franko September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ franko:
that is the million dollar question !

anyone out there for million dollars ?

- manoj September 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

when we call malloc it returns a pointer to the heap, it returns memory in multiple of blocks, each block size depends on implementation of malloc, malloc is going to round of to next integer multiple of block. so its going to return atleast one block. so thats why there won't be problem even if we try to assign value to memory.

- Anonymous September 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

see the IBM like below, it looks it compiler dependent
http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/malloc.htm

- mondac September 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes, it's implementation dependent. Thanks mondac!!
I got 2 different results for 2 different compilers when i say

printf("%p, %d",p,*p);

GCC: 0x804a008 , 0
MS VC++: 00030850, -33686019
TC : 00000 , 0


GCC

- Addy September 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

char * p= NULL;
p = (char*) malloc(0);
printf("%p,%p,",p, p++);
Output - 0x100121,0x100120

int * p= NULL;
p = (int*) malloc(0);
printf("%p,%p,",p, p++);
Output -0x100124,0x100120

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

I really hate it when people ask such questions, I am wondering, they just want to pull of peoples tolerance levels towards such questions, Instead they could beat him with a stick and watch if get angry:)

- kabootar@googlemail.com October 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

>
>> I really hate it when people ask such questions, I am wondering,
>> they just want to pull of peoples tolerance levels towards such
>> questions, Instead they could beat him with a stick and watch if
>> get angry:)
>
No, this isn't about measuring someone's tolerance level, it's about measuring an individual's understanding of the C language. Few, if any, programmers, would knowingly use malloc() to allocate 0 bytes of memory. However, it would be very easy to pass an argument to malloc() that somehow got incorrectly set to zero. The interviewer wants to know if you understand what will happen if such a situation occurs. This is going to be important when you attempt to find the bug that has set the variable in question to a value of zero.

The correct answer to this question is that the result is compiler dependent. However, the most likely behavior is that a non-zero length memory segment will be returned by malloc(). Malloc is guaranteed to return *at least* the number of bytes you request, but may return a contiguous memory segment larger than the requested size if it wishes. Since some malloc() implementations maintain "bins" of preallocated memory it's possible that the size of the memory segment returned will be small, but not zero. Perhaps 8, 16, or 32 bytes in length.

- Wandering programmer May 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

malloc will return atleast one byte of memory....it has to allocatte mininum 1byte memory. so we will get a non zero address

- amm February 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void * Myalloc ( int size )
{
// lets assume we get the address of the available memory in a;
// n be the available size
void *f =a // now f is the void pointer which points to the available memory )
if( size > N )
printf(" in sufficient memory ");
else
n=n+size ;// size is reduced from availabe n
return f;
}
so it will always give a valid address
Correct me if i am wrong

- vijaymukilan July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is not true that malloc will always return at least 1 byte.

int * p= NULL;
p = (int*) malloc(0);

what will be the value of p?

The value of is implementation dependent.


7.20.3 (n869):

If the size of the space requested is zero, the behavior is
implementation-defined: either a null pointer is returned, or the
behavior is as if the size were some nonzero value, except that the
returned pointer shall not be used to access an object.

- dfmcla July 18, 2013 | 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