Synopsys R&D Bloomberg LP Interview Question for Financial Software Developers






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

malloc returns void pointer
new returns the pointer to the object that is created (eg: A *a = new A(); returns pointer of type A)

malloc does not call constructor
new calls constructor

- yours.ramesh February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I got the same question on my interview. First answer is that malloc is allocation method from C and new is allocation method from C++. There is no object-oriented programming in C, and therefore malloc don't know about constructors. So the basic answer is that new allocates memory and calls the constructor, where malloc only allocates memory and return pointer on the void* without any values initalized (we have to memset them to 0). I also mentioned that malloc and new use different allocation techniques so we shouldn't use malloc with delete and new with free.

- R.Kozikowski January 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Object oriented programming is exactly as it says, an orientation. Any language can be used to program object oriented. In particular, anything that can be done in C++ can be done in C. The proof is the C-front compiler.

- ToddL October 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Kozikowski: You are correct. One more difference is there.

malloc always returns 0 (or NULL) when it runs out of memory. but new used to do this until Microsoft Visual Studio 6.0 but after C++03 standard (came in 2003, latest so far, C++0x is about to come!) and in later compiler's new NEVER EVER returns 0 or NULL, it always throws std::bad_alloc exception if it runs out of memory, however you can still change this behavior to old (returning 0) behavior by using this syntax!

int *p = new(nothrow) int;

- rajendra February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@bobby.teja: Where did you find that? It's absolutely wrong! Are you just making guesses here?

Neither new Nor malloc releases memory after program termination. There are 2 cases to this scenario:
1. If you new/malloc some memory and never delete/free that memory then during the program execution this is called memory leak and can lead to undefined behavior of the program causing crashes, overwriting memory, etc.
2. Bobby, if you are talking about program termination then don't blame new/malloc for releasing memory, it is the runtime system that unloads the process from main memory (RAM) and thus there is no question of releasing memory or memory leak, because the program is not in main memory so whole memory acquired by the program has been unloaded from the main memory.

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

if memory cant b allocated...malloc returns NULL
if memory cant b allocated...new throws exception

- yours.ramesh February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

new (nothrow) doesn`t

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

Two more :

1. new can be overloaded, malloc doesn't know what overloading is :)
2. new doesn't require you to specify size of mem to be allocated, malloc cannot work without it.

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

Two more :

1. new can be overloaded, malloc doesn't know what overloading is :)
2. new doesn't require you to specify size of mem to be allocated, malloc cannot work without it.

- JohnyCigar February 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Overloading of new operator is mandatory if my class contains some pointer type member variable.

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

thank you all for the answers

- sp.sangeeta February 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

new returns correct type of pointer
while malloc returns void*


new automatically calculates the size of memory to be allocated while in malloc we have tp pass the size of memory to be allocated

new is an operator in c++ while malloc is a function

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

new returns correct type of pointer
while malloc returns void*


new automatically calculates the size of memory to be allocated while in malloc we have tp pass the size of memory to be allocated

new is an operator in c++ while malloc is a function

- kamlesh June 14, 2011 | 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