C ++:抛出'std :: bad_alloc'实例后调用terminate

时间:2013-03-27 23:05:12

标签: c++ runtime-error

我正在实现一个双重链接列表类,它存储“桶”(节点),每个包含预定义数量的字符。每个存储桶存储指向下一个和前一个存储桶的指针,列表类(BucketString)存储指向头部存储桶的指针。我正在编译使用g ++抛出错误

terminate called after throwing an instance of 'std::bad_alloc'
  what(): std::bad_alloc
make: *** [run] Aborted (core dumped)
每当我运行代码并在列表中添加一个字符串时,使用以下add方法(包含在我的bucket类中),并在需要时从列表类自己的方法中调用。

代码:

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

当抛出错误时,将从'list'类成员方法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

2 个答案:

答案 0 :(得分:5)

1)您可以使用try / catch块阻止终止。

2)听起来这是在执行程序时发生的。它听起来像“make”自动执行程序。正确的吗?

3)如果是这样,你想查看一个调试器,并确定它崩溃的确切行。

4)我怀疑如果你追踪代码,你会看到“diff”,“bucketSizeB”和/或“filled”中的一个或多个变得非常大(或者是负数)。这将是一个错误:)你可以轻松修复 - 一旦你找到它。

5)以下是关于GDB的好教程,如果它恰好是一个方便的调试器:

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

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

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

答案 1 :(得分:5)

这样做:在我的Bucket(std::size_t bucketSizeB)构造函数中,str的初始化程序应该从str(new char[bucketSizeB]更改为str(new char[bucketSizeB_])(即使用传递给cosntructor的参数而不是使用bucketSizeB变量)。