• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: "byte orders" question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "byte orders" question


  • Subject: Re: "byte orders" question
  • From: Sean McBride <email@hidden>
  • Date: Thu, 08 Dec 2011 10:45:31 -0500
  • Organization: Rogue Research Inc.

On Sat, 26 Nov 2011 16:23:47 -0800, Kyle Sluder said:

>> Since you're just doing a memcpy(), you can simply cast the bits and avoid
>> the copying.  Try this:
>>
>> float f = *((float*) &res);
>>
>> Or try defining a C union:
>>
>> union foo {  float f;  u_int32_t u;  };
>> union foo bar;
>> bar.u = CFSwapInt32HostToBig(value);
>> float f = bar.f;
>
>Neither of these is legal. You are not allowed to alias a pointer to
>two different types (except pointer-to-char). See Section 6.5,
>paragraph 7 of the C99 standard:
>http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

(My reply is late, but...)

Strangely, there is no CF version, but there is NSSwapBigDoubleToHost() and variants, which are more expressive that the trickery above.

As Kyle says, don't cast float* to int* (or variations), you see everyone doing it, but it's illegal.  The union strategy is dubious too, but in practice safer.  Looking in NSByteOrder.h you see it is the technique used by NSSwapBigDoubleToHost() and variants.

Cheers,

--
____________________________________________________________
Sean McBride, B. Eng                 email@hidden
Rogue Research                        www.rogue-research.com
Mac Software Developer              Montréal, Québec, Canada


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: "byte orders" question
      • From: Scott Ribe <email@hidden>
  • Prev by Date: Re: Locks
  • Next by Date: Re: "byte orders" question
  • Previous by thread: Re: App crashing when adding to Open Recent menu
  • Next by thread: Re: "byte orders" question
  • Index(es):
    • Date
    • Thread