c++bad-alloc

bad alloc c++ loading file in matrix


I try to load a large file (20gb) and load it into a matrix. However I get a bad_alloc error when it tries to load the file in the matrix. My code is working on Mac but doesn't on Linux.

Here is my code:

std::ifstream ifs(filename, std::ifstream::binary);

loadModel(ifs);

void loadModel(std::istream& in) {
     input_ = std::make_shared<Matrix>();
     input_->load(in); // bad_alloc
}

Solution

  • bad alloc means an error during memory allocation. Probably your matrix does not fit into operating memory available.

    You can check available memory with free command

    $ free
                  total        used        free      shared  buff/cache   available
    Mem:       32780268     2055964    29109172      193300     1615132    30106808
    Swap:        999420           0      999420
    

    In this output, it tells that 29GB available.