[Coco] Drivewire mixed results...

Theodore (Alex) Evans alxevans at concentric.net
Mon Sep 29 10:48:24 EDT 2008


Chuck Youse wrote:
> On Thu, 2008-09-25 at 07:56 -0500, Boisy Pitre wrote:
>> On Sep 25, 2008, at 7:52 AM, Chuck Youse wrote:
>>
>>>>> int calChecksum(char *ptr, int count)
>>>>> {
>>>>>    short Checksum = 0;
>>>>>    while(--count)
>>>>>    {
>>>>>         Checksum += *(ptr++);
>>>>>    }
>>>>>    return (Checksum);
>>>>> }
> 
>> I don't see right off-hand how that might be, but I'm open to  
>> suggestions.  Feel free to provide an improved function and I'll  
>> certainly incorporate it into the next rev of the specification  
>> document.
> 
> You need to be explicit about the signedness of 'char *ptr'.  Since the
> early days of C this has been an implementation-specific detail - ANSI
> both solved and compounded the problem by adding 'signed char' and
> 'unsigned char', which are distinct from 'char'.  (This is of course not
> orthogonal with respect to integers, which are by default signed.)
> 
> In any case, for usual checksum computations this wouldn't matter
> because two's complement and modulo arithmetic is nifty.  Unfortunately,
> you're computing a 16-bit checksum for 8-bit data, which means that
> whether or not the addend at *ptr is sign-extended is significant.
> 
> And no, I'm not just being pedantic; this actually matters.  Even gcc on
> different architectures will implement the rules differently depending
> upon what is most natural for that architecture, or [where applicable]
> how the native compiler behaves, or what compile-time options are given
> it..

Actually the size of the char, short and int are implementation 
dependent.  Char is merely guaranteed to be no larger than short and 
short no larger than int.  In fact it is common for the size of int to 
be either 16 or 32 bits.  The type of the return value for calChecksum 
and Checksum should be changed to march one another.



More information about the Coco mailing list