1

メモリ リーク、8 バイト ブロックが 2 つ、5 バイト ブロックが 1 つあるというエラー メッセージがコンパイラから何度も表示されます。*nameデストラクタですでに配列を削除しています。デストラクタはスコープをどのように処理しますか? fruitメインが終了すると、オブジェクトが範囲外になると削除されるという印象を受けました。

#include "Fruit.h"
#include "LeakWatcher.h"

using namespace std;
Fruit::Fruit(const Fruit &temp )
{
    name = temp.name;
    for(int i = 0; i < CODE_LEN - 1; i++)
    {
        code[i] = temp.code[i];
    }
}
void  Fruit::operator=(const Fruit &tempFruit)
{
    name = tempFruit.name;
    for(int i = 0; i < CODE_LEN; i++)
    {
        code[i] = tempFruit.code[i];
    }

}
Fruit::~Fruit()
{
    delete[] name;

}
bool  Fruit::operator==(const Fruit &tempFruit)
{
    int i = 0;
    while(name[i] != NULL && tempFruit.name[i] != NULL)  
    {
        if(name[i] != tempFruit.name[i])
            return false;
        i++;
    }
    if(name[i] != NULL || tempFruit.name[i] != NULL)
        return false;
    return true;
}
bool  Fruit::operator<(const Fruit &tempFruit)
{
    int i = 0;
    while(name[i] != NULL && tempFruit.name[i] != NULL)  
    {
        if((int)name[i] < (int)tempFruit.name[i])
            return true;
        else if((int)name[i] > (int)tempFruit.name[i])
            return false;
        i++;
    }
    if(name[i] == NULL && tempFruit.name[i] != NULL)
        return true;
    else
        return false;
}
std::ostream & operator<<(std::ostream &os, const Fruit *printFruit)
{
    os << setiosflags(ios::left) << setw(MAX_NAME_LEN) << printFruit->name << " ";
    for(int i = 0; i < CODE_LEN; i++)
    {
        os << printFruit->code[i];
    }
    os << endl;
    return os;
}

std::istream & operator>>(std::istream &is, Fruit *readFruit)
{

    string tempString;
    is >> tempString;
    int size = tempString.length();
    readFruit->name = new char[tempString.length()];
    for(int i = 0; i <= (int)tempString.length(); i++)
    {
        readFruit->name[i] = tempString[i];
    }
    readFruit->name[(int)tempString.length()] = '\0';
    for(int i =0; i < CODE_LEN; i++)
    {
        is >> readFruit->code[i];
    }
    return is;
}
void stuff()
{

}
void main()
{
    _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE ); 
   _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT ); 
   _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE ); 
   _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT ); 
   _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE ); 
   _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
    Fruit *fruit = new Fruit();
    Fruit *fruit1 = new Fruit();
    cin >> fruit;
    *fruit1 = *fruit;
    cout << fruit << fruit1;
    _CrtDumpMemoryLeaks();

}

H

#ifndef _FRUIT_H
#define _FRUIT_H
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
enum { CODE_LEN = 4 }; 
enum { MAX_NAME_LEN = 30 };
class Fruit
{
private:
    char *name;
    char code[CODE_LEN];
public:
    Fruit(const Fruit &temp);
    Fruit(){name = NULL;};
    bool operator<(const Fruit &other);
   friend std::ostream & operator<<(std::ostream &os, const Fruit *printFruit);
    bool operator==(const Fruit &other);
   bool operator!=(const Fruit &other){return!(*this==other);};
    friend std::istream & operator>>(std::istream& is, Fruit *readFruit);
    void  Fruit::operator=(const Fruit &tempFruit);
    ~Fruit();
};
#endif
4

3 に答える 3

2

フルーツ オブジェクトは、スコープ外に出ても削除されません。代わりに、それらは漏れます。これはプログラムが行う最後のことであるため、実際の結果はなく、プログラムが終了すると OS はメモリを再利用します。とはいえ、お漏らしです。

代わりに、スタック上にそれらを作成しないのはなぜですか?

于 2013-10-08T05:20:26.337 に答える
1

コメントで既に行った、例の特定のメモリの問題に対処するのではなく、 and を使用してそれらを完全に回避しstd::stringますstd::array。あなたのコードは要約すると

#include <iostream>
#include <string>
#include <array>
enum { CODE_LEN = 4 }; 
class Fruit
{
private:
    std::string name;
    std::array<char,CODE_LEN> code;
public:
   bool operator<(const Fruit &other);
   bool operator==(const Fruit& other);
   bool operator!=(const Fruit& other) {return!(*this==other);}
   friend std::ostream& operator<<(std::ostream &os, const Fruit& f);
   friend std::istream& operator>>(std::istream& is, Fruit& f);
};

ユーザー定義のコンストラクタ、代入演算子、またはデストラクタがないことに注意してください。一部の演算子も大幅に簡素化できます。

bool Fruit::operator==(const Fruit& rhs)
{
  return code == rhs.code;
}

bool Fruit::operator<(const Fruit& rhs)
{
  return name < rhs.name;
}
于 2013-10-08T05:33:37.117 に答える