c++ - 我释放内存两次 - C++

标签 c++

我已经完成了这个程序,我在其中检查我的“日期”类是否正确。问题是当我运行测试程序时,它返回了以下错误:

  • `./bin/test' 中的错误:双重释放或损坏(fasttop):0x00000000019c07c0 *

这个类的工作是读取并存储一个“日期”(一年)和一些事件(分配在一个字符串数组中)。例如,此类的对象将是:1998 EVENT1 EVENT2 EVENT3。

运营商>>阅读下一个格式:1908#Fantasmagorie#驯悍记#盗贼之手#刺杀吉斯公爵#海滨之旅

嗯,我的问题是我删除了一些指针两次或释放了一些内存两次,我已经尝试了很多东西但我不知道如何修复它(正如你在我的代码中看到的那样,我有当我删除它们时,已经尝试将所有指针设置为 0。): 日期类.h

#ifndef _date_HISTORICA_
#define _date_HISTORICA_

#include <iostream>
#include <string>
#include <cassert>

using namespace std;

class date{
private:
    int year;
    int eventsNum;
    int reserved;
    string * str;
    void resize(int r);
public:
    date();
    //date(int a, string *s, int n);
    date(const date& d);
    ~date();
    int getAge();
    void addEvent(string& s);
    friend ostream& operator<<(ostream& os, const date& d);
    friend istream& operator>>(istream& is, date& d);
};



#endif

日期类代码:

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<date.h>

using namespace std;

void date::resize(int r)
{
    assert(r>=0);
    if(r!=this->reserved)
    {
     if(r!=0)
     {
        string * aux = new string[r];
        if(this->reserved>0)
        {
            int min=this->reserved<r?this->reserved:r;
            for(int i=0; i<min; i++)
                aux[i]=this->str[i];
            delete[] this->str;
            this->str=NULL;
        }
        this->str=aux;
        this->reserved=r;
        if(this->reserved<this->eventsNum)
            this->eventsNum=this->reserved;
     } else
     {
        if(this->reserved>0)
        {
            delete[] this->str;
            this->str=NULL;
        }
        this->year=0;
        this->eventsNum=0;
        this->reserved=0;
     }
    }
}

date::date() : year(0), eventsNum(0), reserved(0), str(0){}

date::date(const date& d)
{
    this->year=d.year;
    this->eventsNum=d.eventsNum;
    this->reserved=d.reserved;
    this->str=new string[this->reserved];
    for(int i=0; i<this->eventsNum; i++)
        this->str[i]=d.str[i];
}

date::~date()
{
    this->year=0;
    this->eventsNum=0;
    this->reserved=0;
    if(this->str)
    delete[] this->str;
    this->str=NULL;
}

int date::getAge(){return this->year;}

ostream& operator<<(ostream& os, const date& d)
{
    os << d.year;
    for(int i=0; i<d.eventsNum; i++)
        os << '#' << d.str[i];
    os << endl;
    return os;
}

void date::addEvent(string& s){
    if (this->eventsNum == this->reserved){
       if (this->eventsNum==0)
          resize(1);
       else
          resize(2*this->reserved);
    }
    this->str[eventsNum]=s;
    eventsNum++;
 }

istream& operator>>(istream& is, date& d)
{
    string line; char c;
    is >> d.year >> c;
    getline(is, line);

    int n=1;
    for(int i=0; i<line.length(); i++)
        if(line[i]=='#')
            n++;

    d.eventsNum=n;
    d.reserved=d.eventsNum;
    delete[] d.str;
    d.str=NULL;
    d.str=new string[n];

    stringstream ss(line);

    for(int i=0; i<n; i++)
        getline(ss, d.str[i], '#');
    return is;
}

测试程序类:

#include<iostream>
#include<fstream>
#include<cronologia.h>
#include<date.h>

using namespace std;

int main(int argc, char * argv[]){
    cout <<  "STATE: IN PROGRESS" << endl;
    cout << "TEST: (2)" << endl;
    date d;

    ifstream f("./data/name.txt");

    while(f >> d)
    {
        cout << d;
    }
    date d1;
    cin >> d1;
    d=d1;
    cout << d << endl;


}

示例文件(应该按日期类阅读):

1900#Sherlock Holmes Baffled#The Enchanted Drawing
1901#Star Theatre#Scrooge, or, Marley's Ghost
1902#A Trip to the Moon
1903#The Great Train Robbery#Life of an American Fireman
1904#The Impossible Voyage
1905#Adventures of Sherlock Holmes; or, Held for Ransom
1906#The Story of the Kelly Gang#Humorous Phases of Funny Faces#Dream of a Rarebit Fiend
1907#Ben Hur#L'Enfant prodigue
1908#Fantasmagorie#The Taming of the Shrew#The Thieving Hand#The Assassination of the Duke of Guise#A Visit to the Seaside

我对我的英语感到抱歉!!! :,(

最佳答案

由于您的代码中没有赋值重载,所以在行中

    d=d1;

d1 的所有成员将按值复制到新对象d。因此,将有对象 date 的两个拷贝,它们在其成员 str 中具有相同的引用值。这两个最终将超出范围,并且都将被破坏。第一个将释放分配的内存,而另一个将尝试释放相同的引用,这就是您收到错误的原因。

关于c++ - 我释放内存两次 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46993324/

相关文章:

c++ - 动态创建 std::vector//创建指向 vector 的指针

c++ - OpenGL在不同的VBO中绘制具有不同颜色、顶点位置和顶点颜色的点?

c++ - 为什么我可以索引到包含超出范围的数字的 3-D 数组并仍然访问数组中的最后一个数字?

c++ - 将 filesystem::path 元素附加到另一个路径的最佳方法是什么?

c++ - 将转换范围缩小到更大的类型(然后再返回)

C++ : gcc compiler warning for large stack allocation

c++ - BOOST 多重指数

c++ - 使 CTime 和 CTimeSpan 忽略夏令时

c++ - 枚举值与枚举名称冲突

c++ - 将笛卡尔宽度和高度转换为等距