C++:在抛出"std::bad_alloc"实例后终止调用
C++: terminate called after throwing an instance of 'std::bad_alloc'
我正在实现一个双重链表分类类,它存储"桶"(节点),每个桶包含预定义数量的字符。每个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变量)
相关文章:
- 从C++实例化QML
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 建议在运行时将带有类实例的列表从c++导入qml
- 约束和显式模板实例化
- 通过实例理解std::move及其目的
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 正在生成未知类实例
- 从DLL中删除类的实例
- 在std::vector上存储带有模板的类实例
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc