OA. free
Free
MathWorks Core Cs & Systems Core Computer Science Medium

Select all statements from the following which are TRUE regarding new and malloc:

MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.

Select all statements from the following which are TRUE regarding new and malloc:

Choose one option.
Show answer & explanation
Answer: A. 'new' calls constructor while 'malloc' does not.

All four statements accurately describe key differences between 'new' and 'malloc'. (A) 'new' invokes constructors for objects; malloc does not. (B) 'new' is a C++ operator; malloc is a C standard library function. (C) 'new' can be overloaded in a class; malloc cannot. (D) 'new' returns the exact pointer type; malloc returns void* requiring casting. Option E is FALSE: 'new' throws std::bad_alloc on failure; malloc returns NULL.

Step-by-step Derivation:
Analysis of each statement:

A) TRUE: new operator automatically calls constructors. malloc only allocates raw memory without initialization.
Example: new MyClass() calls MyClass constructor; malloc(sizeof(MyClass)) does not.

B) TRUE: new/delete are C++ operators defined in the language. malloc/free are C library functions from .

C) TRUE: You can overload operator new in a class for custom allocation strategies. malloc is a standard library function that cannot be overloaded.

D) TRUE: new MyClass* returns MyClass*. malloc returns void* and requires explicit cast: (MyClass*)malloc(...).

E) FALSE: Behavior is reversed. new throws std::bad_alloc exception on failure. malloc returns NULL pointer on failure and does not throw.