Re: structs in Mutable containers
Re: structs in Mutable containers
- Subject: Re: structs in Mutable containers
- From: "Clark Cox" <email@hidden>
- Date: Tue, 16 Sep 2008 09:45:42 -0700
On Tue, Sep 16, 2008 at 7:49 AM, Sean McBride <email@hidden> wrote:
> On 9/16/08 8:24 PM, Graham Cox said:
>
>>CGPoints and NSPoints have the same structure so you can
>>cast one to t'other.
>
> You shouldn't really. You should use NSPointFromCGPoint/
> NSPointToCGPoint. Their implementation is in NSGeometry.h and is not a
> simple cast.
Unless you're building for the ObjC 2.0 runtime (or have defined
NS_BUILD_32_LIKE_64), in which case NSPoint, NSSize and NSRect are
just typedefs of CGPoint, CGSize and CGRect.
> NSPoint and CGPoint may be the same now, but they may not always be.
> (Consider that NSAffineTransformStruct and vImage_AffineTransform were
> the same, but the former changed from float to CGFloat and the latter
> stayed float, even in 64 bit.)
>
> Also, they are separate structs to the compiler, so it is free (though
> unlikely) to align and pack them differently.
No, it is not. The C standard guarantees that two structs with the
same initial sequence of members have the same layout as far as the
common members are concerned. The following is perfectly legal code:
#include <stdio.h>
typedef struct Foo { float x, y; } Foo;
typedef struct Bar { float x, y; } Bar;
int main() {
Foo f = {123,456};
//Treat f as if it is a Bar:
printf("x = %f\n", ((Bar*)&f)->x);
printf("y = %f\n", ((Bar*)&f)->y);
return 0;
}
--
Clark S. Cox III
email@hidden
_______________________________________________
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