【发布时间】:2009-04-28 21:25:01
【问题描述】:
我试图在 OS X 10.5.6 上强制使用 64 位长整数。在 Apple MacBook Intel Core 2 Duo 上运行。这是我的c代码:
#include<stdio.h>
int main()
{
long a = 2147483647; /*== 2^32 - 1*/
long aplus1;
printf("a== %d. sizeof(a) == %d \n", a, sizeof(a));
aplus1 = a+1;
printf("aplus1 = %d \n", aplus1);
}
不带任何开关的编译会产生以下结果:
$ gcc testlong.c -o testlong ;./testlong
a== 2147483647. sizeof(a) == 4
aplus1 = -2147483648
使用 -m64 开关编译产生:
$ gcc testlong.c -o testlong -m64; ./testlong
a== 2147483647. sizeof(a) == 8
aplus1 = -2147483648
所以第二个版本显然是使用 64 位存储,但仍然会产生溢出错误,虽然 2^32 应该很好地在 64 位整数的范围内。有什么想法吗?
我更喜欢可以从 gcc 选项强制执行的解决方案,而不是要求我更改多行源代码(我的实际问题不是具体的上述示例,而是我需要在更通用的情况下强制长整数运算情况)。
【问题讨论】: