Dynamic Memory Allocation deleting pointer!

Is this correct way freeing memory?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

const int size = 10;
int main()
{
	srand(time(NULL));
	int *pointerArray = new int[size];
	
	for (int i = 0; i < size; i++)
	{
		pointerArray[i] = rand() % 100;
		cout << pointerArray[i] << endl;
	}
	
	delete[] pointerArray;
	pointerArray = NULL;

	system("pause");
	return 0;
}
That looks ok.
new should be matched with delete

new [] should be matched with delete []

The setting of the pointer to NULL (or nullptr in C++11) is often good practice, as it avoids having a dangling pointer which seems to point to a valid address, but doesn't.
As long as I type delete [] pointerArray, it will delete whole array. I mean I dont have to type for example; for loop , delete [i] pointerArray; ? Right?
As long as I type delete [] pointerArray, it will delete whole array.
Yes.

I mean I dont have to type for example; for loop , delete [i] pointerArray; ? Right?
Right. you don't have to do that.

Your original code uses the new [] operator precisely one time, so you need delete [] precisely once too.

You didn't use new for each element, so you don't need to use delete for each element.
Thank you for your replay, I appreciate it!
Topic archived. No new replies allowed.