Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 1 Feb 2002 10:31:06 +0100
From:      Cliff Sarginson <cliff@raggedclown.net>
To:        freebsd-questions@freebsd.org
Subject:   Re: unsigned char portability
Message-ID:  <20020201093106.GB1071@raggedclown.net>
In-Reply-To: <20020201071119.GA17682@student.uu.se>
References:  <F74DyNdauuNi4kysRWz00010abe@hotmail.com> <a3cdi5$19e5$1@kemoauc.mips.inka.de> <20020201031412.GA1950@raggedclown.net> <20020201071119.GA17682@student.uu.se>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, Feb 01, 2002 at 08:11:20AM +0100, Erik Trulsson wrote:
> On Fri, Feb 01, 2002 at 04:14:12AM +0100, Cliff Sarginson wrote:
> > On Thu, Jan 31, 2002 at 09:39:17PM +0000, Christian Weisgerber wrote:
> > > June Carey <carey_june@hotmail.com> wrote:
> > > 
> > > > I have a question I was hoping someone could answer.
> > > > Does the "unsigned char" C type have any machine architecture portability 
> > > > problems ?
> > > 
> > > For one thing, you don't know its size.  I'm told there are C
> > > implementations on DSPs and such that have 32-bit chars, simply
> > > because those processors don't support other datasizes or byte
> > > addressing.  Now whether you care about portability to such platforms
> > > is a different matter.
> > >
> > 
> > That doesn't matter, as long as you don't take liberties on it's assumed
> > size, i.e. if it matters use "sizeof", don't fiddle with it inside an aggregate 
> > like a struct except as a member of that struct, you can always treat is an
> > unsigned 8 bit quantity even on a Risc machine, where it's "real" size
> > will probably not be 8 bits (it is irrelevant whether the OS is FreeBSD
> > or not).
> 
> sizeof won't be much use there since 'sizeof(char)' (or 'sizeof(unsigned
> char)') is always 1 by definition. 

I was referring to sizeof as applied to structs, that was not clear from
the way I put it.

If it matters use CHAR_BIT to find
> out how many bits there are in a char.
> 
> Oh, and on most RISC machines you have CHAR_BIT==8 also.
> 
-ditto- above

struct foo {
        char c;
};

sizeof foo will not give the same answer on all machines

I think we are confusing two things here. One is how big a char is for
"practical" purposes, and how much storage space it takes on a
particular machine. As long as you don't play games that involve
architectural assumptions you should be ok (and before anyone says
people do not do that, they do, I spent a long time clearing up a mess
some hired-in programmer had made of some code by using certain
assumptions).
 
> -- 
> <Insert your favourite quote here.>
> Erik Trulsson
> ertr1013@student.uu.se

-- 
Regards
Cliff



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020201093106.GB1071>