9

それぞれが定義済みの文字数を含む「バケット」(ノード) を格納する種類の二重リンク リスト クラスを実装しています。各バケットには前後のバケットへのポインタが格納され、リスト クラス (BucketString) には先頭のバケットへのポインタが格納されます。エラーをスローする g++ を使用してコンパイルしています

terminate called after throwing an instance of 'std::bad_alloc'
  what(): std::bad_alloc
make: *** [run] Aborted (core dumped)

コードを実行し、リストに文字列を追加するたびに、次の add メソッドを使用します。このメソッドはバケット クラスに含まれており、必要に応じてリスト クラスの独自のメソッドから呼び出されます。

コード:

std::size_t bucketSizeB;
int filled;
char* str;
Bucket* next;
Bucket* prev;

Bucket::Bucket() : bucketSizeB(7), str(new char[7]), next(NULL), prev(NULL), filled(0)
{}

Bucket::Bucket(std::size_t bucketSizeB_) : bucketSizeB(bucketSizeB_), str(new char[bucketSizeB]), next(NULL), prev (NULL), filled(0)
{}

Bucket::Bucket(const Bucket& rhs) : bucketSizeB(rhs.bucketSizeB), next(rhs.next), prev(rhs.prev), filled(rhs.filled)
{
    for (int i = 0 ; i < (int) bucketSizeB ; i++)
    {
        str[i] = rhs.str[i];
    }
}

void Bucket::add(std::string line)
{

    int diff = bucketSizeB - filled;    //if the bucket is already partially filled


    std::string tmp = line.substr(0, diff);

    for (std::size_t i = 0 ; i < tmp.length() ; i++)
    {

        str[filled] = line[i];
        ++filled;
    }

    if (line.length() > bucketSizeB)
    {

        next = new Bucket(bucketSizeB);

        next->prev = this;
        next->add(line.substr(diff, line.length()-diff));
    }
}
Bucket::~Bucket()
{
    if (prev)
    {
        if (next)
        {
            prev->next = next;
        }
        else
        {
            prev->next = NULL;
        }
    }
    if (next)
    {
        if (prev)
        {
            next->prev = prev;
        }
        else
        {
            next->prev = NULL;
        }
    }
    delete [] Bucket::str;
}

エラーがスローされると、「リスト」クラス メンバー メソッド append から add メソッドが呼び出されます。これは次のように機能します。

void BucketString::append (std::string& line)
{
    length += line.length();    //Just a way to store the length of the string stored in this BucketString object

    if (!head)   //If the head node pointer is currently null, create a new head pointer
    {

        head = new Bucket(bucketSize);
    }

    Bucket* tmp = head;

    while (tmp->next)   //Finds the tail node
    {
        tmp = tmp->next;
    }
    tmp->add(line);   //Calls the Bucket add function on the tail node
}

バケット クラスのヘッダー ファイルは次のとおりです。

#include <cstddef>
#include <string>
#include <iostream>

#ifndef BUCKET_H_
#define BUCKET_H_

namespace RBNWES001
{
class Bucket
{

    public:
        //Special members and overloaded constructor
        Bucket(void);
        Bucket(std::size_t);
        Bucket(const Bucket&);
        ~Bucket();
        //Copy Assignment not included because it's not needed, I'm the only one who is gonna use this code! :)

        //Add method
        void add(std::string);

        int filled;
        char* str;
        Bucket* next;
        Bucket* prev;
        std::size_t bucketSizeB;
};
}

#endif
4

2 に答える 2

5

これは機能します: 私のBucket(std::size_t bucketSizeB)コンストラクターでは、イニシャライザーの for をto にstr変更する必要があります (つまり、bucketSizeB 変数を使用する代わりに、cosntructor に渡された引数を使用します)。str(new char[bucketSizeB]str(new char[bucketSizeB_])

于 2013-03-28T01:44:06.287 に答える
5

1) try/catch ブロックで終了を防ぐことができます。

2)プログラムを実行すると、これが発生しているように聞こえます。また、「make」がプログラムを自動的に実行するようにも聞こえます。正しい?

3) その場合は、デバッガーを調べて、クラッシュしている正確な行を特定します。

4) コードをトレースすると、"diff"、"bucketSizeB"、および/または "filled" の 1 つまたは複数が非常に大きくなる (または負になる) ことがわかると思います。これはバグです:)簡単に修正できます-見つけたら。

5) 便利なデバッガーである場合は、GDB に関する優れたチュートリアルを次に示します。

http://dirac.org/linux/gdb/

http://www.cs.cmu.edu/~gilpin/tutorial/

http://www.cprogramming.com/gdbtutorial.html

于 2013-03-27T23:10:32.853 に答える