【发布时间】:2020-11-28 00:49:55
【问题描述】:
我安装了 ubuntu/trusty64 vagrant box (https://app.vagrantup.com/ubuntu/boxes/trusty64),我有以下代码:
#include <stdio.h>
int main () {
short count = -1;
int a = 5 ;
int* a_ptr = &a;
char b = 'A';
char* b_ptr = &b;
printf ("count's value = %d \n", count);
printf ("count's address = %p \n", &count);
printf ("a's value = %d \n", a);
printf ("a's address = %p \n", &a);
printf ("a_ptr value = %p \n", a_ptr);
printf ("a_ptr address = %p \n", &a_ptr);
printf ("a_ptr deref'ed= %d \n", *a_ptr);
printf ("\n");
printf ("b's value = %d \n", b);
printf ("b's address = %p \n", &b);
printf ("b_ptr value = %p \n", b_ptr);
printf ("b_ptr address = %p \n", &b_ptr);
printf ("b_ptr deref'ed= %d \n", *b_ptr);
printf ("\n");
for (count = -30; count < 500; count++) {
printf ("test: %3d: %p: (%d, %x)\n", count, b_ptr+count, *(b_ptr+count),*(b_ptr+count)) ;
}
}
显示输出:
count's value = -1
count's address = 0x7ffd9440876a
a's value = 5
a's address = 0x7ffd94408764
a_ptr value = 0x7ffd94408764
a_ptr address = 0x7ffd94408758
a_ptr deref'ed= 5
b's value = 65
b's address = 0x7ffd94408757
b_ptr value = 0x7ffd94408757
b_ptr address = 0x7ffd94408748
b_ptr deref'ed= 65
test: -15: 0x7ffd94408748: (87, 57)
test: -14: 0x7ffd94408749: (-121, ffffff87)
test: -13: 0x7ffd9440874a: (64, 40)
test: -12: 0x7ffd9440874b: (-108, ffffff94)
test: -11: 0x7ffd9440874c: (-3, fffffffd)
test: -10: 0x7ffd9440874d: (127, 7f)
我只显示0x7ffd94408748处的内容,也就是0x7ffd94408757,也就是b的地址。
请注意,内存地址分布在假定的 64 位处理器上的 6 个内存位置。我测试了其他数据类型,似乎其他数据类型也以每个内存位置最多 8 位表示,因此我得出结论,它是一台 8 位机器。
但是,当我运行 $getconf WORD_BIT 时,我得到 32,当我运行 $uname -a 时,我看到“Linux vagrant-ubuntu-trusty-64 3.13.0-170-generic #220-Ubuntu SMP Thu May 9 12:40:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux。”
在我的 VirtualBox 管理器中,我看到这台机器是 64 位 Ubuntu。
这台机器的正确字长是多少?虚拟化是否有一些东西使它的真实字长看起来与所说的不同(即 64 位)?
我在以下位置看到类似的帖子: Determine word size of my processor How to determine whether a given Linux is 32 bit or 64 bit? how to find if the machine is 32bit or 64bit
但是尝试上面的链接显示了 32 和 64 位之间的各种结果,所以我更加困惑。
【问题讨论】:
-
为什么这么复杂?假设您在不强制 32/64b 的情况下进行编译:
sizeof(int) ==sizeof(long) == 4=> 32 位,sizeof(long) == 8=> 64b。你也可以看看sizeof一个指针,或者看看limits.h的定义
标签: c linux operating-system word-size