bad-alloc

How to deal with bad_alloc in C++?

∥☆過路亽.° 提交于 2019-12-17 08:18:27
问题 There is a method called foo that sometimes returns the following error: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Abort Is there a way that I can use a try - catch block to stop this error from terminating my program (all I want to do is return -1 )? If so, what is the syntax for it? How else can I deal with bad_alloc in C++? 回答1: You can catch it like any other exception: try { foo(); } catch (const std::bad_alloc&) { return -1; } Quite what you

std::bad_alloc after replacing boost:python function wrapper with Python/C API

£可爱£侵袭症+ 提交于 2019-12-12 14:10:15
问题 I had a function in C which I used to extend python, previously using the BOOST_MODULE function to accomplish this. This error came up when transitioning to the python-C API. I am certain that the run_mymodule function runs fine without this wrapper. static PyObject * wrap_run_mymodule(PyObject *, PyObject *args) { char *file1, *file2, *file3; PyObject *tmpp; if(!PyArg_ParseTuple(args, "sssO", &file1, &file2, &file3, &tmpp)) return NULL; return Py_BuildValue("i", run_mymodule(file1, file2,

problems with operator new when allocating arrays

爱⌒轻易说出口 提交于 2019-12-12 03:41:33
问题 I'm having prblems with my C++/openGL program. at some point of code, like these(it's a constructor): MyObject(MyMesh * m, MyTexture* t, float *c=NULL, float *sr=NULL, int sh=100){ texture=t; mesh=m; subObjects=NULL; texCoords=NULL; if (texture!=NULL){ texCoords=new float[mesh->numSurfacePoints*2]; the new throws an std::bad_alloc exception. it's the same at another place. is it possible, that i ran out of memory? i dont think so, so if you could help me, i would be glad! bye! 回答1: You should

Memory Error in Visual Studio, but plenty of memory available

蹲街弑〆低调 提交于 2019-12-11 20:39:38
问题 This line of code produces the following error rs[se_idx][ev_idx][re_idx].trs = new re_class[report_size]; std::bad_alloc at memory location 0x0037c29c I think this is related to 'not enough memory'. When I decrease the amount being allocated, it runs fine. I have plenty of memory (16 GB) on the machine and a resource monitor shows only a tiny fraction of it is being used by visual studio. I added the compiler options /F 4000000000 and /LARGEADDRESSAWARE , but still getting the error. How can

Bad_alloc exception when using new for a struct c++

久未见 提交于 2019-12-11 07:26:02
问题 I am writing a query processor which allocates large amounts of memory and tries to find matching documents. Whenever I find a match, I create a structure to hold two variables describing the document and add it to a priority queue. Since there is no way of knowing how many times I will do this, I tried creating my structs dynamically using new. When I pop a struct off the priority queue, the queue (STL priority queue implementation) is supposed to call the object's destructor. My struct code

C++ vector std::bad_alloc error

这一生的挚爱 提交于 2019-12-11 05:48:56
问题 I'm trying to implement a suffix tree in c++ while adding nodes to my vector list, it throws std::bad_alloc after adding a third element into the tree. I don't know why it occurs after the third time, Could you help me resolving this bad_alloc error ? Here's my code : 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

Operator new and bad_alloc on linux

笑着哭i 提交于 2019-12-10 20:12:36
问题 On Linux, malloc doesn't necessarily return a null pointer if you're out of memory. You might get back a pointer and then have the OOM killer start eating processes if you're really out of memory. Is the same true for c++'s operator new or will you get the bad_alloc exception? 回答1: The same is true for operator new, alas :^( 回答2: It's a kernel function rather than a language function - and you can control it with the vm.overcommit_memory and vm.overcommit_ratio sysctls. They're visible in the

function doesn't throw bad_alloc exception

落花浮王杯 提交于 2019-12-10 14:47:21
问题 I'm trying to do an exercise form Stroustrup's C++PL4 book. The task is: Allocate so much memory using new that bad_alloc is thrown. Report how much memory was allocated and how much time it took. Do this twice: once not writing to the allocated memory and once writing to each element. The following code doesn't throw a std::bad_alloc exception. After I execute the program I get message "Killed" in terminal. Also. The following code exits in ~4 seconds. But when I uncomment memory usage

cuda/thrust: Trying to sort_by_key 2.8GB of data in 6GB of GPU RAM throws bad_alloc

独自空忆成欢 提交于 2019-12-10 09:51:46
问题 I have just started using thrust and one of the biggest issues I have so far is that there seems to be no documentation as to how much memory operations require. So I am not sure why the code below is throwing bad_alloc when trying to sort (before the sorting I still have >50% of GPU memory available, and I have 70GB of RAM available on the CPU)--can anyone shed some light on this? #include <thrust/device_vector.h> #include <thrust/sort.h> #include <thrust/random.h> void initialize_data

Strange std::bad_alloc

不打扰是莪最后的温柔 提交于 2019-12-10 02:35:04
问题 As far as I know, there are three reasons why a std::bad_alloc can be thrown: The process requests more memory than what can be served The address space is too fragmented to serve a request for a large chunk of contiguous memory The heap management datastructure is corrupted We have code which runs into a std::bad_alloc, but none of the above reasons seem to apply. The datastructure is a graph stored as a std::list of vertices, where each vertex stores again a std::list of the edges of which