gpt4 book ai didi

c++ - C++中析构函数的一堆不清楚的东西

转载 作者:搜寻专家 更新时间:2023-10-31 00:01:30 24 4
gpt4 key购买 nike

我用 C++ 编写了一些非常简单的代码来对 vector 进行一些简单的操作。这是文件 vector.h 的内容:

#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED

class Vector {
int *coordinates;
int *size;
public:
Vector(int vector_size);
Vector(int*,int);
~Vector();
void print(void);
Vector operator +(Vector);
};
#endif

这是实现(文件:vector.cpp):

#include "vector.h"
#include <iostream>
using namespace std;

Vector::Vector(int vector_size) {
coordinates = new int[vector_size];
size = new int;
*size = vector_size;
}

Vector::Vector(int* vector_coordinates, int vector_size){
coordinates = vector_coordinates;
size = new int;
*size = vector_size;
}

void Vector::print(void){
cout << "[";
for (unsigned short int index =0; index<*size; index++){
cout << coordinates[index];
if (index < *size-1){cout << ", ";};
}
cout << "]\n";
}

Vector Vector::operator+ (Vector other) {
Vector temp(*(other.size));
if ((*temp.size)!=(*(this->size))){
throw 100;
}
int* temp_c = new int[*(other.size)];
int* other_c = other.coordinates;
for (unsigned short int index =0; index<*size; index++){
temp_c[index] = coordinates[index] + other_c[index];
}
temp.coordinates = temp_c;
return (temp);
}

Vector::~Vector(){
delete[] coordinates;
delete size;
}

在我的 main.cpp 中,我执行以下操作:

#include <iostream>
using namespace std;
#include "vector/vector.h"

const int size = 3;

int main() {
int *xxx = new int[size];
xxx[0]=4; xxx[1]=5; xxx[2]=-6;


Vector v(xxx,size);// v = [4, 5, -6]
Vector w(size);// w is a vector of size 3

w = v+v; // w should be w=[8,10,-12]
w.print();
return 0;
}

结果是:

[148836464, 5, -6, 17, 148836384, 0, 0, 17, 0, 0, 0, 17, 3, 0, 0, 17, 0, 0, 0, 17, 148836480, 0, 0 , 17, 0, 10, -12, 135025, 0, 0, 0, 0, 0, 0, , 0, 0,段错误

如果我从析构函数中删除这两行:

delete[] coordinates;
delete size;

一切都按预期工作,程序输出:

[8, 10, -12]

如果有任何解释,我将不胜感激......

更新 1: 我将我的 operator+ 方法更改为以下,但问题没有解决:

Vector Vector::operator+(Vector other) {
int size_of_other = *(other.size);
int size_of_me = *(this->size);
if (size_of_other != size_of_me) {
throw 100;
}
int* temp_c = new int[size_of_me];
int* other_c = other.coordinates;
for (unsigned short int index = 0; index < size_of_me; index++) {
temp_c[index] = coordinates[index] + other_c[index];
}
Vector temp(temp_c,size_of_me);
return (temp);
}

更新 2:我注意到使用运算符:

Vector Vector::operator+(Vector other);

我不会得到想要的结果。使其工作的修改是:

const Vector& Vector::operator+(const Vector& other) {
Vector temp(other.size);
for (unsigned short int index = 0; index < size; index++) {
cout << "("<< index <<") "<<coordinates[index] << "+"
<<other.coordinates[index] << ", "<< endl;
temp.coordinates[index] = coordinates[index] + other.coordinates[index];
}
return (temp);
}

更新 3:在更新 #2 之后,我收到编译器的警告,提示我返回了本地“temp”。我将我的代码更改为以下代码,它完全解决了所有问题并且工作正常(我返回了一个 copy 临时文件):

const Vector Vector::operator+(const Vector& other) const{
Vector temp(other.size);
for (unsigned short int index = 0; index < size; index++) {
temp.coordinates[index] = coordinates[index] + other.coordinates[index];
}
return *(new Vector(temp));
}

最佳答案

您的 Vector::operator+ 至少有一个错误:

int* temp_c = new int;
...
temp_c[index] =

您在索引 temp_c 时它只分配了一个整数。所以你的循环正在踩踏其他内存,导致未定义的行为。

您还需要定义一个复制构造函数,以便您可以正确使用您的Vector 对象。编译器会生成一个默认的拷贝构造函数,但默认的拷贝构造函数一般不适合包含指针的对象。

这一行:

temp.coordinates = temp_c;

导致内存泄漏,因为它覆盖了先前分配的 temp.coordinates vector 。

更新 3:您的代码

return *(new Vector(temp));

虽然它看起来有效,但仍然是内存泄漏。您正在分配一个新的 Vector,然后编译器调用复制构造函数将其复制到函数的返回值中。没有人删除您刚创建的Vector 对象,因此存在内存泄漏。

解决方案是编写复制构造函数,而不是依赖编译器生成的默认复制构造函数。您问题的所有其他答案都说了同样的话。要求您为正确的程序执行此操作。

关于c++ - C++中析构函数的一堆不清楚的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9814716/

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