C++:抛出“std::bad_alloc”的实例后调用终止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15671255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: terminate called after throwing an instance of 'std::bad_alloc'
提问by wesrobin
I am implementing a doubly linked list class of sorts which stores 'buckets' (the nodes), which each contain a predefined number of characters. Each bucket stores a pointer to the next and previous bucket, and the list class (BucketString) stores a pointer to the head Bucket. I am compiling using g++ which throws the error
我正在实现一个双向链表类,它存储“存储桶”(节点),每个存储桶都包含预定义数量的字符。每个bucket存储一个指向下一个和上一个bucket的指针,列表类(BucketString)存储一个指向head Bucket的指针。我正在使用 g++ 进行编译,这会引发错误
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
make: *** [run] Aborted (core dumped)
whenever I run the code and add a string of characters to the list, using the following add method, which is contained within my bucket class, and is called from the list class's own methods whenever needed.
每当我运行代码并将一串字符添加到列表时,使用以下 add 方法,该方法包含在我的存储桶类中,并在需要时从列表类自己的方法中调用。
Code:
代码:
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;
}
When the error is thrown, the add method is being called from the 'list' class member method append, which works as follows:
当抛出错误时,add 方法正在从 'list' 类成员方法 append 中调用,其工作方式如下:
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
}
The header file for the bucket class is:
桶类的头文件是:
#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
采纳答案by wesrobin
This works: in my Bucket(std::size_t bucketSizeB)
constructor the initialiser for str
should change from str(new char[bucketSizeB]
to str(new char[bucketSizeB_])
(ie. use the argument passed to the cosntructor instead of using the bucketSizeB variable).
这有效:在我的Bucket(std::size_t bucketSizeB)
构造函数中,初始值设定项str
应该从str(new char[bucketSizeB]
to更改str(new char[bucketSizeB_])
(即使用传递给 cosntructor 的参数而不是使用 bucketSizeB 变量)。
回答by paulsm4
1) You can prevent termination with a try/catch block.
1) 您可以使用 try/catch 块防止终止。
2) It sounds like this is occurring when you execute the program. It also sounds like "make" executes the program automatically. Correct?
2)这听起来像是在您执行程序时发生的。听起来“make”也会自动执行程序。正确的?
3) If so, you want to look in a debugger and identify the exact line where it's crashing.
3) 如果是这样,您想查看调试器并确定它崩溃的确切行。
4) I suspect if you trace through the code you'll see that one or more of "diff", "bucketSizeB" and/or "filled" become very large (or negative). Which would be a bug :) Which you can easily fix - once you find it.
4)我怀疑如果您跟踪代码,您会看到“diff”、“bucketSizeB”和/或“filled”中的一个或多个变得非常大(或负)。这将是一个错误:) 您可以轻松修复 - 一旦找到它。
5) Here's are good tutorials on GDB, if that happens to be a convenient debugger for you:
5) 这里有关于 GDB 的很好的教程,如果它恰好是你的一个方便的调试器:
http://www.cs.cmu.edu/~gilpin/tutorial/
http://www.cs.cmu.edu/~gilpin/tutorial/