Delete operator clarify

closed account (4ET0pfjN)
I thought when delete operator is used, the pointer gives back this memory to the heap, so after I delete: p_to_num, why does dereferencing it still output: 87?

1
2
3
4
5
6
7
8
9
10
int main()
{ 
   int num = 87;
   int* p_to_num = #

   cout << p_to_num << endl;//outputs address of num
   delete p_to_num;
   cout << p_to_num << endl;
   cout << *p_to_num << endl;//Shouldn't pointer p_to_num point to nothing
}
delete p_to_num can delete only memory that was allocated with the new operator.
closed account (4ET0pfjN)
I guess I was a little mixed up, I tried w/ list and after I used delete, there was som random values when I dereference (i.e. list[0] really *list etc) so the output I got was:
0x692368
87
99
//After deleting list
0x692368//I thought after delete, list would point to NULL, but it still points to an initialized first element, is that correct?
6881476//dereferencing first indx so list[0] *list outputs this string of values
6890224//dido

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  int* list = new int[3];
  list[0] = 87;
  list[1] = 99;
  cout << list << endl;
  cout << list[0] << endl;//87
  cout << list[1] << endl;//99
	
  delete[] list;
  cout << list << endl;
  cout << list[0] << endl;
}


So delete destroys the contiguous block of memory allocated to it by the heap, and then when I try to access list[0], list[1] it's not continuous memory which is what an array is I'm guessing. But when I output cout << list, is this just a name for the first indx of array, which now has the remaining 3 indices destroyed so not continious memory any more?
Last edited on
delete does not change the value of the pointer passed to it.

delete calls the relevant destructors and releases the memory, but the pointer is passed by value and so not modified. It's your responsibility to set it to NULL (or nullptr) if it remains in scope.

It is an error to access the memory block that has been deleted, such a pointer is called a dangling reference. The memory has been returned to the heap and is available to further allocations made by the program. You should note that the error used to be so common that it has a name. This is exactly the sort of problem that C++ was designed to catch. It does so by allowing you to confine most of your allocations/deletes to classes.

A linked list uses the heap to get memory for it's nodes. And it releases these nodes back to the heap when deleting nodes. But a well designed linked list class completely hides heap access from its user.
closed account (4ET0pfjN)
1
2
3
4
5
6
7
8
9
10
11
int* list = new int[4];
	
	list[0] = 66;
	list[1] = 87;
	list[2] = 99;
	
	cout << list << endl;
	delete[] list;
	cout << list << endl;
	list[0] = 999;
	cout << list[0] << endl;

so after I delete list, if I want to reuse it, don' i have reinitalize it as in:
list = new int[4];

yet it still outputs: 999 after I deleted...
delete means that you no longer care about the memory that was allocated for your use by new. That's all it means.

It does NOT mean "please reset that memory to zero", it does NOT mean "make that memory impossible for me to access". All it means is that the memory is free to be used by other things. What happens if nothing else wants that memory? Nothing. Whatever values are in that memory will stay there, untouched, until something else does want to use that memory.
Last edited on
closed account (4ET0pfjN)
So if I dynamically created a new list, then make it point to list BUT program crashes as in:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
  int* list = new int[4];
	
  list[0] = 66;
  list[1] = 87;
  list[2] = 99;
	
  cout << list << endl;
  delete[] list;
  cout << list << endl;
  list[0] = 999;
	
  cout << list[0] << endl;
	
  int* otherList = new int[4];
	
  otherList = &list[1];/*THIS CAUSES PROGRAM TO CRASH*/


I thought it would work since otherList implies pointer to first element (&otherList[0]) so why can I not reassign what otherList points to.
why can I not reassign what otherList points to.

You can. This code compiles and runs.

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

using namespace std;

int main()
{  int* list = new int[4];
	
  list[0] = 66;
  list[1] = 87;
  list[2] = 99;
	
  cout << list << endl;
  delete[] list;
  cout << list << endl;
  list[0] = 999;
	
  cout << list[0] << endl;
	
  int* otherList = new int[4];
	
  otherList = &list[1];
}


Do bear in mind that if you ever try to read or write memory that you've previously deleted you're taking a risk of bad data and depending on how the OS is handing out memory, segFaulting.
Last edited on
I repeat, It is an error to access the memory block that has been deleted.

You may find this helpful.
http://en.wikipedia.org/wiki/Dangling_pointer
closed account (4ET0pfjN)
Moschops, I tried running that code, it crashes unless your compiler is better than the one I'm using, I'm using g++ as part of Code Blocks IDE.
The latest version of the standard states, after explaining that delete calls destructors and deallocation functions (which may themselves change the data, depending on how you wrote them, although for primitive types such as in the example code here, the values are not changed)

If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value (4.10), the deallocation function shall deallocate the storage referenced by the pointer, rendering invalid all pointers referring to any part of the deallocated storage. The effect of using an invalid pointer value (including passing it to a deallocation function) is undefined.


Anything could happen if you tried to access that memory; as kbw says, to access that memory is erroneous, and if you do so, you're asking for trouble.

To make a pointer, and give that pointer a value, and that value happens to be the address unallocated memory, is not an error. You can give a pointer any value you like (i.e. you can make it point at any memory location you want to). It's only a problem if you then try to access the memory that pointer is pointing to.

Which version of g++? I'm using 4.7.0
Ideone's g++ 4.3.4 is also happy with it: http://ideone.com/IPUgO

Edit: Reading back, I could have been clearer here.

This: otherList = &list[1];
involves an attempt to use list to access some deallocated storage (i.e. list[1], which is bad, but otherList = list; is not an attempt to access deallocated storage.
Last edited on
closed account (4ET0pfjN)
I'll look into it, I tried typing in Windows cmd: g++ -v and it gives me GCC compiler info though
gcc version 4.4.1 (TDM-2 mingw32)
Topic archived. No new replies allowed.