Explicitly assign or access values to or from specific address/location in memory?

前端 未结 3 1855
礼貌的吻别
礼貌的吻别 2021-01-16 07:03

My exact question is that there is any provision in c and c++ to assign a value explicitly to particular address for example suppose I want to stor

相关标签:
3条回答
  • 2021-01-16 07:39

    It depends on the platform and OS (if any) you are using. The herohuyongtao's solution is usually valid BY THE LANGUAGE'S POINT OF VIEW. The real problem is the meaning of addresses in your environment (in other words: what is your goal exactly?). If you are using a modern OS, addresses are almost surely virtual (i.e. translated in a transparent way by the CPU's memory management unit). If your goal is to write on some specific PHYSICAL cell (i.e. because it hosts a device and you want to drive it), you must first be sure that the OS has created a view on the physical address for you. The way to do it depends completely on the OS's API. Even if you don't ave any OS, it's nevertheless possible that the addresses you are handling are not "physical". It depends on the CPU and the way it's initialized. A completely different case is when your goal is to change a particular address because "you know" that in your environment it has a specific meaning. I.e. an OS may store at a known virtual address some useful information. Many OSs are used to store at a fixed address a copy of the environment variables and command line arguments, so the C runtime library can easily find them. If this is your case, the herohuyongtao's suggestion is usually good.

    0 讨论(0)
  • 2021-01-16 07:47

    You can do it like this:

    *(int *)0x1846010 = 20;  // store int value 20 at address 0x1846010
    

    Retrieving works similar:

    int x = *(int *)0x1846010;
    

    Note that this assumes that address 0x1846010 is writable - in most cases this will generate an exception like Access violation writing location 0x01846010.

    P.S. For your interest, you can check given address writable or not at run-time by using the following (borrowed from here):

    #include <fcntl.h>
    #include <unistd.h>
    
    int is_writeable(void *p)
    {
        int fd = open("/dev/zero", O_RDONLY);
        int writeable;
    
        if (fd < 0)
            return -1; /* Should not happen */
    
        writeable = read(fd, p, 1) == 1;
        close(fd);
    
        return writeable;
    }
    

    As discussed here, typically you will only ever do this kind of thing for embedded code etc where there is no OS and you need to write to specific memory locations such as registers, I/O ports or special types of memory (e.g. NVRAM).

    0 讨论(0)
  • 2021-01-16 07:53

    Yes.

    Assuming that 0x1846010 is a valid address to which you have write access, you can write:

    *(int*)0x1846010 = 20;
    

    And to access the stored value:

    printf("%d\n", *(int*)0x1846010);
    

    And if you actually do that in a program, it will probably crash, because 0x1846010 very probably isn't a valid address to which you have write access.

    Why exactly do you want to do this? How do you know (if you know) that 0x1846010 is a valid address?

    This assumes that the conversion of the integer value 0x1846010 to a pointer is even meaningful. The result of the conversion is implementation-defined, but is "intended to be consistent with the addressing structure of the execution environment".

    0 讨论(0)
提交回复
热议问题