Practice mode C++

Memory allocated with int* p = new int; must be released with which statement?

Correct answer B. delete[] p;

Explanation

new[] must be paired with delete[], so that the runtime knows an array was allocated and calls every element's destructor. Using plain delete on an array, or free() on memory from new, is undefined behaviour. Simply setting p to nullptr leaks the memory. In modern code std::vector or std::unique_ptr handles this pairing for you.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.