Jump to content
  • Advertisement
Sign in to follow this  
Red Ant

Need a type that is guaranteed to be one byte long (C++)

This topic is 5217 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

I'm looking for a way to define a type that is always exactly one byte long, regardless on which OS the program is compiled. Is this possible, and if so, how would you go about this? I'm not sure if char is necessarily guaranteed to be one byte. IIRC I've heard some people say that on some systems, a char may well be 2 or even 4 bytes long.

Share this post


Link to post
Share on other sites
Advertisement
Hmm, sucks. :( Okay, I'm just conveniently going to assume that on most systems a char == 1 byte long then. Speaking of which, can anyone actually name systems/compilers for which this does not apply?

Share this post


Link to post
Share on other sites
Why don't you use sizeof(char) to check the type at compile time. You could generate an error message for example.

Share this post


Link to post
Share on other sites
sizeof returns the size of the respective type in terms of char, I think. So that would effectively tell me that a char is exactly as large as one char. ;)

Share this post


Link to post
Share on other sites
Hmmm, I'm actually beginning to doubt whether my last statement is true. I've checked about half a dozen language references on the web and they all stated that

Quote:

The sizeof operator yields the size in bytes of the operand, which can be an expression or the parenthesized name of a type.


=\

Share this post


Link to post
Share on other sites
Quote:
Original post by Red Ant
I'm looking for a way to define a type that is always exactly one byte long, regardless on which OS the program is compiled. Is this possible, and if so, how would you go about this?
I'm not sure if char is necessarily guaranteed to be one byte. IIRC I've heard some people say that on some systems, a char may well be 2 or even 4 bytes long.

... What's worse, you have no guarantees about the size of a byte; it's usually 8 bits, of course, but it's not universal. The C++ standard specifies a minimum size for a char - I believe it's 8 bits; see Stroustrup, TCPL 4.6 - but beyond that, again, no guarantees. (You can find out - in C++, #include <limits> and check std::numeric_limits<char>::digits().) The question now is, do you really need one byte, or do you need 8 bits?

If you need a certain number of bits, you can use stdint.h (in C) or Boost's cstdint library.

Share this post


Link to post
Share on other sites
In the C++ standard I read this in chapter "5.3.3 Sizeof":

Quote:

The sizeof operator yields the number of bytes in the object representation of its operand.
...
sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the
result of sizeof applied to any other fundamental type (3.9.1) is implementationdefined.
...


Maybe that helps ;-)

Share this post


Link to post
Share on other sites
Quote:
Original post by Red Ant
Hmmm, I'm actually beginning to doubt whether my last statement is true. I've checked about half a dozen language references on the web and they all stated that

Quote:

The sizeof operator yields the size in bytes of the operand, which can be an expression or the parenthesized name of a type.


=\

No, sizeof(char) is 1 by definition (Stroustrup, TCPL 4.6), so other sizes are given in multiples of char sizes. OTOH I am less and less certain that a char can ever be anything but a byte ...

Share this post


Link to post
Share on other sites
Okay, the MSND C++ reference has this to say about sizeof.

Quote:
The sizeof operator yields the size of its operand with respect to the size of type char.


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_sizeof_operator.asp

Grrr, which one is it now? I wish they'd make up their minds already!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!