Which of the following should be used to free memory from a pointer allocated using the “new” operator?
a) delete
b) free()
c) dealloc()
d) None of the above
Answer:
a) delete
Explanation:
In C++, the delete
operator should be used to free memory allocated by the new
operator. Using free()
with new
-allocated memory is incorrect and can lead to undefined behavior, as free()
is intended for memory allocated with malloc()
in C. Proper memory management is crucial to avoid memory leaks and ensure the stability of the program.
Understanding the correct pairing of memory allocation and deallocation functions is essential for writing robust C++ programs.