Read and write to a memory location

前端 未结 9 1263
忘掉有多难
忘掉有多难 2021-01-05 05:40

After doing lot of research in Google, I found below program

#include 

int main()
{
    int val;
    char *a = (char*) 0x1000;
          


        
9条回答
  •  不知归路
    2021-01-05 06:14

    If you are running your code in user space(which you are), then all the addresses you get are virtual addresses and not physical addresses. You cannot just assume and write to any virtual address.
    In fact with virtual memory model you cannot just assume any address to be a valid address.It is up to the memory manager to return valid addresses to the compiler implementation which the handles it to your user program and not the other way round.

    In order that your program be able to write to an address:

    1. It should be a valid virtual address
    2. It should accessible to the address space of your program

提交回复
热议问题