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/

Reply via email to