Check if app is built in 32 or 64-bit?

2020-07-10 06:25发布

How can I check whether my app is compiled in 32-bit or 64-bit ?

This is helpful to debug low level code (working with buffers for example).

标签: iphone ios
3条回答
萌系小妹纸
2楼-- · 2020-07-10 06:47
#ifdef __LP64__
    NSLog(@"64-bit\t");
#else
    NSLog(@"32-bit\t");
#endif
查看更多
疯言疯语
3楼-- · 2020-07-10 06:51

You could check the size of a pointer. I think on 32bit it is 4bytes and on 64 it should be 8.

if( sizeof(void*) == 4 ) then 32bit else 64bit
查看更多
Fickle 薄情
4楼-- · 2020-07-10 06:52

A compile time check would involve #ifdef'ing for __LP64__, which is ARM's data type size standard. A runtime solution would involve checking the size of pointers, like so:

if (sizeof(void*) == 4) {
    // Executing in a 32-bit environment
} else if (sizeof(void*) == 8) {
   // Executing in a 64-bit environment
}

Thankfully, pointer sizes are the one thing that the different standards for compiling 64-bit code seem to agree on.

查看更多
登录 后发表回答