Questions tagged [bad-alloc]

std::bad_alloc is the type of exception thrown when memory allocation fails in a C++ program

In C++ when operator new or std::allocator cannot allocate memory an exception of type std::bad_alloc will be thrown.

265 questions
1
vote
3 answers

Why does this give a bad_alloc error?

Currently I'm trying to set up a member function for Student that reads a string from cin, is used as an argument for this function and then creates a Student object with the data. However, is it giving me a bad_alloc error. I know the function is…
Mark Berube
  • 206
  • 3
  • 11
1
vote
1 answer

std: bad_alloc runtime error in javascript code

I was solving a problem in leetcode. The problem was find the length of longest substring. I solved the problem and the code is running perfectly on local machine and when I run the code on leetcode playground. But when I submit the code it shows…
user14750748
1
vote
1 answer

What is the reason for std::bad_alloc in this function?

I have written below C++ function which loops through an integer vector. With each pass it subtracts smallest number from all its numbers. It is supposed to return the number of non zero elements at each pass(this is stored in the vector result and…
Salvankar
  • 91
  • 6
1
vote
1 answer

How to fix C++ error: terminate called after throwing an instance of 'std::bad_alloc'. what(): std::bad_alloc

My apologies, I know this type of question already has an answer over here but I couldn't figure out how to use it for my code. I wrote a program for a problem-solving contest that accepts an array and tries to maximize the value of…
Mohit Singh
  • 143
  • 11
1
vote
1 answer

Bad alloc error when creating a 4GB std::string

I am using a multiparser to send http POST request and I need to send a big file (4Go). However when I test my code it give me a bad_alloc exception and crashes. I tried it with smaller files and it worked for files smaller than 500Mo, but the…
aabassi
  • 19
  • 1
1
vote
1 answer

Eigen allocation segmentation fault (Open3D)

I'm trying to make some operations on Pointcloud but when calling VoxelDownSample function i'm getting Segmentation fault signal. I see that the problem is that program want to allocate some astronomic amount of data but i don't know where it come…
KonDziupla
  • 23
  • 3
1
vote
0 answers

Evaluation error: std::bad_alloc. :Error in extracting neighbors of geometric points from specified multi-ring buffers in R

I am executing the following code from https://gis.stackexchange.com/questions/357997/creating-multi-ring-buffers-around-points-in-a-single-layer-and-for-each-point-s. library(sf) library(dplyr) library(ggplot2) library(stringr) library(rgdal)…
Lomnewton
  • 35
  • 6
1
vote
1 answer

Debug Python/C++ program with bad_alloc

I have a Python program that interfaces with a PyBind11 C++ library. Somewhere in the library, something is throwing an std::bad_alloc. Python catches this and handily raises an exception: MemoryError: std::bad_alloc Running it all in GDB: gdb --ex…
Richard
  • 56,349
  • 34
  • 180
  • 251
1
vote
0 answers

What is the scope and usefulness of bad_alloc in Linux?

For a long time I though that C++ (STL) would throw a bad_alloc when there was no available memory. However, guided by some common knowledge I heard about Linux (such as "Linux doesn't really reserve memory until you use it"), I decided to test…
alfC
  • 14,261
  • 4
  • 67
  • 118
1
vote
1 answer

Creating pointer to read from .las binary file throws bad_alloc error

I am currently writing code to read in a .las (LIDAR point data) file. After reading in the file, I created a pointer to iterate through all the points. However, it gives me a bad_alloc error. The only thing I can think of is that header is not the…
p0ps1c1e
  • 176
  • 2
  • 2
  • 14
1
vote
1 answer

Do you always must check for bad_alloc whenever dynamically allocating memory?

If new cannot find enough memory, it throws an exception. Do I absolutely always need to check for that? I never did that and had no issues, but now I've read you should do that. Or only in certain cases? try { pPos = new…
Vegeta
  • 334
  • 2
  • 4
  • 12
1
vote
1 answer

Is accessing accessing a child attribute in the parent constructor UB when using CTRP?

I made a wandbox that captures my issue: https://wandbox.org/permlink/qAX6SL43BvERo32Z Basically, as the tittle suggests. I am using CRTP, and my base / parent class constructor calls a child method using the standard way in CTRP. BUT that child…
1
vote
1 answer

Getting Error : terminate called after throwing an instance of 'std::bad::alloc' what(): std::bad_alloc

Getting an error Getting Error : terminate called after throwing an instance of std::bad_alloc what(): std::bad_alloc #include #include using namespace std; int64_t fibonacci(int64_t n,int64_t m) { int64_t *fibarray =…
1
vote
0 answers

'std::bad_alloc' while reading lmdb in caffe

I have created an lmdb file that contains non-encoded 6-channel images. When I load it into a network in caffe, after the network is loaded, the system RAM usage (as seen using the 'top' command) is initially around 10%, but it goes on increasing,…
1
vote
2 answers

How to implement proper stack unwinding and raii when thrown a bad_alloc during construction

So I'm designing a class that's gonna be handling a bunch of memory and I want to make sure it unwinds properly if something goes wrong during memory allocation in it's constructor. Here's what I've got: class foo{ public: foo(): var1(nullptr),…
wbrege
  • 11
  • 1