2

Is there a maximum to the number of elements in an std::bitset?

In my code (VC++2010) 1<<20 crashes with a stack overflow, but 1<<19 works.

(I'm dealing with huge inputs.)

Pedro d'Aquino
  • 5,130
  • 6
  • 36
  • 46

1 Answers1

1

As far as I see this has nothing to do with the maximum number of elements supported in bitset but has to do with the amount of memory that can be allocated on stack. On VS normally, the maximum memory that can be allocated on stack is 1 MB and if you cross this limit then you get stack overflow. If you require more than this amount of memory, then I would suggest to allocate memory from heap using new rather than allocating on stack. In such case, the memory allocation will fail only when new truly runs out of memory.

Naveen
  • 74,600
  • 47
  • 176
  • 233
  • 2
    Making the large object `static` usually works. Most operating systems can handle large executable binary images/global objects just fine, and I prefer not to resort to the heap. – Potatoswatter Mar 30 '12 at 04:15