What is a key difference between malloc and new in C++ when allocating memory for dynamic objects?
A) malloc is used for allocating memory for built-in data types, while new is used for objects of user-defined classes.
B) malloc performs automatic memory management, while new requires manual memory deallocation using delete.
C) malloc returns a pointer to uninitialized memory, while new returns a pointer to a constructed object with its constructor called.
D) malloc is specific to C programming, and new is specific to C++.
Answer:
C) malloc returns a pointer to uninitialized memory, while new returns a pointer to a constructed object with its constructor called.
Explanation:
One of the main differences between malloc and new is that malloc allocates raw, uninitialized memory, while new not only allocates memory but also calls the constructor of the object being created, ensuring that it is properly initialized. This makes new particularly useful for objects of user-defined classes.