标签: bad-alloc

为什么会出现bad_alloc错误?

目前我正在尝试为Student设置一个成员函数,该函数从cin读取一个字符串,用作该函数的参数,然后用该数据创建一个Student对象.但是,它是否给我一个bad_alloc错误.我知道函数正在获取字符串,但是在创建新对象后它会出现此错误.

错误:

./a.out

Please insert name for student:
Bob_Russel
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted
Run Code Online (Sandbox Code Playgroud)

构造函数:

Student::Student(string tname){

  name = tname;

}
Run Code Online (Sandbox Code Playgroud)

功能:

Student Student::readStudent(istream &i){

  Student *stud;

  string y;

  i >> y;

  stud = new Student(y);

  return  *stud;

}
Run Code Online (Sandbox Code Playgroud)

testStudent.cpp:

 #include "Student.h"

int main(){

  Student *stud3;
  cout << "\nPlease insert name for student:\n";

  stud3->readStudent(cin);


return 0;

}
Run Code Online (Sandbox Code Playgroud)

c++ istream bad-alloc

1
推荐指数
1
解决办法
3942
查看次数

bad_alloc标识符无法识别?

我收到编译器错误:

错误C2061:语法错误:标识符'bad_alloc'

我以前在其他项目中使用过以下代码块,在try块中有不同的内存分配,没有问题.我希望有人可以向我解释为什么bad_alloc不被VS10识别,尽管它没有在其他程序中引起同样的问题?可能性是我错过了一些轻微的语法问题,但我花了好几个小时试图解决它,此时我觉得我可能对错误视而不见.谢谢您的帮助!

try
{
    node* tbr = new node();
    return tbr;
} // End try allocation

catch(bad_alloc)
{
     throw OutOfMemoryException();
} // End catch(bad_alloc)
Run Code Online (Sandbox Code Playgroud)

c++ syntax compiler-errors visual-studio-2010 bad-alloc

1
推荐指数
1
解决办法
2073
查看次数

C ++向量std :: bad_alloc错误

我正在尝试在c ++中实现后缀树,同时将节点添加到向量列表中,在向树中添加第三个元素后,它会引发std :: bad_alloc。我不知道为什么它在第三次之后发生,您能帮我解决这个bad_alloc错误吗?

这是我的代码:

suffix_tree.cpp

#include <iostream>
#include <fstream>
#include <cmath>
#include <sstream>
#include <string>
#include <cstring>
#include "node.h"

using namespace std;

