Thread: STL bitset crash on to_ulong()

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    28

    Question STL bitset crash on to_ulong()

    guys i got a small problem,

    my program to crashes with a
    Code:
    std::overflow_error
    the following code causes it at the second statement:

    Code:
    #include <bitset>
    
    int main()
    {
    	bitset<64> x( string("0000000000000000000000000000010001101000011001010110110001101100") ) ;
    	
    	unsigned long y = x.to_ulong() ;
    
    	getchar() ;
    	return 0 ;
    }
    no crash if i use the decimal value instead :

    Code:
    	bitset<64> x( 18931346540 ) ;
    it doesn't make sense to me. could anyone explain?

    thanks,
    sym
    Last edited by symbiote; 01-12-2009 at 09:25 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    unsigned long on your platform is probably a 32 bit integer. It appears to work in the second case because 18931346540 might be a (currently) non-standard integer literal of a type with greater range than unsigned long (e.g., unsigned long long or __int64), but is truncated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Apparently the size of unsigned long is 32 bits. If the number is too large to convert bitset throws an exception.

    In the second case the literal constant is just too large for unsigned long and 4 bytes of it just gets chopped away. The bitset won't be constructed with the value you pass (instead it will likely be 1751477356).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Is your unsigned long type even 64 bits wide?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    28
    thanks i think i get it.

    i'm using vs 2005. i thought it was supposed to be 64 bits. hmm

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    MS doesn't make long 64 bits under any circumstance due to compatibility concerns.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using bitset class inside another
    By Dhekke in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 06:50 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM