Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storage differences between OS X and iOS

Tags:

macos

ios

storage

I'm writing some binary data files on OS X to read on iOS devices. I was trying to read and write the following structure:

struct poiStruct {
    double latitude, longitude;
    char titleString[41];
};

I find using sizeof that the structure occupies 64 bytes on OS X and 60 bytes on iOS.

Any ideas/explanations on this are much appreciated!

like image 340
Hank Brandenburg Avatar asked Jun 27 '26 08:06

Hank Brandenburg


2 Answers

Difference is not in size of elements but in padding, on OSX the struct gets aligned to 8 bytes boundary so it's

XXXXXXXX double (8 bytes)
YYYYYYYY double (8 bytes)
ZZZZZZZZ char (41 bytes)
ZZZZZZZZ
ZZZZZZZZ
ZZZZZZZZ
ZZZZZZZZ
ZPPPPPPP padding (7 bytes) = 64 bytes

while on iOS it's aligned to 4 bytes so it's

XXXX double (8 bytes)
XXXX 
YYYY double (8 bytes)
YYYY
ZZZZ char (41 bytes)
ZZZZ
ZZZZ
ZZZZ
ZZZZ
ZZZZ
ZZZZ
ZZZZ
ZZZZ
ZZZZ
ZPPP padding (3 bytes) = 60 bytes

The alignment is a requirement which is different because OS X runs on a 64 architecture while iOS runs on ARM6/7 is a 32 bytes architecture. Compiling your OS X application in 32bit mode (which is possible but I discourage it) should make this difference disappear.

like image 139
Jack Avatar answered Jun 29 '26 13:06

Jack


Your Mac App is a 64bit App while the iOS version is 32bit. The difference comes from different padding requirements of the architectures, specifically 32bit requires variables to be 4byte aligned while 64bit requires 8byte alignment, so the compiler will add a different amount of padding at the end of your structure.

like image 31
JustSid Avatar answered Jun 29 '26 12:06

JustSid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!