Sign in to follow this  

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

This topic is 4854 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
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
Quote:
Original post by Miserable
OTOH I am less and less certain that a char can ever be anything but a byte ...



I've found this post on another web page.


Quote:

I've done a web search, and discovered that's not always true:

eg on TI C30/C40 DSP's
sizeof(char) = sizeof(float)= sizeof(long) = sizeof(int) = 1

1 just happens to be 32 bits, which is the smallest adressable unit.

sizeof(char) always has to be 1, but I'm not sure that it has to be one byte. But it does seem that
char *p; p++; will always increment (int)p by 1.
Systems as obscure as this one are probably not worth worrying about. Interesting, though.


http://www.codeproject.com/cpp/PointerArithmetic.asp#xx753543xx

Share this post


Link to post
Share on other sites
Quote:
Original post by Miserable
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.


No, in this case I'm really only interested in bytes. Whether a byte has 5, 7, 8 or god knows how many bits means nothing to me.

Share this post


Link to post
Share on other sites
I was just thinking, if sizeof returns the size of its operand based on the size of a char, all I'd have to do is to figure out the size of one char and then I could deduct the size of the operand in bytes from that, right?


char test[2];

char *pChar = test;

unsigned int nAddr1 = reinterpret_cast <unsigned int> ( pChar );
unsigned int nAddr2 = reinterpret_cast <unsigned int> ( ++pChar );

unsigned int nSizeOfCharInBytes = abs( nAddr2 - nAddr1 );



Would that work?

Share this post


Link to post
Share on other sites
I think the only way to do this is to use the preprocessor and typedefs, so on platforms with strange char sizes you can just add another typedef for it.

I think gcc has header files for exactly this problem :)

Share this post


Link to post
Share on other sites
I'm pretty sure a char is always a single byte. That byte may be 8 or 9 or however many bits long.

http://www.parashift.com/c++-faq-lite/intrinsic-types.html

Share this post


Link to post
Share on other sites
Quote:
Original post by tj963
I'm pretty sure a char is always a single byte. That byte may be 8 or 9 or however many bits long.

http://www.parashift.com/c++-faq-lite/intrinsic-types.html


There was a counterexample posted above.

Share this post


Link to post
Share on other sites
Interestingly, Kernighan and Ritchie's 'The C programming language' (second edition) defines char as 'a single byte, capable of holding one character in the local character set'. (And yes, I do worry about how Unicode works with this) As C++ is supposedly backwards compatible with C...

Share this post


Link to post
Share on other sites
If I recall a single character is supposed to be 8 bits long (using an experience that I've had with decoding a binary string).

Basically I remember in one of my highschool coding classes the teacher had us write encryption algorithms and had us try to decode eachothers "secret message". In my group a friend of mine took the binary string and split it up into 8 bit groups and decoded the letters from the original message from there.

Share this post


Link to post
Share on other sites
the char type is guaranteed to be sizeof(1) and sizeof is guaranteed to return the size in byte. So, use a char.
According to the ISO standard atleast.

Share this post


Link to post
Share on other sites
Found this quote on a site when searching for cleaver things to use as my sign.. Originally snatched from comp.std.c

Quote:

From: Dennis Ritchie
Newsgroups: comp.std.c
Subject: Re: Computing sizeof() during compilation
Organization: Bell Labs, Lucent Technologies

> You are right. It was nice back in the days when things like
>
> #if (sizeof(int) == 8)
>
> actually worked (on some compilers).

Must have been before my time.

Dennis


If I understands the GCC docs right, __attribute__((mode(__byte__))) will ensure that the variable is of a one-byte integer type, if that helps...

God Speed

Share this post


Link to post
Share on other sites
Quote:
Original post by DrPizza
In C and C++ a char is one byte long by definition.

It is not, however, guaranteed to be 8 bits long.


I thought that by definition a byte is 8 bits long

Share this post


Link to post
Share on other sites
ASCII is only from 0 to 127 and so it only needs 7 bits, maybe some systems only give you 7 bits for it, perhaps you should explicitly say signed or unsigned to make it 8 bits

Share this post


Link to post
Share on other sites

This topic is 4854 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this