Number of bits and size of data types in bytes

A

Alex Buell

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;
}
 
J

Jakob Bieling

Alex Buell said:
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
 
A

Alex Buell

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?
 
J

Jakob Bieling

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
 
V

Victor Bazarov

Alex said:
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
 
A

Alex Buell

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.
 
A

Alex Buell

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.
 
M

Markus Schoder

Alex said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top