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

Started by
43 comments, last by C-Junkie 19 years, 7 months ago
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Advertisement
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
In C and C++ a char is one byte long by definition.

It is not, however, guaranteed to be 8 bits long.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
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
--"The greatest pleasure in life is in doing what people say you cannot do." -- Walter Bageholt
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
Quote:Original post by Red Ant
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?

*** Source Snippet Removed ***

Would that work?
No. a char is the smallest addressable size. Therefore, you CANNOT get a type smaller than a char.

You code dies, because you're assuming that pointers always point to a memory location counting by BYTES. As the TI post earlier pointed out, some archs count memory by "dwords" (or whatever the word of the day for 32-bits is) or other sizes.

That, btw, means that the smallest addressable size is 32-bits large.

Use __int8, it it can't get 8 bits, it errors (iirc)
Red Ant, the code you posted will always return 1 (for any pointer type!), ++ and - on a pointer moves it by the size of the data pointed to.

You'd have to be more specific about what you are using it for for us to say the best way to define your byte.

cstdint and it's int8_t might do it, or for example, this might do what you want:
int byte:8;


...
If sizeof(char)=1 and it is 32bits, do we still consider it a byte?
If not, why would 9bit char's be considered a byte?

(PS wchar_t is for larger character sets, e.g. unicode)
(PSS Just about anything that starts with __ is implementation specific, i.e. __int8, except for special macros such as __LINE__ or __FILE__ which are always capitalized.)
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Hi just came back building freetype 2.1.9 and they have an example of a machine which has a 16 bit char type.
from ftconfig.h:
Quote: /* There are systems (like the Texas Instruments 'C54x) where a `char' */
/* has 16 bits. ANSI C says that sizeof(char) is always 1. Since an */
/* `int' has 16 bits also for this system, sizeof(int) gives 1 which */
/* is probably unexpected. */
Quote:Original post by Red Ant
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.


Then what you're looking for is a char. By definition a char is one byte long. sizeof(char)==1. One byte (here) is at least 8 bits, but may be more.
Wow, I'm indredibly confused now. There's just so much seemingly contradictive info. =0 Anyhow, for the time being I will do what some people suggested and just typedef unsigned char byte;, but while we're at ... could we clear up the definition of what a 'byte' really is?

After some thinking, I'd say a byte just is whatever happens to be the (as C-Junkie has said) smallest addressable size on that particular machine, be it 5, 8, 16, 32 or 64 bits long. That's really the only criterion there is. Well anyway, if this is the case, then char (if it is also the size of the smallest addressable unit) is indeed just a synonym for the word 'byte' (pun not intended), and sizeof can be said to return either the size of its operands in terms of char or in bytes, so both statements are equally true. Also, we're guaranteed that there's always a type in C/C++ that allows us to directly access the smallest addressable unit on any machine.
Does that sound right? Anybody disagree?

This topic is closed to new replies.

Advertisement