Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:在抛出';std::bad#u alloc';_C++_Runtime Error - Fatal编程技术网

C++ C++;:在抛出';std::bad#u alloc';

C++ C++;:在抛出';std::bad#u alloc';,c++,runtime-error,C++,Runtime Error,我正在实现一个双链表排序类,它存储“bucket”(节点),每个bucket都包含预定义数量的字符。每个bucket存储一个指向下一个和上一个bucket的指针,list类(BucketString)存储一个指向头bucket的指针。我正在使用抛出错误的g++进行编译 terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc make: *** [run] Aborted (c

我正在实现一个双链表排序类,它存储“bucket”(节点),每个bucket都包含预定义数量的字符。每个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类中,并在需要时从列表类自己的方法调用

代码:

bucket类的头文件是:

#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
#包括
#包括
#包括
#ifndef BUCKET_H_
#定义BUCKET_H_
命名空间RBNWES001
{
类桶
{
公众:
//特殊成员和重载构造函数
桶(空);
铲斗(标准:尺寸t);
桶(const Bucket&);
~Bucket();
//复制作业不包括在内,因为它不需要,我是唯一一个使用此代码的人!:)
//添加方法
voidadd(std::string);
整数填充;
char*str;
桶*下一个;
桶*prev;
标准:尺寸为0.5毫米;
};
}
#恩迪夫
1)您可以使用try/catch块阻止终止

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

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

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

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


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

欢迎使用SO,但我要问您:您尝试了什么?是否添加了打印语句以查看错误的来源?是否使用了调试器?(不,您显示的代码还不够)我已经添加了上述打印语句,这就是我如何知道调用此方法时抛出它的原因。奇怪的是,该方法似乎在添加的前70个左右的字符中正常工作,但随后消息被抛出。您还想要什么?不幸的是,“list”类相当长,我将在“list”类中包含该方法(称为BucketString)调用此方法,如果有帮助的话。当内存不足时,运算符new会抛出bad_alloc。由于add使用递归,可能您是在内存不足的情况下进行递归。请在
next=new Bucket(bucketSizeB)之前添加print语句
并打印
bucketSizeB
,看看它是否变得太大。对不起,我认为现在需要添加更多的代码。当然不像我想的那样是局部错误。但这似乎只是内存问题,所以我会打印出大量内容,看看它是否发现了问题。谢谢!1)隐藏错误并使事情变得更糟,它不会解决问题。2)无关。3) 应该是(已经是)注释。因为成员是按声明顺序初始化的。如果您的编译器设置足够高,您应该得到一个警告。@GManNickG,我不明白为什么您会因为他的特定错误得到警告。
bucketSizeB
变量先前定义为全局变量。。。
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