gpt4 book ai didi

c++ - 构造函数和 operator= 具有动态内存分配

转载 作者:行者123 更新时间:2023-11-30 03:00:25 24 4
gpt4 key购买 nike

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

我正在尝试运行一个小程序来练习 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 行,程序不会崩溃。

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

有什么帮助吗?

最佳答案

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;
}

这是复制c-tor。没有以前分配的成员。 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[] 分配的对象。

关于c++ - 构造函数和 operator= 具有动态内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12074657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com