Number of bits and size of data types in bytes

Discussion in 'C++' started by Alex Buell, Jun 9, 2006.

  1. Alex Buell

    Alex Buell Guest

    I just wrote the following and have a question: Why does my code gets
    it wrong with the class Simple? See last show_size<Simple> function call
    in main () as below:

    #include <iostream>
    #include <string>
    #include <limits>

    class Simple
    {
    public:
    Simple();
    ~Simple();

    void set(std::string text);
    std::string get(void);

    friend std::eek:stream& operator<<(std::eek:stream& ostream, Simple&
    simple);

    private:
    std::string t;
    };

    Simple::Simple() :
    t("")
    {
    }

    Simple::~Simple()
    {
    }

    void Simple::set(std::string text)
    {
    t = text;
    }

    std::string Simple::get(void)
    {
    return t;
    }

    template <typename T>
    const int get_n_bits()
    {
    return std::numeric_limits<T>::digits + 1;
    }

    template <typename T>
    const char* pluralise(const char* singluar, const char* plural)
    {
    return (sizeof(T) == 1) ? singluar : plural;
    }

    template <typename T>
    std::eek:stream& show_size(std::eek:stream& stream, const char* type)
    {
    stream << type << " has ";
    stream << get_n_bits<T>() << " bits";
    stream << ", has length of ";
    stream << sizeof(T);
    stream << " ";
    stream << pluralise<T>("byte", "bytes");
    stream << ".\n";

    return stream;
    }

    int main(int argc, char *argv[])
    {
    show_size<bool>(std::cout, "bool");
    show_size<char>(std::cout, "char");
    show_size<short int>(std::cout, "short int");
    show_size<int>(std::cout, "int");
    show_size<long int>(std::cout, "long int");
    show_size<long long>(std::cout, "long long");
    show_size<float>(std::cout, "float");
    show_size<double>(std::cout, "double");
    show_size<long double>(std::cout, "long double");
    show_size<wchar_t>(std::cout, "wchar_t");
    show_size<Simple>(std::cout, "Simple");

    return 0;
    }


    --
    http://www.munted.org.uk

    Take a nap, it saves lives.
     
    Alex Buell, Jun 9, 2006
    #1
    1. Advertising

  2. Alex Buell <> wrote:
    > I just wrote the following and have a question: Why does my code gets
    > it wrong with the class Simple?


    What do you mean "gets it wrong"? What do you get and what do you
    expect?

    regards
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Jun 9, 2006
    #2
    1. Advertising

  3. Alex Buell

    Alex Buell Guest

    On Fri, 9 Jun 2006 13:59:28 +0200, I waved a wand and this message
    magically appeared from Jakob Bieling:

    > Alex Buell <> wrote:
    > > I just wrote the following and have a question: Why does my code
    > > gets it wrong with the class Simple?

    >
    > What do you mean "gets it wrong"? What do you get and what do you
    > expect?


    I got the result:
    Simple has 1 bits, has length of 4 bytes.

    This can't possibly be right? I'd have expected something bigger? Or
    am I missing something?
    --
    http://www.munted.org.uk

    Take a nap, it saves lives.
     
    Alex Buell, Jun 9, 2006
    #3
  4. Alex Buell <> wrote:

    > On Fri, 9 Jun 2006 13:59:28 +0200, I waved a wand and this message
    > magically appeared from Jakob Bieling:


    >> Alex Buell <> wrote:


    >>> I just wrote the following and have a question: Why does my code
    >>> gets it wrong with the class Simple?


    >> What do you mean "gets it wrong"? What do you get and what do you
    >> expect?


    > I got the result:
    > Simple has 1 bits, has length of 4 bytes.


    > This can't possibly be right? I'd have expected something bigger? Or
    > am I missing something?


    Your implementation for std::string is probably using just a pointer
    (and I assume a pointer is 4 bytes on your platform) to store the
    string, which is why your Simple class (which just contains a string) is
    4 bytes long.

    The "1 bit" info is a bit misleading tho. "numeric_limits" is not
    meaningful in your case, because there is no specialization for this
    type. It would not make much sense either, because your class does not
    represent a numeric value, so there are no digits to be stored.

    Your function returns 1, because numeric_limits (the unspecialized
    template) has 0/false for all its members .. and you add 1 to that ..

    hth
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Jun 9, 2006
    #4
  5. Alex Buell wrote:
    > On Fri, 9 Jun 2006 13:59:28 +0200, I waved a wand and this message
    > magically appeared from Jakob Bieling:
    >
    >> Alex Buell <> wrote:
    >>> I just wrote the following and have a question: Why does my code
    >>> gets it wrong with the class Simple?

    >>
    >> What do you mean "gets it wrong"? What do you get and what do you
    >> expect?

    >
    > I got the result:
    > Simple has 1 bits, has length of 4 bytes.
    >
    > This can't possibly be right? I'd have expected something bigger? Or
    > am I missing something?


    Nothing besides the fact that 'numeric_limits' is for arithmetic types
    mostly, and to have it work for you it has to be _specialised_, which
    you don't seem to have done.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 9, 2006
    #5
  6. Alex Buell

    Alex Buell Guest

    On Fri, 9 Jun 2006 09:54:17 -0400, I waved a wand and this message
    magically appeared from Victor Bazarov:

    > > This can't possibly be right? I'd have expected something bigger? Or
    > > am I missing something?

    >
    > Nothing besides the fact that 'numeric_limits' is for arithmetic types
    > mostly, and to have it work for you it has to be _specialised_, which
    > you don't seem to have done.


    Ah, I need to do that. Thanks.
    --
    http://www.munted.org.uk

    Take a nap, it saves lives.
     
    Alex Buell, Jun 9, 2006
    #6
  7. Alex Buell

    Alex Buell Guest

    On Fri, 9 Jun 2006 15:52:57 +0200, I waved a wand and this message
    magically appeared from Jakob Bieling:

    > > This can't possibly be right? I'd have expected something bigger? Or
    > > am I missing something?

    >
    > Your implementation for std::string is probably using just a
    > pointer (and I assume a pointer is 4 bytes on your platform) to store
    > the string, which is why your Simple class (which just contains a
    > string) is 4 bytes long.
    >
    > The "1 bit" info is a bit misleading tho. "numeric_limits" is not
    > meaningful in your case, because there is no specialization for this
    > type. It would not make much sense either, because your class does
    > not represent a numeric value, so there are no digits to be stored.
    >
    > Your function returns 1, because numeric_limits (the
    > unspecialized template) has 0/false for all its members .. and you
    > add 1 to that ..


    You're absolutely right, I'll change it so it returns 0 for non numeric
    types.
    --
    http://www.munted.org.uk

    Take a nap, it saves lives.
     
    Alex Buell, Jun 9, 2006
    #7
  8. Alex Buell

    Howard Guest

    [OT response] Re: Number of bits and size of data types in bytes

    "Alex Buell" <> wrote in message
    >
    > Take a nap, it saves lives.


    Except while driving.
     
    Howard, Jun 9, 2006
    #8
  9. Alex Buell

    Alex Buell Guest

    Re: [OT response] Re: Number of bits and size of data types inbytes

    On Fri, 09 Jun 2006 14:35:04 GMT, I waved a wand and this message
    magically appeared from Howard:

    > > Take a nap, it saves lives.

    >
    > Except while driving.


    Wiseguy.
    --
    http://www.munted.org.uk

    Take a nap, it saves lives.
     
    Alex Buell, Jun 9, 2006
    #9
  10. Alex Buell wrote:
    > On Fri, 9 Jun 2006 09:54:17 -0400, I waved a wand and this message
    > magically appeared from Victor Bazarov:
    >
    > > > This can't possibly be right? I'd have expected something bigger? Or
    > > > am I missing something?

    > >
    > > Nothing besides the fact that 'numeric_limits' is for arithmetic types
    > > mostly, and to have it work for you it has to be _specialised_, which
    > > you don't seem to have done.

    >
    > Ah, I need to do that. Thanks.


    Rather use

    CHAR_BIT * sizeof(T)

    if you want the size in bits where CHAR_BIT is defined in <climits>.

    numeric_limits<T>::digits

    gives the number of digits in terms of the internally used base which
    needs not be 2 for float types. It also excludes the sign bit for
    integers and gives only the mantissa digits for float types.
     
    Markus Schoder, Jun 9, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sarmin kho
    Replies:
    2
    Views:
    828
    A. Lloyd Flanagan
    Jun 15, 2004
  2. Miki Tebeka
    Replies:
    1
    Views:
    446
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  3. Replies:
    5
    Views:
    548
    Flash Gordon
    Apr 9, 2006
  4. Replies:
    8
    Views:
    506
    Bob Hairgrove
    Apr 10, 2006
  5. Zhi
    Replies:
    2
    Views:
    1,140
    Mike Treseler
    Oct 9, 2007
Loading...

Share This Page