Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a C pointer refer to the physical or virtual address [duplicate]

Tags:

c

pointers

When I am using a pointer in C for a PC program, does it point to the physical or virtual address of the variable ?

int x = 10;
int* ptr = &x;
like image 293
Ramy Sameh Avatar asked Oct 27 '14 10:10

Ramy Sameh


People also ask

Is pointer virtual or physical address?

A pointer is an address that provides a translation within some virtual address space to a piece of physical memory. DTrace executes your D programs within the address space of the operating system kernel itself.

Can virtual and physical address be same?

Yes, it's definitely possible for the same address to map to different physical memory depending on the process that's referencing it. This is in fact the case under Windows. Show activity on this post. Each process has a address space of 4GB in a 32 bit system.

What is mean by virtual address and physical address in C?

It is a virtual address generated by the CPU while a program is running. It is referred to as a virtual address because it does not exist physically. Using this address, the CPU access the actual address or physical address inside the memory, and data is fetched from there.

Who converts virtual address to physical address?

The run time mapping between Virtual address and Physical Address is done by a hardware device known as MMU. In memory management, the Operating System will handle the processes and move the processes between disk and memory for execution .


2 Answers

It depends.

If you're writing a application that will run on top of a operating system, that is, in user mode and the operating system uses virtual memory, it will point to a virtual address (or rather, it will point to a physical address but not the same physical address that will in fact be used).

If you're using an operating system without virtual memory or if you're writing (parts) of the kernel code it will point to the physical address.

like image 106
iveqy Avatar answered Feb 07 '23 02:02

iveqy


It depends on your OS!

In Windows and Linux it's typical virtual memory! Also user space application canno't access the memory using physical addresses.

like image 42
Rizier123 Avatar answered Feb 07 '23 03:02

Rizier123