C++:在抛出"std::bad_alloc"实例后终止调用

C++: terminate called after throwing an instance of 'std::bad_alloc'

本文关键字:实例 alloc 终止 调用 std C++ bad      更新时间:2023-10-16

我正在实现一个双重链表分类类,它存储"桶"(节点),每个桶包含预定义数量的字符。每个bucket存储一个指向下一个和上一个bucket的指针,list类(BucketString)存储一个指向头bucket的指针。我正在使用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

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

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

3)如果是这样,您需要查看调试器并确定它崩溃的确切行。

4)我怀疑如果您跟踪代码,您将看到一个或多个"diff","bucketSizeB"和/或"fill"变得非常大(或为负)。这将是一个错误:)你可以很容易地修复-一旦你找到它。

这里有一些关于GDB的很好的教程,如果这恰好是一个方便的调试器: http://dirac.org/linux/gdb/

http://www.cs.cmu.edu/吉尔平著/教程/

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

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