Xara
Xara

Reputation: 9098

c++: what(): std::bad_alloc error

In my code below, I have a structure DoubleTableEntries which consists of an int and char. The issue I am getting with the code is that the code runs fine when I have size <=5000 but when the size value is greater than that like 6000 or 9033 . It starts giving me this error:

terminate called after throwing an instance of std::bad_alloc
what(): std::bad_alloc
Aborted(core dumped)

I got the above error when my size was 9033 and this loop got stuck at 5923.

I think memory should not be a big issue in my case because RAM size is 4GB and no other big memory consuming program is running along with it.

Please guide me what can I do to avoid this problem.

struct  DoubleTableEntries **NewDoubleTable;
NewDoubleTable = new DoubleTableEntries*[size];


for(int i = 0; i < size; ++i)
          { 
        NewDoubleTable[i] = new DoubleTableEntries[256*256];
  }

Upvotes: 0

Views: 1397

Answers (1)

Jeff
Jeff

Reputation: 3525

You're out of memory, specifically virtual address space allowed for your process.

5923 * (256 * 256) * 8B ~= 3GiB.

32 bit OSs will allow only somewhere between 2 and 3 GiB of virtual address space per process, and will reject attempts to allocate more with this exception.

If you are running a 64 bit OS and building a 64 bit executable, you may have hit your swap limit, which would cause the same error.

Your options at this point are to build a 64 bit binary (if your are using a 64 bit OS), which will still hit disk swap around your sample sizes, or to rework your system if possible to deal with fewer 256^2 * 8B = 0.5 MiB tables at a time.

Upvotes: 5

Related Questions