Deleting array from heap

Hi, I am learning from a webpage, currently arrays pointers and the heap, so i am practicing by making an array on the heap. But what the website shows, I do not think is right.

So what I am trying to do is create an array on the heap, print the elements and then delete the array from the heap. So I have followed basically what the website shows, but I think this only deletes the pointer to the array, and leaves the array on the heap. If this is the case, how do I delete the array from the heap?

1
2
3
4
5
6
 int *arr = new int [10];	
	for (int i = 0; i < 5; i++)
	{
	cout << "Array" << arr[i] << endl;
	}
delete[] arr;        
No, that definitely deletes the array data that's stored on the heap. "Delete" here really just means that the memory that was allocated is now able to be reused for other purposes. It's almost certainly still allocated to the program process and probably still has the old data in it (or at least some of the old data). The delete operator doesn't delete the pointer itself since that's a local variable stored on the stack. In fact, it should still contain the address that was assigned to it by the "new" operator.

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
26
27
28
29
30
31
32
33
#include <iostream>

int main()
{
    using std::cout;
    const int Size = 5;

    int *p = new int[Size];

    for (int i = 0; i < Size; ++i) p[i] = (i + 1) * 10;

    // print the pointer value (address of the array data) and the array data.
    cout << p; for (int i = 0; i < Size; ++i) cout << ' ' << p[i]; cout << '\n';

    delete [] p;

    // p should still point to the same address
    // and the old data is probably still accessible and unchanged (mostly, at least)
    // It is, however, "undefined behavior" to access the data after it's "deleted",
    // so you can't rely on any particular implementation's behavior.
    cout << p; for (int i = 0; i < Size; ++i) cout << ' ' << p[i]; cout << '\n';

    // assigning some new memory to p (will the old address be reused?)
    p = new int[Size];

    for (int i = 0; i < Size; ++i) p[i] = (i + 1) * 100;

    // on my system, the address returned by the first "new" was also returned by the second
    // if we hadn't deleted the array data the first time then we would definitely have received a different address
    cout << p; for (int i = 0; i < Size; ++i) cout << ' ' << p[i]; cout << '\n';

    delete [] p;
}

Last edited on
What you're doing is correct. It only gets a little more complex once you are dealing with more than one dimensional arrays.

What you're doing right now is printing a bunch of garbage though. When you dynamically allocate your data structure, the values do not really have any meaning to them. If you try to access them, you'll most likely see some errors (if you run it with a debugger like Valgrind).

It should be noted that for every new keyword use, there should be a corresponding delete keyword to handle said data once it goes out of scope.
@fiji, good point about the garbage values. I forgot to mention that. However,
fiji wrote:
If you try to access them, you'll most likely see some errors

There won't be any "errors", just meaningless values.
Thank you for the replies. I think it was the garbage values that you mention that was causing me issues, even after I was using delete, it was still feeding that back to me, so I just assumed the delete wasnt clearing the array.

If I can ask just a second question while here, when creating a array on the heap, does the size always need to be stated? Can I not just do int *p = new int [], and leave it at that, to be filled at a later date?

Thank you both for the replies, greatly appreciated
Can I not just do int *p = new int [], and leave it at that, to be filled at a later date?


No. If you want to fill things in at a later date, that would mean allocating a new value on the heap every time you want to add something to the list. One example of this is a binary tree or a linked list, but these get a little more complex. C++ provides a few libraries that can already achieve this for you, so you do not have to write your own data structure unless you want to get very specific.

dutch is correct. There will not be any "errors" in the program, but your debugger should be able to tell you something when your program is accessing garbage or areas where it should not have access to.
Last edited on
Topic archived. No new replies allowed.