How must int* a = new int[10]; be released?
new[] pairs with delete[]. Mixing the forms – or with free – is undefined behaviour.
Pointers and Dynamic Memory · Exercise 1 of 4
Twenty multiple-choice questions on C++ pointers: addresses, dereferencing, nullptr, pointer arithmetic, const pointers, new and delete, leaks, dangling pointers and RAII. Pick an option to check your answer instantly.
How must int* a = new int[10]; be released?
new[] pairs with delete[]. Mixing the forms – or with free – is undefined behaviour.
What does int* p = nullptr; delete p; do?
That is why setting a pointer to nullptr after delete protects against an accidental second delete.
Which code leaks memory?
The first int's address is overwritten before it is deleted, so it can never be freed.
What is a dangling pointer?
It still holds the old address, so it looks valid, but using it is undefined behaviour. (An uninitialized pointer is called a wild pointer.)
Which is true about void* v?
Without a type the compiler does not know how many bytes to read. static_cast<int*>(v) converts back.
On a typical 64-bit system, what are sizeof(char*) and sizeof(double*)?
A pointer holds an address, and addresses have the same size whatever they point to.
Which statement about stack and heap memory is correct?
The stack is fast but limited in size; the heap is large and flexible but must be managed (ideally by RAII objects).
What is the idea behind RAII?
std::vector, std::string, std::unique_ptr, std::lock_guard and std::fstream all follow RAII.
Why would a function take an int** parameter?
A pointer passed by value is a copy; to modify the caller's pointer, pass its address (or a reference int*&).
What does plain new do when the requested memory is not available?
new (std::nothrow) T is the variant that returns nullptr instead of throwing.
Timed online tests with instant scores and explanations.