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?

↘锁芯ラ 提交于 2019-12-12 04:59:30

问题


For example

printf("%u",&a);

gives me output

65524

which is a 16 bit address.


回答1:


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*. Something like

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

will do the job.

That said, you should look into the concepts of virtual memory the first thing.




回答2:


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




回答3:


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);
}


来源:https://stackoverflow.com/questions/37256145/if-my-computer-is-a-32-bit-system-it-has-a-32-bit-address-right-but-when-i-pri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!