[Dibbler] dibbler-server an debian-armel

Tomasz Mrugalski thomson at klub.com.pl
Mon Aug 18 23:07:25 CEST 2008


On Sun, 17 Aug 2008 somebody known as Christian Andersen wrote:

> I did some debugging and changed the code.
>
> -        unsigned short code   = ntohs( *((unsigned short*) (buf+pos)));
> -        pos+=2;
> -        unsigned short length = ntohs( *((unsigned short*) (buf+pos)));
> -        pos+=2;
> +	unsigned short tmp = 0;
> +	memcpy(&tmp, buf+pos, 2);
> +	unsigned short code = ntohs(tmp);
> +	pos+=2;
> +	memcpy(&tmp, buf+pos, 2);
> +	unsigned short length = ntohs(tmp);
> +	pos+=2;
Well, my proposal was quicker:

    unsigned short code   = buf[pos]*256 + buf[pos+1];
    pos+=2;
    unsigned short length = buf[pos]*256 + buf[pos+1];
    pos+=2;

Didn't it work? As far as I understand, reading a byte is always 
successful. Byte multipled by 256 is automatically cast to short.

Calling memcpy just to copy 2 bytes is quite an overhead. I just want to 
make completely sure before I rewrite all network related code...

Regards,
Tomek



More information about the Dibbler mailing list