简体   繁体   English

如果我的计算机是32位系统,则它具有32位地址,对吗? 但是,当我在C中打印任何内存地址时,为什么我得到的地址<32bit?

[英]If my computer is a 32 bit system, it has a 32 bit address right? But when I print any memory address in C why do I get an address <32bit?

For example 例如

printf("%u",&a);

gives me output 给我输出

65524 65524

which is a 16 bit address. 这是一个16位地址。

Because you used wrong format specifier which invokes undefined behavior . 因为您使用了错误的格式说明符,从而导致未定义的行为

To print a pointer, you should use %p format specifier and cast the argument to void* . 要打印指针,应使用%p格式说明符并将参数强制转换为void* Something like 就像是

 printf("%p",(void *)&a);

will do the job. 会做的工作。

That said, you should look into the concepts of virtual memory the first thing. 就是说,您应该首先了解虚拟内存的概念。

You can also simply answer your assumption about address size by checking the size of any pointer instead of a test variable's address: 您还可以通过检查任何指针的大小而不是测试变量的地址来简单地回答有关地址大小的假设:

printf("%zu\n", sizeof(int*));
printf("%zu\n", sizeof(float*));

Assuming that a byte in all systems equals eight bit, you can see about size of address. 假设所有系统中的一个字节等于八位,则可以看到地址的大小。

Please see this SO post 请参阅此帖子

To find the highest possible address for (most of) the currently common systems do this: 要找到(大多数)当前常见系统的最高地址,请执行以下操作:

#include <stdint.h>
#include <stdio.h>

int main(void)
{
  uintptr_t uip = (uintptr_t) -1;
  void * vp = (void*) uip;

  printf("%p\n", vp);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM