The Digital Group Interview Question for Developer Program Engineers


Country: India
Interview Type: Phone Interview




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

Neither of those is correct. Quite simply, if you allocated using new, you must de-allocate with delete. If it was new[], then use delete[]. If it was malloc, use free. No other combination of those operators is correct.

- eugene.yarovoi October 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

free uses a pointer to free up the memory. if u do free of one pointer its memory is freed. Now when u allocate arrray of intergers using new u have the first address of array and also no number of elements in the array so just use for loop inside for loop write free(p++) and it will free up the entire array.

- Ashwin B HUlswar November 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

- int *a = new int[10];
for(int i = 0; i < 10; i++) {
free(a + i);
}
it would crash.

- int *a = new int[10];
free(a);
It simply free the desired memory. All heap blocks were freed -- no leaks are possible
When I checked the binary with Valgrind, it reported an error
mismatch free / delete / delete []

- Barjesh August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) malloc: you cannot use delete with malloc, as no constructor is called during memory allocation and while delete is used on that memory it will call destructor which will lead to seg fault.

2)new: you can use either delete or free. But the problem with using free is that the class desctructor is not called and hence there will be problems later on.

Hence try to stick with malloc-free and new-delete

- dronzer709 May 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) malloc: you cannot use delete with malloc, as no constructor is called during memory allocation and while delete is used on that memory it will call destructor which will lead to seg fault.

2)new: you can use either delete or free. But the problem with using free is that the class desctructor is not called and hence there will be problems later on.

Hence try to stick with malloc-free and new-delete

- dronzer709 May 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Stating the obvious - new/malloc delete/free are not equivalent and we should not do this. However, theoretically speaking, you should be able to overload operator new and use malloc internally and not call the constructor of the class being invoked right?

- Saranya July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Before calling free() on a pointer for which memory was allocated using new() call the destructor of that class and then invoke free();

The new() keyword does the below 2 operations
1.Allocate memory using malloc by invoking the "Operator new"
2.call the constructor.

The delete() keyword does the below 2 operations
1.call the destructor.
2.Deallocate the memory using free by invoking the "Operator delete"

- BJ September 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Conceptually, yes, that's how new and delete work. However, what you said about being able to interchange calling the destructor + calling free with calling delete is not correct.

Memory allocated using new is not guaranteed to be implemented in the same way, or to use the same heap, as memory allocated using malloc.

In short -- you cannot interchange delete and free no matter what you do!

- eugene.yarovoi September 16, 2012 | 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