https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40013501-CH1-SW1html
Integer data typeios |
ILP32 size數據結構 |
ILP32 alignmentapp |
LP64 sizeless |
LP64 alignmentide |
---|---|---|---|---|
|
1 bytepost |
1 byteui |
1 byteurl |
1 byte |
|
1 byte |
1 byte |
1 byte |
1 byte |
|
2 bytes |
2 bytes |
2 bytes |
2 bytes |
|
4 bytes |
4 bytes |
4 bytes |
4 bytes |
|
4 bytes |
4 bytes |
8 bytes |
8 bytes |
|
8 bytes |
4 bytes |
8 bytes |
8 bytes |
pointer |
4 bytes |
4 bytes |
8 bytes |
8 bytes |
|
4 bytes |
4 bytes |
8 bytes |
8 bytes |
|
4 bytes |
4 bytes |
8 bytes |
8 bytes |
|
4 bytes |
4 bytes |
8 bytes |
8 bytes |
|
8 bytes |
4 bytes |
8 bytes |
8 bytes |
|
8 bytes |
4 bytes |
8 bytes |
8 bytes
|
Floating-point type |
ILP32 size |
LP64 size |
---|---|---|
|
4 bytes |
4 bytes |
|
8 bytes |
8 bytes |
4 bytes |
8 bytes
|
數據類型改變:
指針類型由4字節改成8字節;
long,size_t,NSInteger,CFIndex 變爲8字節;
CGFloat 由4字節變爲8字節。
At a high level, to make your code 64-bit clean, you must do the following:
Avoid assigning 64-bit long
integers to 32-bit integers.
Avoid assigning 64-bit pointers to 32-bit integers.
Avoid pointer and long
integer truncation during arithmetic operations (or other arithmetic problems caused by the change in integer types).
Fix alignment issues caused by changes in data type sizes. (內存對齊問題)
Ensure that memory structures that are shared between the 32-bit and 64-bit runtimes share a similar layout.
Rewrite any assembly language code so that your code uses the new 64-bit opcodes and runtime.
Avoid casting variadic functions to functions that take a fixed number of parameters, or vice versa.(變參函數 和 定參函數之間的轉換)
1 Foundation Objects May Be Expensive for Small Payloads 使用NSDictionary、NSArray等對象保存少許數據時耗費更多的內存。
2 Choose a Compact Data Representation 使用緊湊的數據結構,少一些變量(一些變量能夠由某個變量計算得出)
3 Pack Data Structures 注意內存對齊問題,編譯器可能會添加額外的補齊空間。
4 Use Fewer Pointers 64-bit 中指針佔用更多的內存空間。
5 Memory Allocations Are Padded to Preserve Alignment
When allocating memory for C structs, it may be more efficient for you to allocate a few large blocks of memory instead of allocating memory for each individual struct.
6 Cache Only When You Need To
1 Do Not Cast Pointers to Integers
2 Use Data Types Consistently
使用數據類型要保持一致。
枚舉也是有類型的。
NSIntger,CGFloat 已經變成64位了,與int、float進行計算時要注意。
3 Be Careful When Performing Integer Computations
注意有符號和無符號數:有符號數和無符號數進行計算獲得的結果是無符號的;
Unsigned values are zero extended (not sign extended) when promoted to a larger type.(無符號數 轉換成更大的類型時 用0補齊額外的位數)
Signed values are always sign extended when promoted to a larger type, even if the resulting type is unsigned.(有符號數 轉換成更大的類型時 用符號位補齊額外的位數)
Constants (unless modified by a suffix, such as 0x8L
) are treated as the smallest size that will hold the value. Numbers written in hexadecimal may be treated by the compiler as int
, long
, and long long
types and may be either signed
or unsigned
types. Decimal numbers are always treated as signed
types.(常量的對待,十進制爲有符號數)
The sum of a signed value and an unsigned value of the same size is an unsigned value.
位操做Bits and Bitmasks:
If you want the mask value to contain zeros in the upper 32 bits in the 64-bit runtime, the usual fixed-width mask works as expected, because it will be extended in an unsigned fashion to a 64-bit quantity.
If you want the mask value to contain ones in the upper bits, write the mask as the bitwise inverse of its inverse
4 Create Data Structures with Fixed Size and Alignment
使用明確長度的int類型:
Type |
Range |
---|---|
int8_t |
-128 to 127 |
int16_t |
-32,768 to 32,767 |
int32_t |
-2,147,483,648 to 2,147,483,647 |
int64_t |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
uint8_t |
0 to 255 |
uint16_t |
0 to 65,535 |
uint32_t |
0 to 4,294,967,295 |
uint64_t |
0 to 18,446,744,073,709,551,615 |
5 Allocate Memory Using sizeof
6 Update Format Strings to Support Both Runtimes
7 Take Care with Functions and Function Pointers
8 Use the Built-in Synchronization Primitives
9 Never Hard-Code the Virtual Memory Page Size
10 Go Position Independent
11 Don’t Forget your 32-bit Version
12 Make Your App Run Well in the 32-Bit Runtime