Interview Question


Country: United States




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

The memory for the array is not dynamically allocated. It is not defined what happens, when you free memory that was not allocated by new previously. The C++ example below works fine.

int* myArray = new int[10];
delete[] myArray;

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

And why memory on stack can`t be freed by operator delete()? What exactly causes an assertion?

- Anonymous November 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well because delete looks for the memory on the heap.

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

I believe delete only releases memory that has been allocated using new operator.
Just for the sake of it , I tried using delete operator on memory allocated on stack, that has caused my program to crash on Ubuntu and WindowsXP. I don't know if any other OS has a different behavior. Thanks

- blueskin November 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

VC++ crashes on assert(), trying to get some header from deleting pointer. This header may include the size of deleting object (or the size of array).
__________________________
| header | dynamically |
|________| allocated data__|
^
|

operator delete() tryes to get this info (it is there for heap object), but can`t.

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

when you allocate memory using new ; this process also inserts some data such as number of bytes that were allocated.where exactly this bookkeeping will be stored is totally implementation specific.But when you delete the operator looks for that bookkeeping information in order to determine how many bytes to free.probably "looking for this bookkeeping" information is what causes seg fault

- pushkar November 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This would cause _BLOCK_TYPE_IS_VALID assertion. This is the error compiler would throw when user is trying to call delete when memory hasn't been allocated.

/* get a pointer to memory block header */
pHead = pHdr(pUserData);

/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

_free_dbg( pUserData, pHead->nBlockUse );

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

You had an interview at CareerCup? How much is the salary? ;)

- Anonymous December 22, 2010 | 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