Igor,
You need to take another look at what's going on. The first part of what you
say is correct:
> First thing that is wrong with the code, is that the compiler does _not_
> guarantee that allocated space will be zeroed, that is, char s[10] may
> actually contain 'A7z91msbaO' instead of \x0 times ten. So, line s[0]='4'
> just replaces the 'A' with the '4'. Thus the new version of s{10} would be
> '47z91msbaO' .
However, you start going wrong here:
> Lets subtract '0' from s[0], we get '\x4A7z91msbaO' where
> \x4 is one character.
When you say "we get...", that's not what you get. What you get is an integral
value (char or integer, I don't care which at the moment) equal to 4. This
subtraction operation does not affect what's in the "s" array.
Instead, this integral value of 4 is cast via (double) into a double value. The
string "s" is not converted! Just the result of the expression (s[0] - '0').
-- Keith Rollin
-- Palm OS Emulator engineer
Igor Mozolevsky <[EMAIL PROTECTED]> on 01/30/2001 05:00:34 PM
Please respond to "Palm Developer Forum" <[EMAIL PROTECTED]>
Sent by: Igor Mozolevsky <[EMAIL PROTECTED]>
To: "Palm Developer Forum" <[EMAIL PROTECTED]>
cc: (Keith Rollin/US/PALM)
Subject: Re: GCC Compiler Bug? (Or am I just losing my mind?)
At 16:21 30/01/2001 -0800, you wrote:
> > You cannot simply typecast between two incompatible types!!! Have you heard
> > of stol() or stod() or whatevet they're called... Can't remeber now, my
> > mind is on /^perl$/o at the moment...
>
>A little too much so, I think. There are no invalid casts in Thomas's code.
>Converting chars to doubles is just fine, as is converting doubles to long
>integers.
>
>-- Keith Rollin
>-- Palm OS Emulator engineer
Ok, I'll explain...
> char s[10];
> double d;
> s[0] = '4';
> d = (double) (s[0] - '0');
> StrCopy(s, "4");
> d = (double) (s[0] - '0');
First thing that is wrong with the code, is that the compiler does _not_
guarantee that allocated space will be zeroed, that is, char s[10] may
actually contain 'A7z91msbaO' instead of \x0 times ten. So, line s[0]='4'
just replaces the 'A' with the '4'. Thus the new version of s{10} would be
'47z91msbaO' . Lets subtract '0' from s[0], we get '\x4A7z91msbaO' where
\x4 is one character. Now type-casting '\x47z91msbaO' is doing to give
quite a lengthy double.... Same goest for StrCopy, now you'd just get
'\x4\0z91msbaO', still quite a value! Second issue is alignment, I am not
sure about Palm architecture, but most machines prefer to have integers
aligned at 2- or 4- byte boundary... Whilst the compiler (gcc) does not
give any warning about the first one, the second one can be switched on
using -Wcast-align, which is _not_ a subset of -Wall. Generally assuming
that whatever you allocate is going to be automatically zeroed is not a
good practice.
Have fun,
Igor M-)
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/