Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - good to have typedef?

 
Thread Tools Search this Thread
Old 11-06-2009, 09:07 AM   #11
Default Re: good to have typedef?


On Nov 6, 3:47 am, Ian Collins <ian-n...@hotmail.com> wrote:
> Andy Champ wrote:
> > James Kanze wrote:


> >> In which case, uint32_t doesn't necessarily buy you that
> >> much. (It does restrict portability, however, since it is
> >> guaranteed not to be available on some machines.)


> > Luckily for _me_ I only need to worry about Windoze boxes -
> > albeit 64 bit is likely to come up soon. And I'm also lucky
> > that my byte order matches the spec. - not everyone does the
> > ISO 9660 trick of storing a number in both common orders,
> > it's not space efficient!


> > But what should I use if I want to represent a
> > guaranteed-to-be-32-bit quantity?


> uint32_t if the system has it. If not, define your own.


If he only has to worry about Windows, the system has it. On
systems which don't have it, however, defining your own might be
a bit difficult, and the type will probably be significantly
slower than one of the built-in types. (On at least one system,
the built-in unsigned types are significantly slower than the
the signed ones, because the hardware doesn't directly support
them.)

--
James Kanze


James Kanze
  Reply With Quote
Old 11-07-2009, 06:44 PM   #12
Gerhard Fiedler
 
Posts: n/a
Default Re: good to have typedef?
James Kanze wrote:

> On Nov 6, 3:47 am, Ian Collins <ian-n...@hotmail.com> wrote:
>> Andy Champ wrote:
>>> James Kanze wrote:

>
>>>> In which case, uint32_t doesn't necessarily buy you that much. (It
>>>> does restrict portability, however, since it is guaranteed not to
>>>> be available on some machines.)

>
>>> Luckily for _me_ I only need to worry about Windoze boxes - albeit
>>> 64 bit is likely to come up soon. And I'm also lucky that my byte
>>> order matches the spec. - not everyone does the ISO 9660 trick of
>>> storing a number in both common orders, it's not space efficient!

>
>>> But what should I use if I want to represent a
>>> guaranteed-to-be-32-bit quantity?

>
>> uint32_t if the system has it. If not, define your own.

>
> If he only has to worry about Windows, the system has it. On systems
> which don't have it, however, defining your own might be a bit
> difficult, and the type will probably be significantly slower than
> one of the built-in types. (On at least one system, the built-in
> unsigned types are significantly slower than the the signed ones,
> because the hardware doesn't directly support them.)


I'm curious... what system doesn't "have" a uint32_t? I work also on
small 8bit micros (in C, but that's not really the issue here), and if I
really need it, I can use a uint32_t. It's not a native word size on
those systems, but the compiler couldn't care less about that. (Well,
the compiler writer of course does, and I do, too, where performance and
resources are an issue -- but the system "has it".)

Gerhard


Gerhard Fiedler
  Reply With Quote
Old 11-07-2009, 06:48 PM   #13
Gerhard Fiedler
 
Posts: n/a
Default Re: good to have typedef?
Nick Keighley wrote:

> Lovely bug:-
>
> Station_id read_station_id (FILE *in)
> {
> Station_id station_id;
> fscanf (in, "%d", &station_id);
> return station_id;
> }
>
> Station_id was a short.


You should get a compiler or linter that checks for such things -- or
use C++ streams in the first place

I found a few of these while porting a project from VC++ to gcc. gcc has
an option to check printf-type format strings.

Gerhard


Gerhard Fiedler
  Reply With Quote
Old 11-07-2009, 08:43 PM   #14
Bo Persson
 
Posts: n/a
Default Re: good to have typedef?
Gerhard Fiedler wrote:
> James Kanze wrote:
>
>> On Nov 6, 3:47 am, Ian Collins <ian-n...@hotmail.com> wrote:
>>> Andy Champ wrote:
>>>> James Kanze wrote:

>>
>>>>> In which case, uint32_t doesn't necessarily buy you that much.
>>>>> (It does restrict portability, however, since it is guaranteed
>>>>> not to be available on some machines.)

>>
>>>> Luckily for _me_ I only need to worry about Windoze boxes -
>>>> albeit 64 bit is likely to come up soon. And I'm also lucky
>>>> that my byte order matches the spec. - not everyone does the ISO
>>>> 9660 trick of storing a number in both common orders, it's not
>>>> space efficient!

>>
>>>> But what should I use if I want to represent a
>>>> guaranteed-to-be-32-bit quantity?

>>
>>> uint32_t if the system has it. If not, define your own.

>>
>> If he only has to worry about Windows, the system has it. On
>> systems which don't have it, however, defining your own might be a
>> bit difficult, and the type will probably be significantly slower
>> than one of the built-in types. (On at least one system, the
>> built-in unsigned types are significantly slower than the the
>> signed ones, because the hardware doesn't directly support them.)

>
> I'm curious... what system doesn't "have" a uint32_t? I work also on
> small 8bit micros (in C, but that's not really the issue here), and
> if I really need it, I can use a uint32_t. It's not a native word
> size on those systems, but the compiler couldn't care less about
> that. (Well, the compiler writer of course does, and I do, too,
> where performance and resources are an issue -- but the system "has
> it".)
>


Systems like these, that are based on 36-bit hardware

http://www.unisys.com/clearpath


Requiring them to have a uint32_t would be REALLY expensive. To top it
off, these machines also have 9-bit bytes, one's complement
arithmetic, and 72-bit non-IEEE floating point.

Unlike Java, C++ could actually be implemented.


Bo Persson




Bo Persson
  Reply With Quote
Old 11-09-2009, 12:07 AM   #15
James Kanze
 
Posts: n/a
Default Re: good to have typedef?
On Nov 7, 7:44 pm, Gerhard Fiedler <geli...@gmail.com> wrote:
> James Kanze wrote:
> > On Nov 6, 3:47 am, Ian Collins <ian-n...@hotmail.com> wrote:
> >> Andy Champ wrote:
> >>> James Kanze wrote:


> >>>> In which case, uint32_t doesn't necessarily buy you that
> >>>> much. (It does restrict portability, however, since it
> >>>> is guaranteed not to be available on some machines.)


> >>> Luckily for _me_ I only need to worry about Windoze boxes
> >>> - albeit 64 bit is likely to come up soon. And I'm also
> >>> lucky that my byte order matches the spec. - not everyone
> >>> does the ISO 9660 trick of storing a number in both common
> >>> orders, it's not space efficient!


> >>> But what should I use if I want to represent a
> >>> guaranteed-to-be-32-bit quantity?


> >> uint32_t if the system has it. If not, define your own.


> > If he only has to worry about Windows, the system has it.
> > On systems which don't have it, however, defining your own
> > might be a bit difficult, and the type will probably be
> > significantly slower than one of the built-in types. (On at
> > least one system, the built-in unsigned types are
> > significantly slower than the the signed ones, because the
> > hardware doesn't directly support them.)


> I'm curious... what system doesn't "have" a uint32_t?


Neither of the Unisys mainframe architectures: one's 36 bit ones
complement, the other 48 bit signed magnitude (with 8 bits
reserved).

> I work also on small 8bit micros (in C, but that's not really
> the issue here), and if I really need it, I can use a
> uint32_t. It's not a native word size on those systems, but
> the compiler couldn't care less about that. (Well, the
> compiler writer of course does, and I do, too, where
> performance and resources are an issue -- but the system "has
> it".)


The compiler has to provide 32 bit arithmetic somehow, and if
the machine is 8 bits, using 32 bits for long, rather than more,
is the preferred solution. The problems are more with larger
machines, where the machine word size is 36 or 48 bits (or in
the past, 60 bits)---at one time, 36 bits was quite common.

--
James Kanze


James Kanze
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46