简体   繁体   中英

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];
  }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM