Does address (&) give the compiler generated address of the generated address or loader?

int a; printf("address is %u", &a); 

What address is this ..? I mean, this is the address generated by the compiler, that is, the virtual address or physical address of the bootloader specified in RAM.?

As soon as it prints different addresses every time, I think it should be an address in RAM. I just want to make sure.

Please indicate any links that provide a link to your answer.

+4
source share
4 answers

The correct answer is: "it depends."

(printf should use the "% p" directive and direct the address to "void *" to clearly define:

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

although using% u will no doubt work for your specific compiler with any flags you use.)

As @Alex noted, the address is virtual if translation occurs (both in most modern OSs and when working in an "emulated physical" under a virtual machine). The address itself is usually determined at link or load time if "a" has a static storage duration, but at runtime (on the stack as @Als said) if not. Variables declared as โ€œstaticโ€ or โ€œexternalโ€ have a static duration; variables declared outside functional bodies have a static duration; and variables declared in function bodies, but without the use of "extern" or "static", have automatic storage duration (and, thus, are usually in the "stack", although there may be several stacks, as when using POSIX streams).

+2
source

The address returned for a local variable in user space is always a virtual address, not a physical address.

The variable a in your case is distributed across the local storage (stack), and each time you execute your program, the stack space allocated to your function, the variable is at a certain offset in this stack frame, since the address from the stack allocated to your program can be different each time the address returned for the variable is different.

+4
source

In any modern OS, all the addresses you have ever seen at C level are virtual addresses. The example you are giving is a variable on the stack, and the reason it would be different at each execution is because the address of the (virtual) stack is randomized for security reasons.

But in any case, even global characters allowed by the loader have virtual addresses in the address space of the process.

(All this may not be true for embedded devices, but usually this does not happen when learning C)

+3
source

I mean, this is the address generated by the compiler, that is, the virtual address or loader sets the physical address in RAM.

False dichotomy. The address generated by the compiler is moved by the linker, and that address is returned with &. This is a virtual address if you are not working on something odd, like NetWare 3, which does not use VM.

0
source

Source: https://habr.com/ru/post/1400337/


All Articles