-2
//program to print address of variable    

#include<stdio.h>

main () {
    int a = 10;
    printf("%u", &a);
  return;
} 

when the address of a variable is displayed in C or any other programming language, is that address logical or physical tell me please

BLUEPIXY
  • 39,049
  • 7
  • 31
  • 69
Malik
  • 11
  • 6
  • logical address – krpra Oct 01 '17 at 18:34
  • very rare situations (example: 8bit AVR) , when address has different length than unsigned int, %p sholuld be used – Jacek Cz Oct 01 '17 at 18:36
  • 2
    It is garbage. @JacekCz **all 64-bit PCs fail on that**. And even when they're of the same size, the behaviour is still undefined. – Antti Haapala -- Слава Україні Oct 01 '17 at 18:36
  • @JacekCz: U wot? On pretty much every PC you buy at the moment, you have a 64 bit pointer and a 32 bit `unsigned`. Always use the correct format specifier. Pretty please, with sugar on top. – Bathsheba Oct 01 '17 at 18:37
  • 2
    To print an adress you must use `%p` format, or cast the adress into `uintptr_t` to print it in decimal – Gam Oct 01 '17 at 18:41
  • Note that the C code is poor quality C90 code. Good C code would use `int main(void)` and C90 code should use `return 0;` at the end (and that would be correct in C99 or later too, though technically it's not necessary there — you can omit the final `reutrn 0;` in later versions of the standard) – Jonathan Leffler Jun 22 '20 at 20:01

3 Answers3

1

It's neither in your case: "%p" is the appropriate format specifier for a pointer type; currently the behaviour of your program is undefined.

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

It will be the address that your C runtime provides, in that sense it's a logical address. On modern platforms, there are one if not two levels of abstraction between physical addresses and the ones you see in code such as this.

Weather Vane
  • 32,572
  • 7
  • 33
  • 51
Bathsheba
  • 227,678
  • 33
  • 352
  • 470
1

Whatever address you use in(user mode i.e. on the top of your operating system) your program are actually virtual/logical address except when you are writing a part of kernel or if you are using an OS without virtual memory support. e.g.- In assembly

mov eax,[rsi] ; just for example

The mapping of virtual to physical is the job of OS.

The address you use in your programs, the address that your CPU use to fetch data, is not real and gets translated via MMU to some physical address; everyone has one and its size depends on your system(Linux running 32-bit has 4GB address space)

Don't forget to cast the arg to printf to void* when using the address.

Please see my other answer for the standard to use %p specifier for using address in printf

0decimal0
  • 3,828
  • 2
  • 22
  • 38
1

It's the logical address. The virtual protection mechanisms will not allow you to work with real, physical addresses

Note that this happens in the 'common' case of normal applications in an environment with MMU, assuming that your application runs in user-mode, not in kernel mode. On a MMU-less machine, you will get the physical address; but then again, you might not have support for processes, so probably you won't be able to modify main as above.

krpra
  • 476
  • 1
  • 4
  • 19