Re: sizeof(aStruct) in Xcode 4
Re: sizeof(aStruct) in Xcode 4
- Subject: Re: sizeof(aStruct) in Xcode 4
- From: Jens Alfke <email@hidden>
- Date: Tue, 21 Jun 2011 12:58:55 -0700
On Jun 21, 2011, at 12:39 PM, McLaughlin, Michael P. wrote:
> struct AA {
> int n;
> double val;
> };
>
> struct BB {
> short ss, tt;
> double val;
> };
>
> sizeof(AA) and sizeof(BB) both come out as 16 even though they "should" be 12.
No, I think 16 is correct. The ‘double’ field is 8 bytes in size so it will be aligned on an 8-byte boundary; in each case that introduces 4 bytes of padding before it. (Disclaimer: I’m not a C language guru.)
There are some #pragmas you can use to tweak struct alignment, to get tighter packing; I think they’re documented in the GCC docs but they should probably apply to Clang too.
> I pack sequences of these structs into NSData objects (at which point they are 12 bytes) and pass them (NSPipe) to some subprocesses (NSTask) running independently. As such, sizeof(struct …) is important in the conversion of NSData to something else.
If you’re just sending these around between processes on the same machine, it shouldn’t matter what exact alignment or size they are, as long as everyone’s using the same struct definitions.
—Jens
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden