Say I have an array declared as:
int* array;
and I fill the array with however many integers.
If I later say
array = NULL;
does this free the memory that the numbers in array occupied, or does it just make the array unusable while the memory still lingers?
If you allocated the memory using
new [](more likely, since the name of the variable isarray) ornew, this will result in a memory leak.Setting the pointer to
NULLwill not release the memory, only reassign the pointer. In other words, you will lose any chance to refer to the previously allocated memory.Use
delete[]to release memory allocated withnew[], anddeleteto release memory allocated withnew.However, consider not using raw pointers and manual memory management at all, they are most often not needed and error-prone. The C++ Standard Library comes with collections and smart pointers that perform memory management under the hood and keep you safe from this kind of mistakes.