Node build_suffix_tree(string text){
    Node root = Node();
    int n = text.length();
    int count;
    Node * currentNode = &root;
    Node tmpNode;
    string suffix;
    int suffixLen;


    for(int i=0; i<n; i++){
        suffix = text.substr(i,n);
        suffixLen = suffix.length();
        count = 1;
        currentNode = &root;

        while(count <= suffixLen){
            cout << suffix << endl;
            int pos = -1;


            // bad_alloc occurs here
            (*currentNode).addFils(Node(suffix[0], …
Run Code Online (Sandbox Code Playgroud)

c++ vector bad-alloc

1
推荐指数
1
解决办法
1万
查看次数

what():std :: bad_alloc - 我内存不足吗?

我的数据集:960个维度中的500,000个点.文件大小为1.9 GB(1,922,000,000字节).

该代码适用于较小的数据集,但为此,它每次都会在同一点崩溃.这是一个最小的例子:

#include <iostream>
#include <vector>

template<typename T>
class Division_Euclidean_space {
 public:
  /**
   * The data type.
   */
  typedef T FT;

  /**
   * Constructor, which
   * sets 'N' and 'D' to zero.
   */
  Division_Euclidean_space()
      : N(0),
        D(0) {

  }

  /**
   * @param n - size of data
   */
  void setSize(size_t& n) {
    N = n;
  }

  /**
   * @param n - size of data
   */
  void setSize(int n) {
    N = n;
  }

  /**
   * Get the number of …
Run Code Online (Sandbox Code Playgroud)

c++ memory bad-alloc

1
推荐指数
1
解决办法
3816
查看次数

Win32上的内存不足(?)问题(与Linux相比)

我有以下问题:

在Windows机器上运行的程序(32位,3.1Gb内存,VC++ 2008和mingw编译代码)都会失败并bad_alloc抛出异常(在分配大约1.2Gb之后;在尝试分配900万双倍的向量时抛出异常,即大约75Mb)仍然有足够的RAM(至少根据任务经理).

在Linux机器(32位,4Gb内存; 32位,2Gb内存)上运行的相同程序运行良好,峰值内存使用量约为1.6Gb.有趣的是,由mingw生成的win32代码在wine下的4Gb linux机器上运行也失败了bad_alloc,虽然在不同的(稍后)位置然后在windows下运行时...

有什么可能的问题?

  • 堆积碎片?(我怎么知道?怎么解决这个问题?)
  • 堆腐败?(我运行了启用了pageheap.exe的代码,没有报告错误;实现了带边界检查的矢量访问 - 再次没有错误;代码基本上没有指针,只使用了std::vectors和std::lists.在Valgrind下运行程序( memcheck)消耗太多内存并提前结束,但没有发现任何错误)
  • 内存不足???(应该有足够的内存)

而且,在Linux版本工作时(甚至在内存较少的机器上)Windows版本失败的原因可能是什么?(另请注意,如果可以产生任何影响,/ LARGEADDRESSAWARE链接器标志将与VC + 2008一起使用)

任何想法将非常感激,我在我的智慧结束这... :-(

c++ linux winapi bad-alloc

0
推荐指数
1
解决办法
2249
查看次数

为什么600MB后动态内存分配失败?

我使用三维char数组实现了一个bloom过滤器(位表),它运行良好,直到达到无法再分配内存并给出bad_alloc消息的程度.在分配600MB后,它在下一个扩展请求中给出了这个错误.

布隆过滤器(阵列)预计会增长到8到10GB.

这是我用来分配(扩展)位表的代码.

unsigned char ***bit_table_=0;
unsigned int ROWS_old=5;
unsigned int EXPND_SIZE=5;


void expand_bit_table()
     {
         FILE *temp;
         temp=fopen("chunk_temp","w+b");
         //copy old content
         for(int i=0;i<ROWS_old;++i)
             for(int j=0;j<ROWS;++j)
                 fwrite(bit_table_[i][j],COLUMNS,1,temp);
         fclose(temp);
         //delete old table
         chunk_delete_bit_table();
         //create expanded bit table ==> add EXP_SIZE more rows
         bit_table_=new unsigned char**[ROWS_old+EXPND_SIZE];
         for(int i=0;i<ROWS_old+EXPND_SIZE;++i)
            {
                bit_table_[i]=new unsigned char*[ROWS];
                for(int k=0;k<ROWS;++k)
                    bit_table_[i][k]=new unsigned char[COLUMNS];
            }
         //copy back old content

          temp=fopen("chunk_temp","r+b");
         for(int i=0;i<ROWS_old;++i)
         {
            fread(bit_table_[i],COLUMNS*ROWS,1,temp);
         }
          fclose(temp);
         //set remaining content of bit_table_to 0
         for(int i=ROWS_old;i<ROWS_old+EXPND_SIZE;++i)
             for(int j=0;j<ROWS;++j)
                 for(int k=0;k<COLUMNS;++k) …
Run Code Online (Sandbox Code Playgroud)

c++ bitarray bad-alloc dynamic-arrays

0
推荐指数
1
解决办法
1742
查看次数

使用c ++中的向量的bad_alloc异常

我用c ++编程不是很专业,所以我的问题可能看起来有点愚蠢,但我无法理解我做错了什么.

我想用指令分配一个向量

vector <short int> myvec(rowotot * coltot)
Run Code Online (Sandbox Code Playgroud)

为了表示矩阵.

在继续之前,我想验证我是否有足够的空间来分配我的向量.如果我使用

try { 
    vector <short int> myvec(rowtot * coltot);
}
catch (bad_alloc& ba) {
    cert << "ERROR: ";
    cerr << ba.what() << endl;
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试修改向量的元素,我不能因为

myvec未在此范围内声明

在保存向量中的任何值之前,如何检查内存?我希望我的问题很明确.

c++ vector bad-alloc

0
推荐指数
1
解决办法
808
查看次数

实现向量调整大小功能时出现bad_alloc错误

我试图在C++中实现向量调整大小功能.我想我处理了每种情况但仍然出现bad_alloc错误.此调整大小实现中的三种情况:1)当new_size小于old_size时(在我的代码中,大小); 2)当new_size大于但小于容量时; 3)情况3:当新的_size大于容量时

void Vector::resize(int new_size)
{
    //if the new_size is smaller, make the size smaller, don't need to worry about memory
    //the content is reduced to its first n element
    //remove those beyond and destroy them

    if(new_size < size){

        for(int i = size-1; i > new_size; i--){
            erase(i);
        }
        size = new_size;
    }

    //if the new_size is bigger
    //case 1: new_size is smaller than capacity
    //inserting at the end of as many elements as needed
    if(new_size > size && …
Run Code Online (Sandbox Code Playgroud)

c++ pointers resize vector bad-alloc

-1
推荐指数
1
解决办法
523
查看次数