Spaceship.h
#include <iostream>
#include <string>
#include <math.h>
#include <vector>
class Position {
public:
Position (std::string Name, double x, double y, double z);
private:
std::string Name;
double x, y, z; //Coordinates on a 3D map
};
class Flight {
public:
Flight (Position *Start, Position *End);
~Flight();
private:
Position *Start;
Position *End;
};
class Spaceship {
public:
Spaceship (std::string Name, Flight *flightPlan);
~Spaceship();
private:
std::string Name;
Flight *flightPlan;
};
class Universe {
public:
Universe ();
~Universe ();
void addSpaceship (Spaceship *Spaceship);
private:
std::vector <Spaceship*> *Spaceships; // A vector of Spaceship pointers
std::vector <Spaceship*>::iterator start, end;
int numberOfSpaceships;
};
Spaceship.cpp
#include "Spaceship.h"
Position::Position (std::string Name, double x, double y, double z) {
this->Name = Name;
this->x = x;
this->y = y;
this->z = z;
}
//------------------------------------------------------------
Flight::Flight (Position *Start, Position *End) {
this->Start = Start;
this->End = End;
}
Flight::~Flight () {
delete this->Start;
delete this->End;
}
//--------------------------------------------------------------
Spaceship::Spaceship (std::string Name, Flight *flightPlan) {
this->Name = Name;
this->flightPlan = flightPlan;
}
Spaceship::~Spaceship () {
delete this->flightPlan;
}
void Spaceship::printFlightPlan () {
std::cout << "Spaceship name: " << this->Name << std::endl;
this->flightPlan->printFlightPlan();
}
//---------------------------------------------------------------
Universe::Universe () {
this->Spaceships = new std::vector <Spaceship*>();
this->start = this->Spaceships->begin();
this->end = this->Spaceships->end();
this->numberOfSpaceships = Spaceships->size();
}
Universe::~Universe() {
delete this->Spaceships;
}
void Universe::addSpaceship(Spaceship *Spaceship) {
this->Spaceships->push_back(Spaceship);
this->start = this->Spaceships->begin();
this->end = this->Spaceships->end();
}
int main (void) {
Position *Start = new Position("Home", 45.566, 34.5677, -12.4565);
Position *End = new Position ("Far Far Away", 67.4564, -56.6765, 23.4565);
Flight *virginFlight = new Flight(Start, End);
Spaceship *sp1 = new Spaceship("Virgin Mary", virginFlight);
Spaceship *sp2 = new Spaceship("Something else", virginFlight);
Universe *alpha = new Universe();
alpha->addSpaceship(sp1);
alpha->addSpaceship(sp2);
delete alpha;
return 0;
}
好吧,这应该是我最干净的代码版本。它只有构造函数和析构函数。如果我错过了一些变量/函数,请假设它们在那里。这编译并运行得很好。我唯一的问题是内存泄漏。
Valgrind报告分配&gt;至少删除2倍。
答案 0 :(得分:1)
你的程序泄露了。这是由于您的代码中的Spaceship *的std :: vector。只有当STL向量容器包含不是指向对象的指针的对象时,它才能管理和释放它的内部存储器。
这里当您向载体添加Spaceship并且删除alpha时,不会释放主程序中sp1和sp2指向的内存。
在现代C ++中,您应该避免使用原始指针,而是尝试使用智能指针。在这里,我尝试更改您的代码,我认为现在不会泄漏。
SpaceShip.h
#include <iostream>
#include <string>
#include <math.h>
#include <vector>
#include <memory>
class Position {
public:
Position(std::string Name, double x, double y, double z);
private:
std::string Name;
double x, y, z; //Coordinates on a 3D map
};
class Flight {
public:
Flight(Position *Start, Position *End);
~Flight();
void printFlightPlan();
private:
Position *Start;
Position *End;
};
class Spaceship {
public:
Spaceship(std::string Name, Flight *flightPlan);
~Spaceship();
void printFlightPlan();
private:
std::string Name;
Flight *flightPlan;
};
class Universe {
public:
Universe();
~Universe();
void addSpaceship(std::shared_ptr<Spaceship>);
private:
std::vector <std::shared_ptr<Spaceship>> *Spaceships; // A vector of Spaceship pointers
std::vector <std::shared_ptr<Spaceship>>::iterator start, end;
int numberOfSpaceships;
};
#include "Spaceship.h"
Position::Position(std::string Name, double x, double y, double z) {
this->Name = Name;
this->x = x;
this->y = y;
this->z = z;
}
//------------------------------------------------------------
Flight::Flight(Position *Start, Position *End) {
this->Start = Start;
this->End = End;
}
Flight::~Flight() {
/*delete this->Start;
delete this->End;*/
}
void Flight::printFlightPlan() {
}
//--------------------------------------------------------------
Spaceship::Spaceship(std::string Name, Flight *flightPlan) {
this->Name = Name;
this->flightPlan = flightPlan;
}
Spaceship::~Spaceship() {
delete this->flightPlan;
}
void Spaceship::printFlightPlan() {
std::cout << "Spaceship name: " << this->Name << std::endl;
this->flightPlan->printFlightPlan();
}
//---------------------------------------------------------------
Universe::Universe() {
this->Spaceships = new std::vector<std::shared_ptr<Spaceship>>;
this->start = this->Spaceships->begin();
this->end = this->Spaceships->end();
this->numberOfSpaceships = Spaceships->size();
}
Universe::~Universe() {
//delete this->Spaceships;
}
void Universe::addSpaceship(std::shared_ptr<Spaceship> Spaceship) {
this->Spaceships->push_back(Spaceship);
this->start = this->Spaceships->begin();
this->end = this->Spaceships->end();
}
int main(void) {
Position *Start = new Position("Home", 45.566, 34.5677, -12.4565);
Position *End = new Position("Far Far Away", 67.4564, -56.6765, 23.4565);
Flight *virginFlight = new Flight(Start, End);
std::shared_ptr<Spaceship> sp1(new Spaceship("Virgin Mary", virginFlight));
std::shared_ptr<Spaceship> sp2(new Spaceship("Something else", virginFlight));
Universe *alpha = new Universe();
alpha->addSpaceship(sp1);
alpha->addSpaceship(sp2);
delete alpha;
return 0;
}