ProgrammingModerateHCL · Tech Mahindra pattern

In C++, what does new do that malloc does not?

  1. A. It never fails
  2. B. It calls the constructor of the object it allocates
  3. C. It frees the memory automatically
  4. D. It allocates on the stack instead of the heap

Correct answer: B. It calls the constructor of the object it allocates

new allocates memory and then runs the constructor (delete runs the destructor before freeing). malloc only returns raw bytes. new throws std::bad_alloc on failure by default rather than returning NULL.

More OOP with C++ and Java questions

Exams that ask this topic