Archived

This topic is now archived and is closed to further replies.

Quickie

This topic is 5474 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Which C++ header file contains the sizes (in bytes or bits) of the standard simple data types (char, short, int, long, float, double, long double)? I need this info to make a header file that will work with different compilers (a neat template class that could help remove unneccessary derefencing).

Share this post


Link to post
Share on other sites
cout << sizeof(char) << endl;
cout << sizeof(int) << endl;

[edited by - Abob on January 19, 2003 10:57:41 PM]

Share this post


Link to post
Share on other sites
Will this work?

  
#include <iostream>

using namespace std;

int main()
{
cout << "int: " << sizeof(int) << endl;
cout << "bool: " << sizeof(bool) << endl;
cout << "long: " << sizeof(long) << endl;
cout << "short: " << sizeof(short) << endl;
cout << "char: " << sizeof(char) << endl;
cout << "long double: " << sizeof(long double) << endl;

return 0;
}


results are:

  
int: 4
bool: 1
long: 4
short: 2
char: 1
long double: 8

Share this post


Link to post
Share on other sites
No, I need the values at compile-time so that the preprocessor can handle them. I know in climits there's all the constants I need for integers and chars, but I need some way to find out how many bits are in floats / doubles at COMPILE-TIME (and it varies with different compilers so I can't just assume a double is 8 bytes or anything like that).

[edited by - Brobanx on January 19, 2003 11:08:19 PM]

Share this post


Link to post
Share on other sites
I think you''re looking for for integer values and for floating points. Both are in the std namespace.

Share this post


Link to post
Share on other sites
Could this work?

    
#define intSize sizeof(int)

#define longSobuleSize sizeof(long double)

...


[edited by - HunterTKilla on January 19, 2003 12:12:09 AM]

Share this post


Link to post
Share on other sites
The preprocessor doesn''t recognize sizeof. Although, the original problem I''m trying to solve can be easily overcome using the boost::call_traits library.

Share this post


Link to post
Share on other sites
quote:
Original post by Brobanx
The preprocessor doesn''t recognize sizeof.

Of course. The preprocessor is just like a text replace utility.

hmm... what kind of code construct you are trying to do?

Share this post


Link to post
Share on other sites
I always thought sizeof was evaluated at compile-time. Why exactly won''t it work for you again? Is there a reason the preprocessor needs to know what the size of types are?

Share this post


Link to post
Share on other sites
I''m confused as to why sizeof is no good here? It is the proper operator. I think that maybe you should read the first few chapters of your C++ book again Brobanx.

Share this post


Link to post
Share on other sites
well, in float.h FLT_MANT_DIG, DBL_MANT_DIG and LDBL_MANT_DIG are defined as the number of bits in the mantissa, and...err....guess the exponent

DBL_MANT_DIG is defined as 53 (MSVC6, x86) even though there are actually 52 mantissa bits, so I'm presuming this includes the sign bit.

Perhaps you could assume that the size in bytes is the next multiple of 2.


e.g.
mantBits = DBL_MANT_DIG-1;
mantBytes = (mantBits+7)/8;
dblBytes = mantBytes+(mantBytes%2);

I know this is a VERY dodgy hack, and don't even know if it would even work on anything other than an x86, but it's a suggestion none-the-less.


Joanus D'Mentia
---------------
Serving Mars since 3069.

[edited by - joanusdmentia on January 20, 2003 7:39:45 AM]

Share this post


Link to post
Share on other sites
sizeof IS evaluated at compile time. It''s not a runtime operator. sizeof(int) is always replaced by the compiler with a constant integral value at compile time.

Share this post


Link to post
Share on other sites
quote:
Original post by Brobanx
No, I need the values at compile-time so that the preprocessor can handle them.

The preprocessor runs *before* compile-time. That''s why it''s called a "pre" processor.

Share this post


Link to post
Share on other sites
Thank you SabreMan, I got about 5 responses from people that didn't know what the preprocessor was.

I wanted some way to check the size of types before compiling some template specializations, so that I could more efficiently pass parameters / const references in templatized classes (like my vector class) where I may not know the size of a parameter, and cannot judge whether a reference or a copy is more efficient.

ie...


      
template<class T>
class const_reference {
T const& val;
... other code here ...
};

#if (NUMBYTES_DOUBLE < NUMBYTES_POINTER)
template <>
class const_reference<double> {
double val;
... other code here ...
};
#endif


However, it was much quicker when I just downloaded the Boost libraries and used boost::call_traits classes.

[edited by - Brobanx on January 20, 2003 12:23:37 AM]

Share this post


Link to post
Share on other sites