On Mar 29, 2:51 am, SasQ <s...@go2.pl> wrote:
> Dnia Sun, 25 Mar 2007 01:57:59 +0100, SasQ napisa³(a):
> Thanks for everyone for their explanations. It has cleared me
> a couple of things. Now I'll try to sumarize it:
> Machine word: sizeof(long int):
> 8-bit 4 Emulated as four 8-bit registers.
> 16-bit 4 Emulated as two 16-bit registers.
> 32-bit 4 Doesn't have to be emulated.
> 64-bit 4 or 8? Doesn't have to be emulated.
You're still limiting yourself too much. C++ has nothing to say
about the size of a machine word. All it guarantees is that
char has at least 8 bits (but there have been implementations
with 9, 10 and 32 bit chars, at least), that int is at least 16
bits (I've seen 16, 32, 36 and 48---and 24 wouldn't surprise me
for some machines I've heard of), that long is at least 32 bits,
and the next standard will also require a long long of at least
64 bits.
In addition, you are guaranteed that the size of any type is a
positive integral value, and that:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
Note that if char is 32 bits, all of the integral types can have
a size of 1.
> Now I have doubts only on 64-bit machines, where 'long int'
> may be that 'at least 32 bits', but it can be more also 
> So if it can, sould it or not?
All of the 64 bit machines I use have a 64 bit long. It seems
the most natural.
> Next, we have 'short int', which the Standard requires to be
> at least 16 bits. So, let's look:
> Machine word: sizeof(short int):
> 8-bit 2 Emulated as two 8-bit registers
> 16-bit 2 Doesn't have to be emulated.
> 32-bit 2 Doesn't have to be emulated.
> 64-bit 2 Doesn't have to be emulated.
> Here, I have some doubts on 64-bit platform. On 32-bit
> 'short' is mostly 16-bit and no more, because it should be
> able to be shorter than plain 'int'.
That's not required. On word addressed machines (e.g. Unisys
2200), it's probably the same as int. In fact, on a Unisys
2200, I would expect short, int and long all to have 36 bits.
> On 64-bit platforms
> it could be more, if plain 'int' were 64-bit. But I don't
> know how it is there, and I've seen only one particular
> case, where plain 'int' has still 32 bits, so the 'short
> int' has to be 16-bit.
There's no "has to" about it. In practice, on a byte addressed
64 bit machine, the vendor will probably want to offer access to
all natively supported lengths. Since there are four, and there
are only four integral types, there is only one solution.
> And now we come to plain 'int' type 
> The Standard requires it to be at least as much as 'short int',
> and defines it as the type most convenient for integer arithmetics
> on the particular platform. So, if we apply the same rules as
> for 'long int' [with the emulation], we would get:
> Machine word: sizeof(int):
> 8-bit 2?? Emulated as two 8-bit registers??
> 16-bit 2 Doesn't have to be emulated.
> 32-bit 4 Doesn't have to be emulated.
> 64-bit 4 or 8? Doesn't have to be emulated.
> I don't think emulating 'int' as two 8-bit registers to be
> the most convenient for the 8-bit platform to compute on
> integers
It's more convenient that using even more bytes, and it is the
least the standard allows.
> Even if 16-bit platforms could emulate C++
> Standard rules and feel good with it, for 8-bit machines
> there is something wrong, I think. Something, that was
> missed by the creators of Standard, or [more probably
]
> by me :/ So what is the thing I am missing here?
That people wanted C to be useful, and so defined a generally
useful set of rules for the period. (One could easily argue
today that an int should be required to be at least 32 bits.)
C++ just took over these rules.
> I think I know the theory [C++ Standard] but I don't know
> how to apply it in practice.
You apply it in practice by first deciding what your goals are.
If you're code targets desktop computers or larger, for example,
it's perfectly reasonable to assume that an int is at least 32
bits. If your code makes extensive use of the Windows API, for
its GUI, you might as well assume that int is 32 bits, and 2's
complement to boot. If you think that your code might have to
run on embedded systems, or on mainframes, or legacy systems,
then you'll have to be a lot more careful. Still, for most
code, all you have to worry about is the maximum and minimum
values you need to handle.
--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34