3

Suppose we write a program in C and print the address of one of the variables declared in the program, is the address that gets printed on the screen the virtual address or the physical address of the variable? If it is the virtual address, why is it that it still has the same range as a bit range of physical memory? Eg. for a 32 bit machine if it returns 0x833CA23E.

PsiX
  • 1,661
  • 1
  • 17
  • 35
shaveenk
  • 1,983
  • 7
  • 25
  • 37
  • 1
    Neither the C nor the C++ programming language contains any notion of "virtual" or "physical" memory. There's only "memory". The question cannot be answered from within either language. – Kerrek SB Jun 29 '14 at 22:41
  • The only machines you'll find where physical and virtual addresses are the same are systems that lack virtual memory (and a memory management unit) at all, such as many embedded systems. For example, ucLinux is a version of Linux designed to operate on these platforms. FreeRTOS is a tiny OS (can get it down to around 8kB) with no virtual memory support. – Mike DeSimone Jun 29 '14 at 22:56
  • 1
    Also, in modern systems, the "bitness" of the machine is the number of bits in a virtual address. Physical addresses may not have as many bits (e.g. on a 64-bit machine, physical addresses may only have 40 or 48 bits actually wired; higher-order physical address bits are ignored). In older architectures, it described the width of the internal data registers, and (physical) addresses had more bits. For example the 8086 was 16-bit but had 20-bit addressing (1024k); the 6502 was 8-bit but had 16-bit addresses (64k). – Mike DeSimone Jun 29 '14 at 23:01
  • @MikeDeSimone: Note that at the hardware level, current x86-64 is only 48-bit for virtual addresses too. – Oliver Charlesworth Jun 29 '14 at 23:05
  • True, but a pointer is still 8 bytes, not 6. – Mike DeSimone Jun 29 '14 at 23:38

3 Answers3

7

The address is going to be a virtual address in virtual memory, because the application has no knowledge of physical memory. That is hidden by the kernel and the MMU.

I am not sure what you mean by the same "bit range". If you have a 32-bit address space it will range across the entire 32-bit space regardless of what amount of physical memory you have. Likewise for 64-bit.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • does that mean if I have 4GB of ram on a 32 bit machine, running a 32 bit OS, the virtual memory space and the physical memory space will be the same? – shaveenk Jun 29 '14 at 22:44
  • 1
    @shaveenk, No, the kernel will choose a mapping and the MMU will enforce the mapping. Generally your program is sharing resources with the kernel and other applications, so it would not make sense to map all virtual addresses to the same physical addresses for all applications. – merlin2011 Jun 29 '14 at 22:46
  • Also, if you have virtual memory support, the computer can use some disk space as additional memory, "paging out" pages of RAM to disk when they aren't used and some other process needs more RAM. The processes involved are halted while the paging occurs, so they don't notice anything amiss; to them, it looks like their data is always in RAM. – Mike DeSimone Jun 29 '14 at 22:52
4

In most typical cases (Windows, Linux, etc.) it'll be a virtual address.

In the typical cases like Linux and Windows, both virtual addresses and physical addresses are normally 32 bits, so having numbers in the same range becomes inevitable. It is possible to allocate more than 4 gigabytes of memory, and when/if you do so, you end up with addresses larger than 32 bits--but unless you take special steps to do that, a 32-bit address is what you'll get by default.

When you do use more than 4 GB of memory under a 32-bit OS, you're normally doing so via some special API, such as Windows' Address Windowing Extensions. Using these, you get access to more than 4 GB of RAM, but it's not what's going to happen by default with code that's even close to portable.

Some (versions of some) operating systems also use Intel's Physical Address Extensions (PAE) to give the system as a whole access to more than 4 GB of RAM, but even when these are in use, any single process running on the system is still limited to addressing 4 GB (i.e., with PAE, you can have a limit of 4 GB per process, whereas older systems had a limit of 4 GB total, divided as needed between the processes).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

It will be a 32 bit virtual address in most cases.

If your OS supports does paging then it would be the virtual address. It could have been mapped to the same physical address using paging. Linux and Windows do paging.

Another thing that matters is the architecture. On Intel x86 32bit system it will be 32 bit address. The first 10 bits of the address will be used to get page table. The second 10 bits will be used to get page from the selected page table. And the last 12 bits will give you the actual physical address from that page.

I hope it answers your question.

Sami
  • 155
  • 3