• 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: "Clark S. Cox III" <email@hidden>
  • Date: Tue, 13 Dec 2011 07:34:34 -0800

On Nov 26, 2011, at 4:23 PM, Kyle Sluder wrote:

> On Sat, Nov 26, 2011 at 4:17 PM, Greg Guerin <email@hidden> wrote:
>> 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.

Not true. The union solution is perfectly legal, and doesn't involve pointer aliasing.

> 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


See footnote 82:

"If the member used to access the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation."

--
Clark S. Cox III
email@hidden

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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

  • Prev by Date: Filling gradient in NSTableHeaderView not working properly
  • Next by Date: How select NSTextField programatically?
  • Previous by thread: Re: "byte orders" question
  • Next by thread: NSIndexSet of NSButtonCells in NSOutlineView
  • Index(es):
    • Date
    • Thread