Thread: Binary distribution problem

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    5

    Binary distribution problem

    1) Why is function sizeof() evaluating function bitset<16> of consuming 4 bytes, when it should be 2 bytes?

    2) Why is it now evaluating bitset<40> of consuming 8 bytes when it should be 5 bytes?

    3) Why is it evaluating bitset<5> of consuming 4 bytes, when the amount of bits isn't sufficient enough to create 1 byte.

    My assumption would be that it is including the bytes allocated for the bitset<> itself?

    Code:
    #include <iostream>
    #include <bitset>
    using namespace std;
    
    int main()
    {
        cout << "The amount of bytes function bitset<16> is consuming: " << sizeof(bitset<16>) << endl;
        
        return 0;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A class really only consists of its member data, so there is no "bytes allocated for the bitset<> itself."
    Nevertheless, as far as I can see, the standard does not guarantee what the size of bitset<N> should be, so it's up to the implementers.
    As for the second question, first, do you know what possible types that exist in C++ and their respective sizes?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    It probably rounds it to the nearest 32bit boundary. Something like:

    Bytes = 4 * ( ( Bits + 31 ) / 32 );

    Or:

    Bytes = ( ( Bits + 31 ) & ~31 ) >> 3;
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    In GCC's library implementation, it it computed by the following macro:
    Code:
    #define _GLIBCXX_BITSET_WORDS(__n) \
      ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
       ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
    Where bits per words is set to (__CHAR_BIT__ * __SIZEOF_LONG__)

    Why that is so, I have no idea.
    The main class just has an array of _GLIBCXX_BITSET_WORDS(__n) 'words'.
    Where __n is the template argument you pass.
    Last edited by manasij7479; 12-01-2014 at 10:14 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, that's a horrible implementation.
    In MSVC, it's:

    Code:
    template<size_t _Bits>
    	class bitset
    	{	// store fixed-length sequence of Boolean elements
    public:
    	typedef typename _If<_Bits <= 32, _Uint32t, _ULonglong>::type _Ty;
    ...
    }
    where _Uint32t = 32-bit type, _Ulonglong = 64-bit type (essentially, if Bits <= 32, _Ty is a typedef for a 32-bit type; otherwise a 64-bit type).

    So the implementation has two choices: either use some existing fixed type to represent the storage which means it will be limited to the available sizes offered by the language, or it could use a char array to hold the bits for a possibly more flexible representation.

    But think a little about how you'd implement it were it to use a char array. It might give you some insight.
    And if it's not implemented as a char array, then why is, for example, the size not 5 if using 40 bits? It might also help if you're familiar with hardware since implementations tend to be done in certain ways in order to work more efficiently on hardware.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by manasij7479 View Post
    In GCC's library implementation, it it computed by the following macro:
    Code:
    #define _GLIBCXX_BITSET_WORDS(__n) \
      ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
       ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
    Where bits per words is set to (__CHAR_BIT__ * __SIZEOF_LONG__)

    Why that is so, I have no idea.
    The main class just has an array of _GLIBCXX_BITSET_WORDS(__n) 'words'.
    Where __n is the template argument you pass.
    That macro looks like it gives you the minimum number of 32bit or 64bit units the template argument needs, so you would need to still multiply the number it returns by the sizeof(long) to get the byte count I believe. Which should scale to represent the system you are on.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  7. #7
    Registered User
    Join Date
    Nov 2014
    Posts
    5
    Hi, I apologize for my late reply. Thank you everyone for your helpful input.

    @Elysia, Don't the statements that define class sizeof() consume memory? So when the compilier intercepts the function, it has to reserve memory for sizeof(), along with the data inserted?

    (Sorry if my question doesn't make any sense, when it comes to any form computer science, I always have trouble conveying my thoughts clearly.)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Densetsu
    Don't the statements that define class sizeof() consume memory? So when the compilier intercepts the function, it has to reserve memory for sizeof(), along with the data inserted?
    sizeof is a compile time operator, not a function.
    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

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Densetsu View Post
    @Elysia, Don't the statements that define class sizeof() consume memory? So when the compilier intercepts the function, it has to reserve memory for sizeof(), along with the data inserted?
    No, sizeof is a compile-time operator, which means that the compiler will evaluate that and replace with the number of bytes in the compiled executable. A constant. So in your executable, you will have some instructions that says "print n" where n is a number.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by laserlight View Post
    sizeof is a compile time operator, not a function.
    So the new proposed standard change to make it both did not get accepted?

    I thought the change was C++ but, maybe it is in C standard it was proposed.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I do not recall any proposal about making sizeof a function. Why would you do it anyway?
    EDIT: Making sizeof able to behave like a function would break lots of existing code since it's often used in meta template programming.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Given that sizeof() doesn't even evaluate its operand, making it a function doesn't even make sense. There is a difference between evaluating an expression and computing what type or size the result of that expression will be.


    It is actually a coincidence of language rules that "sizeof expr" and "sizeof(expr)" have the same meaning - and that because the expression "expr" and "(expr)" are both expressions which yield the same result (in terms of value, if the expression is evaluated, and type).

    Personally, I'd be a tiny bit happier if people weren't allowed to believe sizeof() was a function. I've seen people make a huge number of mistakes because they believe that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. .NET framework distribution
    By Shingetsu Kurai in forum Windows Programming
    Replies: 4
    Last Post: 09-03-2011, 02:32 PM
  2. Exponential Distribution
    By charlest in forum C Programming
    Replies: 1
    Last Post: 11-24-2009, 09:37 PM
  3. Gamma distribution in C#?
    By dudeomanodude in forum C# Programming
    Replies: 2
    Last Post: 06-07-2008, 01:59 PM
  4. z-distribution function in C
    By petermichaux in forum C Programming
    Replies: 3
    Last Post: 01-14-2004, 01:37 AM
  5. Best Distribution
    By gnu-ehacks in forum Linux Programming
    Replies: 4
    Last Post: 11-21-2001, 03:59 AM