Using new and delete on classes

Hello,

Should be a pretty simple answer to this one, I would hope. Just need to clear up some confusion.

I created a class called part {}. It behaves just as I expected, except when the delete keyword is used. You can see from the output that as it goes along, it is destructing not only the last created class, but also those created previous to it.

So my question is, why does the destructor get called for the previous parts? Shouldn't they no longer stick around after I use delete on them? My thinking is that once I delete the first part, and since the same memory address is used to create the second part, that memory address should hold no longer know anything about the first part, letalone be able to use its destructor.

But then again, this is why I'm in the beginners forum in the first place, I guess. Any help is appreciated.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class part {
      
     public: 
      double x_cm;
      double y_cm;
      double z_cm;
      
      string type;
      string name;
      int part_number;
      static int numParts;
      
      double I[3][3];
      double Rg[3]; 
      
      double mass;
      
      int section;
      int section_support;

      part(double x, double y, double z, double input_mass, int input_section,
           int input_section_support, string input_type, string input_name) {
          
          x_cm = x;
          y_cm = y;
          z_cm = z;
          mass = input_mass;
          section = input_section;
          section_support = input_section_support;
          type = input_type;
          name = input_name;
          
          part_number = ++numParts;

          cout << "Part " << part_number << " of " << numParts << " created\n"; 
      };
      
      ~part() {
          cout << "Destructing part number " << part_number << endl << endl;
      };
      
      void display();

};

int part::numParts = 0;

void part::display() {
     
     cout << "\n\nProperties of \"" << name << "\":\n";
     cout << "x_cm = " << x_cm << endl;
     cout << "y_cm = " << y_cm << endl;
     cout << "z_cm = " << z_cm << endl;
     cout << "mass = " << mass << endl;
     cout << "section = " << section << endl;
     cout << "section_support = " << section_support << endl;
     cout << "type = " << type << endl;
     cout << "name = \"" << name << "\"" << endl;
}

int main () {
    
    vector<part> parts;
    
    part* newPart = new part(0,0,0,10,2,1,"Generic Part","First Part");
    cout << "\nMemory address of new part = " << newPart << "\n";
    parts.push_back(*newPart);
    delete newPart;
    
    newPart = new part(1,1,1,10,2,1,"Generic Part","Second Part");
    cout << "\nMemory address of new part = " << newPart << "\n\n";
    parts.push_back(*newPart);
    delete newPart;
    
    newPart = new part(2,2,2,10,2,1,"Generic Part","Third Part");
    cout << "\nMemory address of new part = " << newPart << "\n\n";
    parts.push_back(*newPart);
    delete newPart;
    
    parts[0].display();
    parts[1].display();
    parts[2].display();

    cin.get(); 
    return 0;
}


And here is the output:
-----------------------

Part 1 of 1 created

Memory address of new part = 0x4d3d38
Destructing part number 1

Part 2 of 2 created

Memory address of new part = 0x4d3d38

Destructing part number 1

Destructing part number 2

Part 3 of 3 created

Memory address of new part = 0x4d3d38

Destructing part number 1

Destructing part number 2

Destructing part number 3



Properties of "First Part":
x_cm = 0
y_cm = 0
z_cm = 0
mass = 10
section = 2
section_support = 1
type = Generic Part
name = "First Part"


Properties of "Second Part":
x_cm = 1
y_cm = 1
z_cm = 1
mass = 10
section = 2
section_support = 1
type = Generic Part
name = "Second Part"


Properties of "Third Part":
x_cm = 2
y_cm = 2
z_cm = 2
mass = 10
section = 2
section_support = 1
type = Generic Part
name = "Third Part"


This was explained a few topics ago. What happens is when you use push_back on your vector. It has to re-allocate extra space for the new one, so it copies all of the objects into a new vector space, deletes the old ones and points itself to the new one.

If you set the initial capacity of your vector to something above 3 you won't see it.
Topic archived. No new replies allowed.