dynamic memory question

I just started learning c++. I'm using the tut on this site and I bought 2 books.

I'm now experimenting with dynamic memory and I got confused. When I try to delete my used memory it doesn't seem completly deleted.

Here is the code:
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
34
35
36
37
38
39
40
41
#include <iostream>

using namespace std;

int main(){
	// my little test
	int i,n;
	int * p;
	i = 5;
	
	p= new (nothrow) int[i];
	if (p == 0){
		cout <<"Error: memory could not be allocated";
	}else{
		p[0] = 123;
		p[1] = 234;
		p[2] = 345;
		p[3] = 456;
		p[4] = 567;
		
		cout <<"Before delete:"<< endl;
		for (n=0; n<i; n++){
			cout <<"p["<< n <<"]: "<< p[n] << endl;
		}
		delete[] p;
		
		cout <<"\nAfter delete:"<< endl;
		for (int wtf=0; wtf<i; wtf++){
			cout <<"p["<< wtf <<"]: "<< p[wtf] << endl;
		}
	}

	
	// wait for the user to exit
	short int exitNumber;
	cout <<"\n\nPress ctrl-c to exit.";
	cin >> exitNumber;
	
	// terminate the program
	return 0;
}


And here is the output:
Before delete:
p[0]: 123
p[1]: 234
p[2]: 345
p[3]: 456
p[4]: 567

After delete:
p[0]: 7409408
p[1]: 7416464
p[2]: 345
p[3]: 456
p[4]: 567


How come p[0] and p[1] are deleted, but p[2], p[3] and p[4] aren't?
Because c++ never said said to erase deleted memory, delete just unallocates the memory. What you have there is a stray pointer, it points to a memory location that is no longer meaningful in it's previous context. If you were to have a much longer, and more complicated program, those values would eventually be overwritten, but for your program, nothing ever claims that memory and writes over it.

Check it:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

using namespace std;

int main()
{
	// my little test
	int i,n;
	int * p;
	i = 5;

	p= new (nothrow) int[i];
	if (p == 0){
		cout <<"Error: memory could not be allocated";
	}else{
		p[0] = 123;
		p[1] = 234;
		p[2] = 345;
		p[3] = 456;
		p[4] = 567;

		cout <<"Before delete:"<< endl;
        cout << p << endl;
		for (n=0; n<i; n++){
			cout <<"p["<< n <<"]: "<< p[n] << endl;
		}
		delete[] p;
		p= new (nothrow) int[i];

		cout <<"\nAfter delete:"<< endl;
		cout << p << endl;
		for (int wtf=0; wtf<i; wtf++){
			cout <<"p["<< wtf <<"]: "<< p[wtf] << endl;
		}
	}

	delete[] p;
	cout << *(new (nothrow) int[i]);
	p = new (nothrow) int[i];
	cout <<"\nAfter delete:"<< endl;
	cout << p << endl;
	for (int wtf=0; wtf<i; wtf++){ //You will see completely arbitrary values here
		cout <<"p["<< wtf <<"]: "<< p[wtf] << endl;
	}


	// wait for the user to exit
	short int exitNumber;
	cout <<"\n\nPress ctrl-c to exit.";
	cin >> exitNumber;

	// terminate the program
	return 0;
}


If you wanted to, you could just make a pointer and read through memory until you hit a piece of memory that for one reason or other can't be read by your program
Last edited on
Aah..

Thanks for the fast reply. I gues my program works as inteded then :)
Yes, do not forget that delete [] p only marks the memory spaces p points to as deallocated. Since p may still contain the address of the deallocated memory, after typing delete [] p, you should type p=0, so that p doesnt point to them anymore.
Topic archived. No new replies allowed.