【发布时间】:2018-04-15 14:15:53
【问题描述】:
我收到核心转储错误(内存泄漏),我不确定它来自哪里。我尝试一次运行程序部分并将问题最小化。任何帮助,将不胜感激。谢谢
基本头文件。
#ifndef VEHICLE_H
#define VEHICLE_H
#include <iostream>
#include <string>
using namespace std;
class Vehicle{
protected:
char *name;
static ostream *out ;
public:
Vehicle();
Vehicle(string &n);
Vehicle (const Vehicle & b);
~Vehicle();
Vehicle& operator =(const Vehicle &b);
virtual void print(void) = 0;
virtual void read(void) = 0;
};
基础构造函数
#include "vehicle.h"
#include <iostream>
#include <cstring>
ostream * Vehicle :: out = &cout;
Vehicle :: Vehicle(){
name = new char;
strcpy(name, "");
}
Vehicle :: Vehicle(string &n){
int len = n.length()+ 1;
delete [] name;
name = new char[len];
strcpy(name,n.c_str());
}
Vehicle :: Vehicle(const Vehicle & v){
int len = strlen(v.name)+ 1;
delete [] name;
name = new char[len];
strcpy(name,v.name);
}
Vehicle & Vehicle::operator = (const Vehicle &b){
if(this == &b){
return *this;
}
int len = strlen(b.name)+ 1;
delete [] name;
name = new char[len];
strcpy(name,b.name);
return *this;
}
Vehicle:: ~Vehicle(){
delete [] name;
}
派生头文件
#ifndef MOTORVEHICLE_H
#define MOTORVEHICLE_H
#include <cstring>
#include "vehicle.h"
class MotorVehicle: public Vehicle{
protected:
string make;
string model;
double mpg;
public:
MotorVehicle();
MotorVehicle (const string &n,const string &m = "",const string &md = "", const double &mp = 0.0);
//accessor functions
string getName()const;
string getMake()const;
string getModel()const;
double getMpg()const;
ostream & getOut();
//mutator functions
string setName();
void setMake();
void setModel();
void setMpg();
void setOut(ostream & o);
//virtual functions
void print();
void read();
};
#endif // MOTORVEHICLE_H
机动车实施。这就是错误发生的地方。
#include "motorVehicle.h"
#include <string>
#include <iostream>
MotorVehicle:: MotorVehicle(): Vehicle(), make(""), model(""), mpg(0.0){
make = "";
model = "";
mpg = 0.0;
}
MotorVehicle :: MotorVehicle(const string &n, const string &m, const string &md, const double &mp){
int len = n.length()+ 1;
delete [] name;
name = new char[len];
strcpy(name,n.c_str());
make = m;
model = md;
mpg = mp;
}
//accessor functions
string MotorVehicle :: getName()const{
string temp(name);
return temp;
}
string MotorVehicle :: getMake()const {
return make;
}
string MotorVehicle :: getModel()const {
return model;
}
double MotorVehicle :: getMpg()const {
return mpg;
}
ostream & MotorVehicle :: getOut(){
return *out;
}
//mutator functions
string MotorVehicle :: setName(){
cerr << "dododd" <<endl;
string nm1;
cin >> nm1;
int len = nm1.length()+ 1;
delete [] name;
name = new char[len];
strcpy(name,nm1.c_str());
}
void MotorVehicle :: setMake(){
cin >> make;
}
void MotorVehicle :: setModel(){
cin >> model;
}
void MotorVehicle :: setMpg(){
cin >> mpg;
}
void MotorVehicle :: setOut(ostream & o){
out = &o;
}
//virtual function
void MotorVehicle :: print(){
*out << name << make << model << mpg <<" ";
}
void MotorVehicle :: read(){
*out << "Please enter name for this Vehicle: " << endl;
setName();
*out << "Please enter make for this Vehicle: " << endl;
setMake();
*out << "Please enter model for this Vehicle: " << endl;
setModel();
*out << "Please enter miles per gallon for this Vehicle: " << endl;
setMpg();
}
卡车头
#ifndef TRUCK_H
#define TRUCK_H
#include "motorVehicle.h"
class Truck: public MotorVehicle{
protected:
double cargoCapacity;
public:
Truck();
Truck (string &n,string &m,string &md, double &mp , double &cp);
//accessor functions
double getcargoCapacity()const;
//mutator functions
void setcargoCapacity();
//virtual functions
void print();
void read();
};
#endif
卡车实施
#include "truck.h"
Truck :: Truck() : MotorVehicle(){
cargoCapacity = 0.0;
}
Truck :: Truck(string &n, string &m, string &md, double &mp, double &cp ): MotorVehicle (n,m,md,mp){
cargoCapacity = 0.0;
}
double Truck :: getcargoCapacity()const{
return cargoCapacity;
}
void Truck :: setcargoCapacity(){
cin >> cargoCapacity;
}
void Truck :: print(){
*out << name << make << model << mpg << cargoCapacity << " ";
}
void Truck :: read(){
*out << "Please enter cargo capacity for this Vehicle: " << endl;
setcargoCapacity();
*out << "Please enter name for this Vehicle: " << endl;
setName();
*out << "Please enter make for this Vehicle: " << endl;
setMake();
*out << "Please enter model for this Vehicle: " << endl;
setModel();
*out << "Please enter MPG for this Vehicle: " << endl;
setMpg();
}
车头
#ifndef CAR_H
#define CAR_H
#include "motorVehicle.h"
class Car: public MotorVehicle{
private:
string trim;
public:
Car();
Car (string &n,string &m,string &md,double &mp, string &t);
//accessor functions
string getTrim()const;
//mutator functions
void setTrim();
//virtual functions
void print();
void read();
};
#endif // CAR_H
汽车实施
#include "car.h"
Car :: Car (){
MotorVehicle();
}
Car :: Car(string &n, string &m, string &md, double &mp, string &t) : MotorVehicle (n,m,md,mp){
trim = "";
}
string Car :: getTrim()const{
return trim;
}
void Car :: setTrim(){
cin >> trim;
}
void Car :: print(){
*out << name << make << model << mpg << trim << " ";
}
void Car :: read (){
*out << "Please enter trim for this Vehicle: " << endl;
setTrim();
*out << "Please enter name for this Vehicle: " << endl;
setName();
*out << "Please enter make for this Vehicle: " << endl;
setMake();
*out << "Please enter model for this Vehicle: " << endl;
setModel();
*out << "Please enter MPG for this Vehicle: " << endl;
setMpg();
}
主要
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <typeinfo>
#include "motorVehicle.h"
// car.h & truck.h included twice to test #ifndef #define ... #endif structure
#include "car.h"
#include "car.h"
#include "truck.h"
#include "truck.h"
typedef vector<MotorVehicle *> vectorOfMotorVehicle;
using namespace std;
// prompt the user for the Vehicle type to create, and get the reply
string prompt(void);
int main() {
vectorOfMotorVehicle v; // store dynamically created MotorVehicles
// or objects derived from MotorVehicles
int numVehicles = 0; // denotes how many MotorVehicles we have
string reply; // indicates type of MotorVehicle to build
bool error = false; // set when a user response is in error
string outputLocation; // where the output of print() will go
ofstream *out = NULL;
MotorVehicle *m;
// uncomment following line to test that Vehicle is an abstract base class
// Vehicle theVehicle;
// push a Vehicle into the vector so first "real" Vehicle is at position 1
m = new MotorVehicle("None");
v.push_back(m);
// chose where the output will go
cout << "Where would you like the output? ";
cin >> outputLocation;
if (outputLocation == "stdout") {
; // no action to take, because stdout (i.e., cout) is the default
} else if (outputLocation == "stderr") {
v[0]->setOut(cerr);
} else {
out = new ofstream;
out->open(outputLocation.c_str());
if (out->fail()) {
cerr << "Error: error writing to " << outputLocation << endl;
return 1;
}
v[0]->setOut(*out);
}
// get the type of Vehicle to create
reply = prompt();
// loop, reading vehicle descriptions, until a "quit" command is received
while (reply != "quit") {
// create the new MotorVehicle object and push it into the vector
switch (toupper(reply[0])) {
case 'T' : m = (MotorVehicle *) (new Truck);
v.push_back(m);
break;
case 'C' : m = (MotorVehicle *) (new Car);
v.push_back(m);
break;
case 'Q' : reply = "quit";
continue;
default : cerr << "Incorrect response\n\n";
error = true;
}
// if no error, then we have a new Vehicle to initialize via read()
if (!error) {
numVehicles++;
v[numVehicles]->read();
}
// reset error flag, and request a new Vehicle type
error = false;
reply = prompt();
}
// report on what Vehicles were created to test read() and print()
for (int i = 0; i <= numVehicles; i++) {
//*out << "Vehicle " << i << endl;
// print the Vehicle characteristics (attributes)
v[i]->print();
//*out << endl;
// free the storage for this Vehicle
delete v[i];
}
// if we opened an output file, then close it
if (out != NULL) {
out->close();
delete out;
}
return 0;
}
// prompt the user for the Vehicle type to create, and get the reply
string prompt() {
string reply; // the user reponse to the prompt
// prompt for and get user response
cout << "\nWhich type of vehicle would you like to initialize"
<< "\n--car or truck (or \"quit\" to exit): ";
cin >> reply;
return reply;
}
当派生类尝试调用 setName 函数时,它会不断抛出核心转储。泄漏从何而来?
谢谢
【问题讨论】:
-
name = '\0';有效地将0分配给该指针,而不是它的内容。 -
内存泄漏本身很少导致核心转储。而是使用 debugger 来捕捉正在运行的崩溃,您会准确地发现它在哪里。您还可以使用像 Valgrind 这样的内存调试器来帮助您查找内存错误(包括泄漏)。我还建议您花点时间阅读 Eric Lippert 的 How to debug small programs。
-
char *name;为什么,你已经知道 std::string。 -
@manni66 这是一个要求。
-
@user0042 strcpy(name, "");我试过这个但没有效果..