[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: endianness (was Re: sysadmin qualifications (Re: apt-get vs. aptitude))



Jonathan Dowland <jmtd@debian.org> writes:

> On Thu, Oct 17, 2013 at 05:29:33PM +0200, berenger.morel@neutralite.org wrote:
>> Speaking about endianness, it really is hard to manage:
>> 
>> void myfunction( ... )
>> {
>> #ifdef BIG_ENDIAN
>> move_bytes_in_a_specific_order
>> #else
>> move_bytes_in_the_other_specific_order
>> #endif
>> }
>
> Bad way to manage endian in C. Better to have branching based on C
> itself (rather than preprocessor), otherwise you run the risk of never
> testing code outside the branch your dev machine(s) match. E.g. use
>
> char is_little_endian( … ) {
>   int i = 1;
>   int *p = &i;
>   return 1 == *(char*)p;
> }
>
> Or similar. The test will likely be compiled out as a no-op anyway with
> decent compilers (GCC: yes; Sun Workshop: no.)

What's wrong with htonl and other similar functions/macroes?


Reply to: