Bug 45713 - sizeof std::bitset<ULONG_MAX> == 1
Summary: sizeof std::bitset<ULONG_MAX> == 1
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 3.4.4
: P3 minor
Target Milestone: 4.6.0
Assignee: Paolo Carlini
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-09-17 23:18 UTC by Luc Hermitte
Modified: 2010-09-18 01:31 UTC (History)
1 user (show)

See Also:
Host: -
Target: -
Build: -
Known to work:
Known to fail:
Last reconfirmed: 2010-09-17 23:53:05


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Luc Hermitte 2010-09-17 23:18:26 UTC
Hello,

std::bitset cannot be instantiated with an extremely big number of bits like ULONG_MAX.
e.g.
--------- >% --------------
#include <iostream>
#include <bitset>

int main()
{
    std::cout << sizeof (std::bitset<0xffffffff>) << std::endl;
}
--------- >% --------------
outputs 1 with g++ 3.4.4 and 4.3.4 on cygwin, and with gcc 3.4.? on Solaris.

It seems related to the macro _GLIBCXX_BITSET_WORDS that overflows. With a simple division + modulo formula, the bug vanishes.
i.e.

#define _GLIBCXX_BITSET_WORDS(__n) \
 ((__n) < 1 ? 0 : (((__n)/_GLIBCXX_BITSET_BITS_PER_WORD) + (((__n)%_GLIBCXX_BITSET_BITS_PER_WORD==0)?0:1)))

HTH,

--Luc Hermitte
Comment 1 Paolo Carlini 2010-09-17 23:53:05 UTC
Confirmed, I will apply a variant (__n is unsigned here and the original expression can be simplified) of your patch momentarily, after testing. Of course the issue is really noticeable only on 32-bit machines...
Comment 2 paolo@gcc.gnu.org 2010-09-18 01:29:46 UTC
Subject: Bug 45713

Author: paolo
Date: Sat Sep 18 01:29:31 2010
New Revision: 164388

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=164388
Log:
2010-09-17  Luc Hermitte  <hermitte@free.fr>
	    Paolo Carlini  <paolo.carlini@oracle.com>

	PR libstdc++/45713
	* include/std/bitset: Fix _GLIBCXX_BITSET_WORDS macro.
	* testsuite/23_containers/bitset/45713.cc: New.
	    

Added:
    trunk/libstdc++-v3/testsuite/23_containers/bitset/45713.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/std/bitset

Comment 3 Paolo Carlini 2010-09-18 01:31:09 UTC
Fixed for 4.6.0.