Hi everyone. I want to know how to use a very large number of bitset.
A short version of my program:
1 2 3 4 5 6 7 8
constunsignedint T=4294967296; //Greatest number of unsigned int
#include <bitset>
int _tmain(int argc, _TCHAR* argv[])
{
std::bitset<T> &x=*(new std::bitset<T>);
x[1]=1; x[T-2]=1;
delete &x;
}
The program crash with a assestion that bitset index outside range.
The reason may be bitset only accept numbers in int range as parameter. When I reduce T by half, program runs fine.
How can I make bitset accept that large number, since my program need to generate repeatingly a random unsigned int and I need to check a number has been generated or not.
By setting it to 4294968296, you are requesting the class to allocate 512 MB of continuous memory. That's a massive amount and is probably not available.
What I want to do is check every number from 0 to 2^32 if they have been generated, so I need 2^32 bit.
By the way, if I reduce T by half, the program (with 256 MB RAM) runs normally. In 32-bit program, we can allocate 2 GB RAM, so I think RAM is not a problem.
If unsigned int is 32 bits the largest value it can hold is 4294967295. If you try to assign the value 4294967296 to T the value will wrap-around and become 0. You can't store much in a zero-sized bitset!
> constunsignedint T=4294967296; // Greatest number of unsigned int
That just can't be right. 2n is even, and std::numeric_limits<unsigned int>::max() must be odd.
Verify that there is no truncation taking place (print out the value of T).
Duplicate post. Can't delete it for some reason (permission denied etc.)