1

菜鸟在这里。我正在从书中做一个练习,编译器没有报告任何错误,但是当我尝试运行它时程序崩溃了。

我正在尝试运行一个小程序来执行 Cow 类的不同方法。它显式地具有:构造函数、默认构造函数、复制构造函数、析构函数、重载赋值运算符和显示其内容的方法。

我将把整个项目:

类规范:

//cow.h -- For project Exercise 12.1.cbp

class Cow
{
    char name[20]; // memory is allocated in the stack
    char *hobby;
    double weight;
public:
    Cow();
    Cow(const char *nm, const char *ho, double wt);
    Cow(const Cow &c);
    ~Cow();
    Cow & operator=(const Cow &c);
    void ShowCow() const; // display all cow data
};

方法实现:

// cow.cpp -- Cow class methods implementation (compile with main.cpp)

#include <cstring>
#include <iostream>
#include "cow.h"

Cow::Cow() // default destructor
{
    strcpy(name, "empty");
    hobby = new char[6]; // makes it compatible with delete[]
    strcpy(hobby, "empty");
    weight = 0.0;
}

Cow::Cow(const char *nm, const char *ho, double wt)
{
    strcpy(name, nm); // name = nm; is wrong, it copies the address of the argument pointer (swallow copying)
    /*if (name[20] != '\0') // if it's not a string, make it a string (in case nm is larger than 20)
        name[20] = '\0';*/
    hobby = new char[strlen(ho) + 1]; // allocates the needed memory to hold the argument string
    strcpy(hobby, ho); // copies the pointed-to data from the argument pointer to the class pointer
    weight = wt;
}

Cow::Cow(const Cow &c) // copy constructor
{
    strcpy(name, c.name); // copies the value to the desired address
    char *temp = hobby; // stores the address of the memory previously allocated with new
    hobby = new char[strlen(c.hobby) + 1];
    strcpy(hobby, c.hobby); // copies the value to the new address
    delete[] temp; // deletes the previously new allocated memory
    weight = c.weight;
}

Cow::~Cow()
{
    delete[] hobby;
}

Cow & Cow::operator=(const Cow &c) // overloaded assignment operator
{
    strcpy(name, c.name);
    char *temp = hobby;
    hobby = new char[strlen(c.hobby) + 1];
    strcpy(hobby, c.hobby);
    delete[] temp;
    weight = c.weight;
    return *this;
}

void Cow::ShowCow() const
{
    std::cout << "Name: " << name << '\n';
    std::cout << "Hobby: " << hobby << '\n';
    std::cout << "Weight: " << weight << "\n\n";
}

客户:

// main.cpp -- Exercising the Cow class (compile with cow.cpp)

#include "cow.h"
#include <iostream>

int main()
{
    using std::cout;
    using std::cin;

    Cow subject1; // default constructor
    Cow subject2("Maria", "Reading", 120); // non-default constructor
    Cow subject3("Lula", "Cinema", 135);
    subject1 = subject3; // overloaded assignment operator
    Cow subject4 = subject2; // copy constructor
    subject1.ShowCow();
    subject2.ShowCow();
    subject3.ShowCow();
    subject4.ShowCow();

    cin.get();
    return 0;
}

我隐藏了代码的某些部分以定位可能的问题,并且该程序似乎不喜欢这两行:

subject1 = subject3;
Cow subject4 = subject2

特别是在重载的赋值运算符和复制构造函数中,如果我隐藏该delete[] temp行,程序不会崩溃。

我完全是菜鸟,可能有点愚蠢,但我看不出我在这些定义中做错了什么。

有什么帮助吗?

4

2 回答 2

3
Cow::Cow(const Cow &c) // copy constructor
{
    strcpy(name, c.name); // copies the value to the desired address
    char *temp = hobby; // stores the address of the memory previously allocated with new
    hobby = new char[strlen(c.hobby) + 1];
    strcpy(hobby, c.hobby); // copies the value to the new address
    delete[] temp; // deletes the previously new allocated memory
    weight = c.weight;
}

这是复制品。没有以前分配的成员。this->hobby指向随机垃圾。所以当你delete[] temp(即this->hobby在新任务之前)时,你会得到 UB。

Cow & Cow::operator=(const Cow &c) // overloaded assignment operator
{
    strcpy(name, c.name);
    char *temp = hobby;
    hobby = new char[strlen(c.hobby) + 1];
    strcpy(hobby, c.hobby);
    delete[] temp;
    hobby = name;
    weight = c.weight;
    return *this;
}

hobby = name是不正确的,因为它会导致内存泄漏 + 析构函数将尝试删除未分配的对象operator new[]

于 2012-08-22T13:53:14.117 回答
0

@ForEveR 已经注意到您的复制构造函数正在删除从未分配的内存。

But I'm going to argue that the entire exercise is flawed and is teaching C with classes, NOT C++. If you used string all your problems would go away because you wouldn't have to manage resources directly. This eliminates the need for you to write a destructor or copy assignment/constructor entirely. For example:

class Cow
{
    std::string name;
    std::string hobby;
    double weight;
public:
    Cow();
    Cow(const std::string& nm, const std::string& ho, double wt);
    void ShowCow() const; // display all cow data
};
于 2012-08-22T14:05:33.620 回答