Pointer Deletion

So I have a few other posts that I made here, and everyone was so helpful and positive that I came back with another question!! Yea!!

So I do believe I have created my linked list correctly, but Im not so sure about the deleting.....

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
55
56
#include <stdlib.h>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	srand(time(NULL));
	
	struct Bomb {
		int x;
		int y;
		Bomb *Next;
		Bomb(){
			Next = NULL;
			x=0;
			y=0;
		}
	};
	
	Bomb *Starting = new Bomb;  //setting up initial list... 
	Bomb *Ending = Starting;
	
	int maxb,minb,numb;
	cout << "Range Max: "; cin >> maxb; cout << endl;
	cout << "Range Min: "; cin >> minb; cout << endl;
	cout << "How Many? "; cin >> numb; cout << endl;
	
	int i;for(i=0;i<numb;i++){
		Ending->Next = new Bomb;            
		Ending->x = rand() % maxb + minb;
		Ending->y = rand() % maxb + minb;
		Ending = Ending->Next;
	}
	i=1; //reset for counter
	
	Bomb *Temp = Starting->Next;
	cout << "Bomb" << i << "\n\tX Coordinate: " << Starting->x << "\n\tY Coordinate: " << Starting->y << endl << endl;
	while(Temp->Next != NULL){
		i++;
		cout << "Bomb" << i << "\n\tX Coordinate: " << Temp->x << "\n\tY Coordinate: " << Temp->y << endl << endl;
		Temp = Temp->Next;
	}
	cout << "Bomb" << i << "\n\tX Coordinate: " << Temp->x << "\n\tY Coordinate: " << Temp->y << endl << endl;
	
	//deletion/deallocation of memory
	Temp = Starting;
	delete Starting;
	while(Temp->Next != NULL){
		Ending=Temp;
		Temp=Temp->Next;
		delete Ending;
	}
	delete Temp;
	return 0;
}



Also, my random number range algorithm could use some "pointers", (hehe), as well!
Last edited on
Looks correct.

Another way I've seen people do it is by recursing in the destructor:
~Node() { if (next != null) delete next; }
Of course, if you'd ever want to delete a single item, you'll end up deleting everything behind it as well.
Oh, didn't look closely enough:

Don't define a struct (or anything, for that matter) inside a function. Move your struct definition outside of main(). Usually you'll even want to put it in a separate file.
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
#include <stdlib.h>
#include <iostream>
#include <ctime>
using namespace std;

    struct Bomb {
		int x;
		int y;
		Bomb *Next;
		Bomb(){
			Next = NULL;
			x=0;
			y=0;
    }

int main()
{
	srand(time(NULL));
	
	Bomb *Starting = new Bomb;  //setting up initial list... 
	Bomb *Ending = Starting;
	
	int maxb,minb,numb;
	cout << "Range Max: "; cin >> maxb; cout << endl;
	cout << "Range Min: "; cin >> minb; cout << endl;
	cout << "How Many? "; cin >> numb; cout << endl;
	
	int i;for(i=0;i<numb;i++){
		Ending->Next = new Bomb;            
		Ending->x = rand() % (maxb-minb+1) + minb;        //also changed my min max algorithm, thanks to another post on this forum
		Ending->y = rand() % (maxb-minb+1) + minb;
		Ending = Ending->Next;
	}
	i=1;
	Bomb *Temp = Starting->Next;
	cout << "Bomb" << i << "\n\tX Coordinate: " << Starting->x << "\n\tY Coordinate: " << Starting->y << endl << endl;
	while(Temp->Next != NULL){
		i++;
		cout << "Bomb" << i << "\n\tX Coordinate: " << Temp->x << "\n\tY Coordinate: " << Temp->y << endl << endl;
		Temp = Temp->Next;
	}
	
	//deletion/deallocation of memory           It used to look like this....

	while(Starting->Next != NULL){            //Temp = Starting;
		Temp=Starting;                                 //delete Starting;
		Starting=Temp->Next;                    //while(Temp->Next != NULL){
		delete Temp;                                    //Ending=Temp;
	}                                                                 //Temp=Temp->Next;
	delete Starting;                                       //delete Ending;}
	return 0;                                                    //delete Temp;
}


haha thanks Gaminic. I was thinking that as I was lazily typing it in main. I had a slight mix up in my deletion loop. Note the changes. Let me know anyone if they notice any memory leaks.
Last edited on
Topic archived. No new replies allowed.