What does the C++ expression new int(5) produce?
Correct answer
C. A pointer to a heap int initialised to 5
14 views
C. A pointer to a heap int initialised to 5
14 views
new allocates storage from the free store, constructs the object there, and yields a pointer, so new int(5) initialises the int to 5 and must later be released with delete. In contrast, new int[5] allocates an array of five ints and requires delete[] rather than delete. Mixing the scalar and array forms is undefined behaviour, which is one reason modern C++ prefers std::vector and smart pointers.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.